Python for Kids
Lecture-7
List, Tuple & Dictionary
Topics
• List, Examples, Functions
• Tuple, Examples, Functions
• Dictionary, Examples, Functions
What is List ?
Definition:
- A List is a collection of elements.
- Elements may different types.
- List is Mutable type.
- Uses square Brackets [ ]
Example:
List1 = ['physics', 'chemistry', 1997, 2000]
List2 = [1, 2, 3, 4, 5 ]
List3 = ["a", "b", "c", "d"]
Example Program of List
List1 = ['physics', 'chemistry', 1997, 2000]
print(List1)
['physics', 'chemistry', 1997, 2000]
days = [‘SUN’, ’MON’, ‘TUE’, ‘WED’]
print(days)
[‘SUN’, ’MON’, ‘TUE’, ‘WED’]
Indexing of List
days = [‘SUN’, ’MON’, ‘TUE’, ‘WED’]
-4 -3 -2 -1
SUN MON TUE WED
0 1 2 3
Index Example of List
days = [‘SUN’, ’MON’, ‘TUE’, ‘WED’]
print(days[0]) print(days[-4]) print(days[1])
SUN SUN MON
print(days[3]) print(days[-1]) print(days[-3])
WED WED MON
Slicing Example of List
days = [‘SUN’, ’MON’, ‘TUE’, ‘WED’]
print(days[0:]) print(days[-1::-1])
SUN,MON,TUE, WED WED, TUE, MON, SUN
print(days[0::2]) print(days[0:2])
SUN, TUE SUN,MON
Looping Example of List
days = [‘SUN’, ’MON’, ‘TUE’, ‘WED’]
for item in days:
print(item)
days = [‘SUN’, ’MON’, ‘TUE’, ‘WED’]
SUN
MON
for i in range(4):
TUE print(days[i])
WED
Functions of List
len(list): Gives the total length of the list.
max(list): Returns item from the list with max value.
min(list): Returns item from the list with min value.
[Link](item): Adds item at end of the list
[Link](item): counts occurrence of item in the list
[Link](seq): Appends the contents of seq to list
Functions of List
[Link](item): first index in list that item appears
[Link](pos, item): Inserts item into list at pos
[Link](): Removes and returns last item
[Link](pos): Removes and returns positional item
[Link](item): Removes item from list
[Link](): Reverses items of list in place
[Link](): Sorts items of list in ascending order
What is Tuple ?
Definition:
- A tuple is a collection of elements like List.
- Elements may different types.
- Tuple is immutable type.
- Uses round Brackets ()
Example:
tp1 = ('physics', 'chemistry', 1997, 2000)
tp2 = (1, 2, 3, 4, 5 )
tp3 = ("a", "b", "c", "d“)
Example Program of Tuple
tp1 = ('physics', 'chemistry', 1997, 2000)
print(tp1)
('physics', 'chemistry', 1997, 2000)
days = (‘SUN’, ’MON’, ‘TUE’, ‘WED’)
print(days)
(‘SUN’, ’MON’, ‘TUE’, ‘WED’)
Indexing of Tuple
days = (‘SUN’, ’MON’, ‘TUE’, ‘WED’)
-4 -3 -2 -1
SUN MON TUE WED
0 1 2 3
Index Example of Tuple
days = (‘SUN’, ’MON’, ‘TUE’, ‘WED’)
print(days[0]) print(days[-4]) print(days[1])
SUN SUN MON
print(days[3]) print(days[-1]) print(days[-3])
WED WED MON
Slicing Example of Tuple
days = (‘SUN’, ’MON’, ‘TUE’, ‘WED’)
print(days[0:]) print(days[-1::-1])
SUN,MON,TUE, WED WED, TUE, MON, SUN
print(days[0::2]) print(days[0:2])
SUN, TUE SUN,MON
Difference with List
days = (‘SUN’, ’MON’, ‘TUE’, ‘WED’)
days[0] = ‘SAT’
# Not Valid in tuple
print(days)
Error
Looping Example of Tuple
days = (‘SUN’, ’MON’, ‘TUE’, ‘WED’)
for item in days:
print(item)
days = (‘SUN’, ’MON’, ‘TUE’, ‘WED’)
SUN
MON
for i in range(4):
TUE print(days[i])
WED
Updating Tuple
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
# So let's create a new tuple as follows
tup3 = tup1 + tup2
print (tup3)
12, 34.56, abc, xyz
Functions of Tuple
len(tuple): Gives the total length of the tuple.
max(tuple): Returns max item from the tuple.
min(tuple): Returns item from the tuple with min value.
[Link](item): counts occurrence of item.
[Link](item): first index in tuple that item appears
What is Dictionary ?
Definition:
- A dictionary is a collection of key and values.
- Key and values are separated by colon ( : ).
- dictionary is Mutable type.
- Uses curly Brackets { }
- Keys must be unique while values may not be.
Example:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First’}
days= {1: ‘SUN’, 2: ‘MON’, 3: ‘TUE’, 4: ‘WED’}
Example of Dictionary
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print ( dict['Name'])
Zara
print ( dict['Age'])
7
days= {1: ‘SUN’, 2: ‘MON’, 3: ‘TUE’, 4: ‘WED’}
print(days[1]) SUN
Print(days[4]) WED
Updating Dictionary
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School" # Add new entry
print ("Age: ", dict['Age'])
print ("'School:", dict['School']) Age : 8
School : DPS School
Delete Dictionary items
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
del dict['Name’] # remove entry with key 'Name'
[Link]() # remove all entries in dict
del dict # delete entire dictionary
print (‘Age: ', dict['Age'])
print (‘School:’, dict['School']) Error
Functions of Dictionary
[Link](): Removes all elements of dictionary dict.
[Link](): Returns a copy of dictionary dict.
[Link](): Create a new dictionary with keys from
seq and values set to value.
[Link](key, default=None): For key key, returns value
or default if key not in dictionary.
Functions of Dictionary
[Link](): Returns a list of dict's (key, value) tuple
pairs.
[Link](): Returns list of dictionary dict's keys.
[Link](key, default=None): Similar to get(), but
will set dict[key]=default if key is not already in dict.
[Link](dict2): Adds dictionary dict2's key-values
pairs to dict.
[Link](): Returns list of dictionary dict's values.
Assignment-7
1. Write a program that accepts a list from user and print the alternate
element of list.
2. Write a program that accepts a list of numbers, and reverse the content
of list and display it. Do not use reverse() method.
3. Input a list of 10 numbers, Find and display the largest number of a list
without using built-in function max().
4. Write a program that keeps student's name and his marks in a
dictionary as key-value pairs and display students name and marks.
5. Write a program that keeps name and birthday in a dictionary as key-
value pairs. The program should ask the name of person then search
and display person’s birthday.
Solution-7
Solution-7
Solution-7
Solution-7