filmov
tv
Lesson - 21 : Python3 - Python Variables : Dictionary

Показать описание
**************************************************
**************************************************
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 ('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:
**************************************************
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 ('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: