Professional Documents
Culture Documents
Python Kuppiya 03 - E19
Python Kuppiya 03 - E19
Python Lists
Creating a list
Like a string, a list is a sequence of values. In a string, the values are
characters; in a list, they can be any type. The values in list are called elements
or sometimes items.
Eg :-
animals = [‘cat’, ‘dog’ , ‘monkey’]
x= [10, 20, 30, 40]
Characteristics of a list
Mutable
Linear data structure
Mixed type element
Variable length
Zero based indexing
Nested list
A nested list is simply a list that occurs as an element of another list (which may
of course itself be an element of another list).
Eg :-
a = [ [10,20] ,[30,40] ,[50,60] ]
List operation
Replace
Insert
Sort
Delete
Append
Extend
Reverse
Python tuples
A tuple is a collection of objects which ordered and immutable. Tuples are
sequences, just like lists. The differences between tuples and lists are, the tuples
cannot be changed unlike lists and tuples use parentheses, whereas lists use
square brackets.
Creating a tuple
Eg :-
animals = (‘cat’, ‘dog’ , ‘monkey’)
x= (10, 20, 30, 40)
Sequence Operations of lists and tuples
Length
Count
Index
Membership
Concatenation
Min value
Max value
Sum
Python Dictionary
1. Creating a new dictionary
Python dictionary is a container of the unordered set of objects like lists. The
objects are surrounded by curly braces { }. The items in a dictionary are a
comma-separated list of key:value pairs where keys and values are Python data
type.
new_dict = { }
print (new_dict)
There is also a method called get() that will give you the same result:
color.get(‘col1’)
Color.get(‘col2’)
3. Change Values
phone = {'brand':'iphone','model':'6S','year':'2017’}
phone[‘year’] = ‘2016’
phone[‘color’] = ‘gold’
There is also a method called update() that will give you the same result:
phone.update({'space':'64GB’})
5. Find the length of a Python dictionary
len(phone)
phone.keys()
phone.values()
phone.items()
Thank You