Dictionaries
-
Upload
salma-subh -
Category
Technology
-
view
411 -
download
0
Transcript of Dictionaries
![Page 1: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/1.jpg)
Dictionaries
Dictionaries \ Are similar to other compound types except that they can use any immutable type as an index. As
an example, we will create a dictionary to translate English words into Spanish. For this dictionary, the
indices are strings.
![Page 2: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/2.jpg)
1- create a dictionary
One way to create a dictionary is to start with the empty dictionary and addelements. The empty dictionary is denoted {}:
>> var={}>> var [‘ key1 ’] = ‘value’>> var [‘key 2’] = ‘value 2’
The first assignment creates a dictionary named var ; the other assignmentsadd new elements to the dictionary. We can print the current value of thedictionary in the usual way:
>> print var>> {‘var2’: ‘value 2’ , ‘var1’ : ‘value1’ }
![Page 3: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/3.jpg)
![Page 4: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/4.jpg)
The key-value pairs are not in order! Fortunately, there is no reason to careabout the order, since the elements of a dictionary are never indexed with integerindices. Instead, we use the keys to look up the corresponding values
Hint \
![Page 5: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/5.jpg)
2- Dictionary operations
* The del statement removes a key-value pair from a dictionary.
![Page 6: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/6.jpg)
** change any value
*** The len function also works on dictionaries; it returns the number of key-value pairs:
![Page 7: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/7.jpg)
3-Dictionary methods
>> .keys() >> .values() >> .items() >> .has_key()
![Page 8: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/8.jpg)
![Page 9: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/9.jpg)
If you try to call a method without specifying an object, you get an error. Inthis case, the error message is not very helpful:>>> has_key('one')Name Error: has_key
Hint \
![Page 10: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/10.jpg)
Copyin
gIf you want to modify a dictionary and keep a copy of the original,
use thecopy method ,,,
![Page 11: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/11.jpg)
Copying >>
user and copy refer to the same object; copy refers to a fresh copy of the same dictionary. If we modify user, copy is also changed:
![Page 12: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/12.jpg)
نفس على تؤشر و ال من كلال في العناصر عناوين أو العناصر
copy
userRAM
copy user
ahmed
karim
ali
![Page 13: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/13.jpg)
If we modify copy, opposites is unchanged:
![Page 14: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/14.jpg)
nice program
![Page 15: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/15.jpg)
counting the number of letter in word ..
![Page 16: Dictionaries](https://reader036.fdocuments.in/reader036/viewer/2022062703/55524739b4c905954f8b497d/html5/thumbnails/16.jpg)
Good luck !By /salma subh