You are on page 1of 5

Sequences

 Sequences allow us to store multiple values in an organized and efficient


manner.

 Python language provides following sequences


o list
o tuple
o set
o frozen set
o dictionary
List
 used to store group of items
 The List is Python’s compound data type.
 In Python a list contains one or more items of with in square
brackets[] ,separated by comma
 Lists are Mutable.
=>#Demonstration of List-
 >>>mylist=[10,20,20,40]
 >>>mylist=[“vaag”,”devi”,”college”]
 >>>mylist=[10,10,20,30,”vaag”,”devi”]
 >>>mylist=[10,10,20,30,’vaagdevi’,’college’]
 >>>mylist[0]
 10
 >>>mylist[2:4]
 [20,30,’vaagdevi’]
=>We can modify the contents of the list
List are mutable 30 changed to 35
 >>>mylist[3]=35
 >>>mylist
o [10,10,20,35,’vaagdevi’,’college’]
=>To add an element at the end of the list
 >>>mylist.append(40)
 >>>mylist
o [10,10,20,35,’vaagdevi’,’college’,40]
=>To insert an element at the specified position
 100 is inserted at the 5th index position
o >>>mylist.insert(5,100)
 >>>mylist
o [10,10,20,35,’vaagdevi’,100,’college’,40]
=>To reverse the elements in the list
 >>>mylist.reverse()
 >>>mylist
o [40,’college’, 100, ’vaagdevi’,35,20,10,10]
 mylist=[10,20,[100,101,102],30]
o mylist[0]=>o/p10
o mylist[1]=>o/p 20
o mylist[2][0]=>o/p100
o mylist[2][1]=>o/p101
o mylist[2][2]=>o/p102
o mylist[3]=>o/p 30

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)

=>Tuples cannot be declared with a single element unless followed by a


comma.
 >>>names = ('Jeff') # considered as string type
 >>>print(names)
 >>>print(type(names))
o output
o 'Jeff'
o <class 'string'>
 >>>names = ('Jeff',) # tuple with single element
 >>>print(names)
 >>>print(type(names))
o (Jeff) <class 'tuple'>
o
=>Each element in the tuple is accessed by the index in the square brackets [].
An index starts with zero and ends with (number of elements – 1)

 >>>names = ('Jeff', 'Bill', 'Steve', 'Yash')


o >>>print(names[0]) # prints 'Jeff'
o >>>print(names[1]) # prints 'Bill'
o >>>print(names[2]) # prints 'Steve'
o >>>print(names[3]) # prints 'Yash'

=>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’}

You might also like