Professional Documents
Culture Documents
Datatypes2 Sequences
Datatypes2 Sequences
Tuples
Tuple is an immutable (unchangeable) collection of elements of different
data types. It is an ordered collection, so it preserves the order of elements in
which they were defined.
=>Tuples are defined by enclosing elements in parentheses (), separated by
a comma.
The following declares a tuple type variable.
>>>tpl=() # empty tuple
o >>>print(tpl)
o ()
>>>names = ('Jeff', 'Bill', 'Steve', 'Yash') # string tuple
o >>> print(names)
o ('Jeff', 'Bill', 'Steve', 'Yash')
>>>nums = (1, 2, 3, 4, 5) # int tuple
>>> print(nums)
o (1, 2, 3, 4, 5)
>>>employee=(1, ‘Steve', True, 25, 12000)
# heterogeneous data tuple
>>>print(employee)
o (1, 'Steve', True, 25, 12000)
=>Tuple with out parentheses
We can also define a tuple
The tuple object can include elements separated by a comma without
parentheses.
>>>nums = 1, 2, 3, 4, 5 # int tuple
>>>print(nums)
o (1, 2, 3, 4, 5)
=>The tuple supports negative indexing also, the same as list type. The
negative index for the first element starts from -number of elements and ends
with -1
>>>names = ('Jeff', 'Bill', 'Steve', 'Yash')
o >>>print(names[-4]) # prints 'Jeff'
o >>>print(names[-3]) # prints 'Bill'
o >>>print(names[-2]) # prints 'Steve'
o >>>print(names[-1]) # prints 'Yash
=>Tuple is unchangeable
>>> names = ('Jeff', 'Bill', 'Steve', 'Yash')
>>> names[0] = 'Swati'
Traceback (most recent call last): File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
=>However, you can delete an entire tuple using the del keyword.
>>> del names
=>Set
A set is a mutable collection of distinct hashable objects, same as
the list and tuple.
It is an unordered collection of objects, meaning it does not record element
position or order of insertion and so cannot access elements using indexes.
A set object contains one or more items, not necessarily of the same
type, which are separated by a comma and enclosed in curly brackets {}
myset={10,20,3040,40,30,’vaagdevi’,’course’}
myset
{10,20,30,40,’course’,’vaagdevi’}
=>Frozen set
Frozen set is just an immutable version of a Python set object.
While elements of a set can be modified at any time, elements of the frozen
set remain the same after creation.
Due to this, frozen sets can be used as keys in Dictionary or as elements of
another set
=>Dictionary
Dictionaries are unordered collection of elements in curly braces in the form
of a key:value pairs that associate keys to values.
Dictionaries are Mutable.
As dictionary elements does not have index value ,
the elements are accessed through the keys defined in key:value pairs.
#Demonstration of Dictionary- Program to save Phone nos. in dictionary &
print it
Eg:1
>>>Phonedict={“Madhav”:9876567843,”Dipak”:7650983457,”Murugan
”:90672 08769,”Abhinav”:9870987067}
>>>print(Phonedict)
o Output:
o {'Madhav': 9876567843, 'Dipak': 7650983457, 'Murugan':
9067208769, Abhinav': 9870987067}
Eg:2
>>>Courses={1:’python,2:’datascience’,’third’:’machinelearning’}
>>>Courses[‘third’]
o ‘machinelearning’
>>>Courses.get(‘third’)
o ‘machinelearning’
>>>‘courses[‘third’]=‘Hadoop’
>>>Courses
o {1:’python,2:’datascience’,’third’:’Hadoop’}
>>>Courses[‘four’]=‘machinelearing’
>>>Courses
o {1:’python,2:’datascience’,’third’:’Hadoop’, ‘four’:‘machinelearing’}