Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python
Download 1.25 Mb. Pdf ko'rish
|
Coding in Python A Comprehensive Beginners Guide to Learn the Realms
Random Dictionary Methods
Dictionaries are flexible in the sense that they allow you to do several things. For example, you can check if a certain key exists in the dictionary or not. I will use the if statement in the code. officeitem1 = {'printer' : 'Produced by HP', 'scanner': 'it is hybrid', 'laptop': 'dell', 'paper': 'A4 type', 'drafting pads': 'It is blank', 'pen': 'parker', 'table': 'made of wood', 'table lights': 'They are LED', 'office chair': 'boss'} if "scanner" in officeitem1: print("Yes, I have got 'scanner' in the office.") else: print("Sorry, I do not have that item.") = RESTART: C:/Users/saifia computers/Desktop/sample.py Yes, I have got 'scanner' in the office. >>> There is another method known as the clear() method that will empty your dictionary. See the following code sample. officeitem1 = {'printer' : 'Produced by HP', 'scanner': 'it is hybrid', 'laptop': 'dell', 'paper': 'A4 type', 'drafting pads': 'It is blank', 'pen': 'parker', 'table': 'made of wood', 'table lights': 'They are LED', 'office chair': 'boss'} print(officeitem1) officeitem1.clear() print(officeitem1) = RESTART: C:/Users/saifia computers/Desktop/sample.py {'printer': 'Produced by HP', 'scanner': 'it is hybrid', 'laptop': 'dell', 'paper': 'A4 type', 'drafting pads': 'It is blank', 'pen': 'parker', 'table': 'made of wood', 'table lights': 'They are LED', 'office chair': 'boss'} {} >>> You can see that the clear method has emptied the dictionary. Python allows you to create perfect copies of your dictionary. You can create as many copies as you want to. The method is dubbed as the copy() method. It is a built-in Python method. officeitem1 = {'printer' : 'Produced by HP', 'scanner': 'it is hybrid', 'laptop': 'dell', 'paper': 'A4 type', 'drafting pads': 'It is blank', 'pen': 'parker', 'table': 'made of wood', 'table lights': 'They are LED', 'office chair': 'boss'} print(officeitem1) officeitem2 = officeitem1.copy() print(officeitem2) = RESTART: C:/Users/saifia computers/Desktop/sample.py {'printer': 'Produced by HP', 'scanner': 'it is hybrid', 'laptop': 'dell', 'paper': 'A4 type', 'drafting pads': 'It is blank', 'pen': 'parker', 'table': 'made of wood', 'table lights': 'They are LED', 'office chair': 'boss'} {'printer': 'Produced by HP', 'scanner': 'it is hybrid', 'laptop': 'dell', 'paper': 'A4 type', 'drafting pads': 'It is blank', 'pen': 'parker', 'table': 'made of wood', 'table lights': 'They are LED', 'office chair': 'boss'} >>> There is another built-in method to create copy of the dictionary. The method is labeled as the dict() method. officeitem1 = {'printer' : 'Produced by HP', 'scanner': 'it is hybrid', 'laptop': 'dell', 'paper': 'A4 type', 'drafting pads': 'It is blank', 'pen': 'parker', 'table': 'made of wood', 'table lights': 'They are LED', 'office chair': 'boss'} print(officeitem1) officeitem2 = dict(officeitem1) print(officeitem2) = RESTART: C:/Users/saifia computers/Desktop/sample.py {'printer': 'Produced by HP', 'scanner': 'it is hybrid', 'laptop': 'dell', 'paper': 'A4 type', 'drafting pads': 'It is blank', 'pen': 'parker', 'table': 'made of wood', 'table lights': 'They are LED', 'office chair': 'boss'} {'printer': 'Produced by HP', 'scanner': 'it is hybrid', 'laptop': 'dell', 'paper': 'A4 type', 'drafting pads': 'It is blank', 'pen': 'parker', 'table': 'made of wood', 'table lights': 'They are LED', 'office chair': 'boss'} >>> We had the exact copy of the same dictionary. There is a bit of difference in writing the code. If you have to create a dictionary from scratch, you can use the dict() constructor to do that. I will take an empty dictionary and fill it in with the keys and values by using the dict() constructor. officeitem1 = dict(printer = 'Produced by HP', scanner = 'it is hybrid', laptop = 'dell', paper = 'A4 type', draftingpads = 'blank', pen = 'parker', table = 'made of wood') print(officeitem1) = RESTART: C:/Users/saifia computers/Desktop/sample.py {'printer': 'Produced by HP', 'scanner': 'it is hybrid', 'laptop': 'dell', 'paper': 'A4 type', 'draftingpads': 'blank', 'pen': 'parker', 'table': 'made of wood'} >>> The keys should not contain any spaces while you are constructing a dictionary by the dict() constructor. Please take a look at how I wrote drafting pads. If you leave any spaces between the keys, Python interpreter will return syntax error. |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling