**************************************************
Python Core PlayList : • Lesson - 01 : Python3 - What is python
Python Advanced PlayList : • Lesson - 46 : Python Advanced - Python OOP...
**************************************************
Python DataType : Dictionary:
Dictionaries in Python are lists of Key:Value pairs. This is a very powerful datatype to hold a lot of related information that can be associated through keys. The main operation of a dictionary is to extract a value based on the key name. Unlike lists, where index numbers are used, dictionaries allow the use of a key to access its members. Dictionaries can also be used to sort, iterate and compare data.
Dictionaries are created by using braces ({}) with pairs separated by a comma (,) and the key values associated with a colon(:). In Dictionaries the Key must be unique. Here is a quick example on how dictionaries might be used:
room_num = {'john': 425, 'tom': 212}
room_num['john'] = 645 # set the value associated with the 'john' key to 645
print (room_num['tom']) # print the value of the 'tom' key.
room_num['isaac'] = 345 # Add a new key 'isaac' with the associated value
print (room_num.keys()) # print out a list of keys in the dictionary
print ('isaac' in room_num) # test to see if 'issac' is in the dictionary. This returns true.
Built-in Dictionary Functions :
1. cmp(dict1, dict2) : Compares elements of both dict.
2. len(dict) : Gives the total length of the dictionary. This would be 3. equal to the number of items in the dictionary.
4. str(dict) : Produces a printable string representation of a dictionary
5. type(variable) : Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type.
Built-in Dictionary Methods:
1. dict.clear() : Removes all elements of dictionary dict
2. dict.copy() : Returns a shallow copy of dictionary dict
3. dict.fromkeys() : Create a new dictionary with keys from seq and values set to value.
4. dict.get(key, default=None) : For key key, returns value or default if key not in dictionary
5. dict.has_key(key) : Returns true if key in dictionary dict, false otherwise
6. dict.items() : Returns a list of dict's (key, value) tuple pairs
7. dict.keys() : Returns list of dictionary dict's keys
8. dict.setdefault(key, default=None) : Similar to get(), but will set dict[key]=default if key is not already in dict
9. dict.update(dict2) : Adds dictionary dict2's key-values pairs to dict
10. dict.values() : Returns list of dictionary dict's values
Sample Projects : https://github.com/SadaLearningHub1/P...
On this page of the site you can watch the video online Lesson - 21 : Python3 - Python Variables : Dictionary with a duration of hours minute second in good quality, which was uploaded by the user Sada Learning Hub 18 December 2017, share the link with friends and acquaintances, this video has already been watched 110 times on youtube and it was liked by 2 viewers. Enjoy your viewing!