Beginner’s Guide to Python

Python Built-in Methods – Python Dictionary pop() Method

Python Dictionary pop() Method Removes a key from the dictionary Usage If specified key is in the dictionary, the pop() method removes it and returns its value. You can also specify the default parameter that will be returned if the specified key is not found. If default is not specified and key is not in the dictionary, a KeyError is raised. Syntax dictionary.pop(key,default) Parameter Condition Description …

Python Built-in Methods – Python Dictionary items() Method

Python Dictionary items() Method Returns a list of key-value pairs in a dictionary Usage The items() method returns a list of tuples containing the key:value pairs of the dictionary. The first item in each tuple is the key, and the second item is its associated value. Syntax dictionary.items() Examples # Print all items from the dictionary D = …

Python Built-in Methods – Python Dictionary get() Method

Python Dictionary get() Method Returns the value for key if exists Usage The get() method returns the value for key if key is in the dictionary. You can also specify the default parameter that will be returned if the specified key is not found. If default is not specified, it returns None. Therefore, this method never raises a KeyError. It’s an easy way of getting the …

Python Built-in Methods – Python Dictionary copy() Method

Python Dictionary copy() Method Copies the dictionary shallowly Usage The copy() method returns the Shallow copy of the specified dictionary. Syntax dictionary.copy() Example D = {‘name’: ‘Bob’, ‘age’: 25} X = D.copy() print(X) # Prints {‘age’: 25, ‘name’: ‘Bob’} copy() vs Assignment statement Assignment statement does not copy objects. For example, old_Dict = {‘name’: ‘Bob’, ‘age’: 25} new_Dict = old_Dict …

Python Built-in Methods – Python Dictionary clear() Method

Python Dictionary clear() Method Removes all items from the dictionary Usage Use clear() method to remove all items from the dictionary. This method does not return anything; it modifies the dictionary in place. Syntax dictionary.clear() Example D = {‘name’: ‘Bob’, ‘age’: 25} D.clear() print(D) # Prints {} clear() vs Assigning Empty Dictionary Assigning an empty dictionary D = {} is …

Python Built-in Methods – Python Set update() Method

Python Set update() Method Updates the set by adding items from all the specified sets Usage The update() method updates the original set by adding items from all the specified sets, with no duplicates. You can specify as many sets as you want, just separate each set with a comma. If you don’t want to update the original set, use union() method. Syntax …

Python Built-in Methods – Python Set symmetric_difference_update() Method

Python Tuple count() Method Python Set symmetric_difference_update() Method Updates the set by keeping only elements found in either set, but not in both Usage The symmetric_difference_update() method updates the set by keeping only elements found in either set, but not in both. If you don’t want to update the original set, use symmetric_difference() method. The symmetric difference is actually the union of the …

Python Built-in Methods – Python Set symmetric_difference() Method

Python Set symmetric_difference() Method Returns a new set with items from all the sets, except common items Usage The symmetric_difference() method returns a new set containing all items from both the sets, except common items. If you want to modify the original set instead of returning a new one, use symmetric_difference_update() method. The symmetric difference is actually the union of the two sets, minus …

Python Built-in Methods – Python Set remove() Method

Python Set remove() Method Removes a specified item from the set Usage The remove() method removes a specified item from the set. If specified item is not present in the set, the method raises KeyError. Syntax set.remove(item) Parameter Condition Description item Required An item you want to remove from the set Examples # Remove ‘red’ from the set S = {‘red’, ‘green’, ‘blue’} …

Python Built-in Methods – Python Set issuperset() Method

Python Set issuperset() Method Determines whether all items in the specified set are present in the original set Usage The issuperset() method returns True if all items in the specified set are present in the original set, otherwise FALSE. Syntax set.issuperset(set) Parameter Condition Description set Required A set to search for common items in Basic Example # Check if all …