Category Archives: Library

15. If … Else

Python Conditions and If statements Python supports the usual logical conditions from mathematics: Equals: a == b Not Equals: a != b Less than: a < b Less than or equal to: a <= b Greater than: a > b Greater than or equal to: a >= b These conditions can be used in several ways, most commonly in “if statements”… Read More »

14.8 Dictionary Methods

Dictionary Methods Example: Create a dictionary with 3 keys, all with the value 0:x = (‘key1’, ‘key2’, ‘key3’)y = 0thisdict = dict.fromkeys(x, y)print(thisdict) Output:[‘key1’: 0, ‘key2’: 0, ‘key3’: 0] Python has a set of built-in methods that you can use on dictionaries. Method Syntax Description clear() dictionary.clear() Removes all the elements from the dictionary copy() dictionary.copy() Returns a… Read More »

14.7 Nested Dictionaries

Nested Dictionaries A dictionary can contain dictionaries, this is called nested dictionaries. Example Create a dictionary that contain three dictionaries:myfamily = {  “child1” : {    “name” : “Emil”,    “year” : 2004  },  “child2” : {    “name” : “Tobias”,    “year” : 2007  },  “child3” : {    “name” : “Linus”,    “year” : 2011  }} Output:{‘child1’: {‘name’: ‘Emil’, ‘year’: 2004}, ‘child2’: {‘name’: ‘Tobias’, ‘year’: 2007}, ‘child3’: {‘name’: ‘Linus’, ‘year’: 2011}} Or, if you want to add three dictionaries into a new dictionary:… Read More »

14.6 Copy Dictionaries

Copy a Dictionary You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a reference to dict1, and changes made in dict1 will automatically also be made in dict2. There are ways to make a copy, one way is to use the built-in Dictionary method copy(). Example Make a copy of a dictionary with the copy() method:thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}mydict = thisdict.copy()print(mydict) Output:{‘brand’: ‘Ford’,… Read More »

14.5 Loop Dictionaries

Loop Through a Dictionary You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well. Example Print all key names in the dictionary, one by one:for x in thisdict:  print(x) Output:brandmodelyear Example Print all values in the dictionary, one by one:for x in thisdict:  print(thisdict[x]) Output:FordMustang1964 Example… Read More »

14.4 Remove Dictionary Items

Removing Items There are several methods to remove items from a dictionary: Example The pop() method removes the item with the specified key name:thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}thisdict.pop(“model”)print(thisdict) Output:{‘brand’: ‘Ford’, ‘year’: 1964} Example The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead):thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}thisdict.popitem()print(thisdict) Output:{‘brand’: ‘Ford’, ‘model’: ‘Mustang’} Example The del keyword removes the item with… Read More »

14.3 Add Dictionary Items

Adding Items Adding an item to the dictionary is done by using a new index key and assigning a value to it: Example thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}thisdict[“color”] = “red”print(thisdict) Output:{‘brand’: ‘Ford’, ‘model’: ‘Mustang’, ‘year’: 1964, ‘color’: ‘red’} Update Dictionary The update() method will update the dictionary with the items from a given argument. If the item does not exist, the… Read More »

14.2 Change Dictionary Items

Change Values You can change the value of a specific item by referring to its key name: Example Change the “year” to 2018:thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}thisdict[“year”] = 2018 Output:{‘brand’: ‘Ford’, ‘model’: ‘Mustang’, ‘year’: 2018} Update Dictionary The update() method will update the dictionary with the items from the given argument. The argument must be a dictionary, or an iterable object… Read More »

14.1 Access Dictionary Items

Accessing Items You can access the items of a dictionary by referring to its key name, inside square brackets: Example Get the value of the “model” key:thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}x = thisdict[“model”] Output:Mustang There is also a method called get() that will give you the same result: Example Get the value of the “model” key:x = thisdict.get(“model”) Output:Mustang Get… Read More »

14. Dictionaries

thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964} Dictionary Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and does not allow duplicates. As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered. Dictionaries are written with curly brackets, and have keys and values: Example Create… Read More »