Dictionaries<br />Dictionaries   <br />Are similar to other compound types except that they can use  any immutable type as...
 1- create a dictionary<br />One way to create a dictionary is to start with the empty dictionary and add<br />elements. T...
Hint <br />The  key-value pairs  are not in order!<br />Fortunately, there is no reason to care<br />about the order, sinc...
2- Dictionary operations<br />* The del statement removes a key-value pair  from a dictionary.<br />
** change any value <br />*** The len function also works on dictionaries; it returns the number of key-value pairs:<br />
3-Dictionary methods<br /> >>          .keys()<br /> >>          .values()<br /> >>          .items()<br /> >>          .h...
Hint <br />If you try to call a method without specifying an object, you get an error. In<br />this case, the error messag...
Copying<br />If you want to modify a dictionary and keep a copy of the original, use the<br />copy method ,,,<br />
Copying >><br />user and copy refer to the same object; copy refers to a fresh copy of  the same dictionary. If we modify ...
ahmed<br />karim<br />user<br />copy<br />ali<br />user<br />copy<br />كل من ال                و              تؤشر على نفس...
If we modify copy, opposites is unchanged:<br />
nice program<br />
counting the number of  letter in  word ..<br />
Good luck !<br />By /salma subh<br />
Upcoming SlideShare
Loading in …5
×

Dictionaries

556 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
556
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dictionaries

  1. 1. Dictionaries<br />Dictionaries <br />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.<br />
  2. 2. 1- create a dictionary<br />One way to create a dictionary is to start with the empty dictionary and add<br />elements. The empty dictionary is denoted {}:<br />>> var={}<br />>> var[‘ key1 ’] = ‘value’<br />>> var [‘key 2’] = ‘value 2’<br />The first assignment creates a dictionary named var ; the other assignments<br />add new elements to the dictionary. We can print the current value of the<br />dictionary in the usual way:<br />>> print var<br />>> {‘var2’: ‘value 2’ , ‘var1’ : ‘value1’ }<br />
  3. 3.
  4. 4. Hint <br />The key-value pairs are not in order!<br />Fortunately, there is no reason to care<br />about the order, since the elements of a dictionary are never indexed with integer<br />indices. Instead, we use the keys to look up the corresponding values<br />
  5. 5. 2- Dictionary operations<br />* The del statement removes a key-value pair from a dictionary.<br />
  6. 6. ** change any value <br />*** The len function also works on dictionaries; it returns the number of key-value pairs:<br />
  7. 7. 3-Dictionary methods<br /> >> .keys()<br /> >> .values()<br /> >> .items()<br /> >> .has_key()<br />
  8. 8.
  9. 9. Hint <br />If you try to call a method without specifying an object, you get an error. In<br />this case, the error message is not very helpful:<br />>>> has_key('one')<br />Name Error: has_key<br />
  10. 10. Copying<br />If you want to modify a dictionary and keep a copy of the original, use the<br />copy method ,,,<br />
  11. 11. Copying >><br />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:<br />
  12. 12. ahmed<br />karim<br />user<br />copy<br />ali<br />user<br />copy<br />كل من ال و تؤشر على نفس العناصر أو عناوين العناصر في ال <br />RAM<br />
  13. 13. If we modify copy, opposites is unchanged:<br />
  14. 14. nice program<br />
  15. 15. counting the number of letter in word ..<br />
  16. 16. Good luck !<br />By /salma subh<br />

×