Python Tutorial ( PDFDrive )
dict['Age']:Traceback (most recent call last):File "test.py", line 8, in <module>print "dict['Age']: ", dict['Age'];TypeError: 'type' object is unsubscriptableNote: del() method is discussed in subsequent section.Properties of Dictionary Keys: Dictionary values have no restrictions. They can be any arbitrary Python object, either standard objects or userdefinedobjects. However, same is not true for the keys.There are two important points to remember about dictionary keys:(a) More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keysencountered during assignment, the last assignment wins. Following is a simple example:#!/usr/bin/pythondict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'};print "dict['Name']: ", dict['Name'];When the above code is executed, it produces following result:dict['Name']: Manni(b) Keys must be immutable. Which means you can use strings, numbers or tuples as dictionary keys butsomething like ['key'] is not allowed. Following is a simple example:#!/usr/bin/pythondict = {['Name']: 'Zara', 'Age': 7};print "dict['Name']: ", dict['Name'];When the above code is executed, it produces the following result:Traceback (most recent call last):File "test.py", line 3, in <module>dict = {['Name']: 'Zara', 'Age': 7};TypeError: list objects are unhashableBuilt-‐in Dictionary Functions & Methods: Python includes the following dictionary functions:SN12Function with Descriptioncmp(dict1, dict2)Compares elements of both dict.len(dict)Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.TUTORIALS POINT Simply Easy Learning
34str(dict)Produces a printable string representation of a dictionarytype(variable)Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionarytype.The dictionary functions are explained below individually:cmp(dict1, dict2) Description The method cmp() compares two dictionaries based on key and values.Syntax Following is the syntax for cmp() method:cmp(dict1, dict2)Parameters • dict1 -- This is the first dictionary to be compared with dict2.• dict2 -- This is the second dictionary to be compared with dict1.Return Value This method returns 0 if both dictionaries are equal, -1 if dict1 < dict2 and 1 if dict1 > dic2.Example The following example shows the usage of cmp() method.#!/usr/bin/pythondict1 = {'Name': 'Zara', 'Age': 7};dict2 = {'Name': 'Mahnaz', 'Age': 27};dict3 = {'Name': 'Abid', 'Age': 27};dict4 = {'Name': 'Zara', 'Age': 7};print "Return Value : %d" % cmp (dict1, dict2)print "Return Value : %d" % cmp (dict2, dict3)print "Return Value : %d" % cmp (dict1, dict4)Let us compile and run the above program, this will produce the following result:Return Value : -1Return Value : 1Return Value : 0len(dict) Description The method len() gives the total length of the dictionary. This would be equal to the number of items in thedictionary.TUTORIALS POINT Simply Easy Learning
- Page 129 and 130: str = "this is string example....wo
- Page 131 and 132: Return Value This method returns a
- Page 133 and 134: Description The method expandtabs()
- Page 135 and 136: Traceback (most recent call last):F
- Page 137 and 138: Let us compile and run the above pr
- Page 139 and 140: str = "This is string example....wo
- Page 141 and 142: str = "-";seq = ("a", "b", "c"); #
- Page 143 and 144: lstrip() Description The method lst
- Page 145 and 146: Max character: xmin(str) Descriptio
- Page 147 and 148: str = "this is really a string exam
- Page 149 and 150: Parameters • chars -- You can sup
- Page 151 and 152: Parameters• str -- This is the st
- Page 153 and 154: Parameters • NAReturn Value This
- Page 155 and 156: Example The following example shows
- Page 157 and 158: CHAPTER10Python ListsThe most basic
- Page 159 and 160: Python Expression Results Descripti
- Page 161 and 162: Syntax Following is the syntax for
- Page 163 and 164: list( seq )Parameters • seq -- Th
- Page 165 and 166: Return Value If elements are of th
- Page 167 and 168: list.insert(index, obj)Parameters
- Page 169 and 170: Syntax Following is the syntax for
- Page 171 and 172: CHAPTER11Python TuplesAtuple is a s
- Page 173 and 174: len((1, 2, 3)) 3 Length(1, 2, 3) +
- Page 175 and 176: -11-1len(tuple) Description The met
- Page 177 and 178: min value element : 200tuple(seq) D
- Page 179: #!/usr/bin/pythondict = {'Name': 'Z
- Page 183 and 184: type(variable) Description The meth
- Page 185 and 186: Syntax Following is the syntax for
- Page 187 and 188: Syntax Following is the syntax for
- Page 189 and 190: Return Value This method does not
- Page 191 and 192: CHAPTER13Python Date & TimeApython
- Page 193 and 194: #!/usr/bin/pythonimport time;localt
- Page 195 and 196: import timeprint "time.altzone %d "
- Page 197 and 198: Parameters • sec -- These are the
- Page 199 and 200: Example The following example shows
- Page 201 and 202: • %w - day of the week as a decim
- Page 203 and 204: time.time( ) Description The metho
- Page 205 and 206: 2time.tznameAttribute time.tzname i
- Page 207 and 208: CHAPTER14Python FunctionAfunction i
- Page 209 and 210: #!/usr/bin/python# Function definit
- Page 211 and 212: return;# Now you can call printinfo
- Page 213 and 214: Inside the function : 30Outside the
- Page 215 and 216: A module is loaded only once, regar
- Page 217 and 218: 'sqrt', 'tan', 'tanh']Here, the spe
- Page 219 and 220: CHAPTER16Python Files I/OThis chapt
- Page 221 and 222: rb+WWbw+wb+AAba+ab+Opens a file for
- Page 223 and 224: fo.write( "Python is a great langua
- Page 225 and 226: EXAMPLE: Following is the example t
- Page 227 and 228: operations from the operating syste
- Page 229 and 230: # Close opened filefo.close()Let us
3
4
str(dict)
Produces a printable string representation of a dictionary
type(variable)
Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary
type.
The dictionary functions are explained below individually:
cmp(dict1, dict2)
Description
The method cmp() compares two dictionaries based on key and values.
Syntax
Following is the syntax for cmp() method:
cmp(dict1, dict2)
Parameters
• dict1 -- This is the first dictionary to be compared with dict2.
• dict2 -- This is the second dictionary to be compared with dict1.
Return Value
This method returns 0 if both dictionaries are equal, -1 if dict1 < dict2 and 1 if dict1 > dic2.
Example
The following example shows the usage of cmp() method.
#!/usr/bin/python
dict1 = {'Name': 'Zara', 'Age': 7};
dict2 = {'Name': 'Mahnaz', 'Age': 27};
dict3 = {'Name': 'Abid', 'Age': 27};
dict4 = {'Name': 'Zara', 'Age': 7};
print "Return Value : %d" % cmp (dict1, dict2)
print "Return Value : %d" % cmp (dict2, dict3)
print "Return Value : %d" % cmp (dict1, dict4)
Let us compile and run the above program, this will produce the following result:
Return Value : -1
Return Value : 1
Return Value : 0
len(dict)
Description
The method len() gives the total length of the dictionary. This would be equal to the number of items in the
dictionary.
TUTORIALS POINT
Simply Easy Learning