This document provides information about Python data types including tuples, sets, and dictionaries. It defines tuples as immutable lists that can be converted from and to lists using tuple() and list(). It describes sets as unordered collections of unique elements and set operations like union, intersection, and difference. Finally, it discusses dictionaries as unordered collections of key-value pairs that allow looking up values by key. It also covers dictionary methods for accessing items, keys, and values as well as adding, removing, and clearing items.
3. Tuples
• Tuples are similar to list but immutable
• Can Convert from list to tuple and vice versa
• tuple(list)
• list(tuple)
>>>my_tuple = ('reza', 1362, 22, 'aban')
>>>my_tuple
('reza', 1362, 22, 'aban')
>>> my_tuple[2]
22
>>> my_tuple[2] = 14
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
my_tuple[2] = 14
TypeError: 'tuple' object does not support item assignment
>>>
5. Sets
• Unordered collection of unique objects
• List to set : b = set(a)
• Set to list : a = list(b)
• Set Operations
• Union: a | b
• Intersection: a & b
• Difference: a - b
8. Dictionaries
• Unordered key-value pairs
• Keys are unique and immutable objects
• Value can change
• Check if a given key is present
• dict.has_key(key)
• key in dict
{ 'key' : 'value' }
>>>my_dic1 = {}
>>>my_dic1['name'] = 'reza'
{'name': 'reza'}
>>> my_dic2 = {'name' : 'reza', 'age' : 23}
>>> my_dic2
{'age': 23, 'name': 'reza'}
9. Dictionaries
>>> user = {'name': 'reza', 'age': 23, 'from': 'Iran'}
>>> user
{'age': 23, 'from': 'Iran', 'name': 'reza'}
>>>
>>> user.has_key('name')
True
>>> user.has_key('hobby')
False
>>> 'name' in user
True
>>> 'Iran' in user
False
10. Dictionaries
• Get tuple of items : dict.items()
• Get list of keys : dict.keys()
• Get list of values : dict.values()
• Get a particular item : dict.get(key)
15. help()
• Help on built-in function replace:
• help(name.replace)
replace(...)
S.replace(old, new[, count]) -> string
Return a copy of string S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
(END)