Vidya Vikas Mandal’s
SHREE DAMODAR COLLEGE OF COMMERCE & ECONOMICS
Affiliated to Goa University
Accredited by NAAC with Grade ‘A’
Python Programming
Unit III - Part II
• Programme: Bachelor of Vocation(Software Technologies)
• Semester: IV
Programming
• Course Code: STG401
• Course Title: Python Programming
Python
• Name of the Teacher and Designation: Mr. Sumit Kumar
(Assistant Professor)
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Programming
LIST, TUPLE AND DICTIONARY
Python
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Outline
• Lists: list operations, list slices, list methods, list loop, mutability,
aliasing, cloning lists, list parameters, Lists as arrays
• Tuples: tuple assignment, tuple as return value
Programming
• Dictionaries: operations and methods
• advanced list processing - list comprehension
Python
• Illustrative programs: selection sort, insertion sort, merge sort,
histogram
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Learning Outcomes
CO1. Explain fundamental principles, syntax and semantics of Python
Programming
programming.
CO2. Demonstrate the understanding of program flow control and handling of strings,
Python
functions, files & exception handling.
CO3. Determine the methods to create and develop Python programs by utilizing the
data structures like lists, dictionaries, tuples and sets.
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Vidya Vikas Mandal’s
SHREE DAMODAR COLLEGE OF COMMERCE & ECONOMICS
Affiliated to Goa University
Accredited by NAAC with Grade ‘A’
Dictionaries
operations and methods
29-03-2022
Dictionaries
• Dictionary is an unordered collection of elements.
• An element in dictionary has a key: value pair.
• All elements in dictionary are placed inside the curly braces i.e. { }
• Elements in Dictionaries are accessed via keys and not by their position.
Programming
• The values of a dictionary can be any data type.
• Keys must be immutable data type (numbers, strings, tuple)
Python
• Operations on dictionary:
1. Accessing an element
2. Update
3. Add element
4.Membership
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Programming
Python
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
How to create a dictionary?
• Empty dictionary: placing items inside curly braces {} separated by comma.
my_dict = {}
• Dictionary with integer keys my_dict = {1: 'apple',
Programming
2: 'ball'}
• Dictionary with mixed keys my_dict = {'name': 'John', 1: [2, 4,
3]}
Python
• Using dict() my_dict
= dict({1:'apple', 2:'ball'})
• From sequence having each item as a pair
my_dict = dict([(1,'apple'), (2,'ball')])
We can also create a dictionary using the built-in function dict().
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
How to access elements from a dictionary?
• Dictionary uses Key to access values.
• Key can be used either inside square brackets or with the get() method.
• The difference: While using get(), None is returned instead of KeyError, if
Programming
the key is not found.
my_dict = {'name':'Jack', 'age': 26}
Python
print(my_dict['name']) Output:
Jack
print(my_dict.get('age')) Output:
26
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
How to change / add elements in a dictionary?
• Dictionary are mutable: items can be added or modified
• If the key is already present, value gets updated, else a new key: value pair
is added to the dictionary.
Programming
my_dict = {'name':'Jack', 'age': 26}
update value my_dict['age'] = 27
Python
add item my_dict['address'] =
'Downtown'
Output: {'address': 'Downtown', 'age': 27, 'name': 'Jack'}
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
How to delete elements in a dictionary?
• By using pop() method - removes as item with the provided key and returns
the value.
• By using popitem() method - removes and return an arbitrary item (key,
value) form the dictionary.
Programming
• By using clear() method - removes all items at once using the clear()
• del keyword also removes individual items or the entire dictionary itself.
Python
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
How to delete elements in a dictionary?
• Create a dictionary squares = {1:1, 2:4, 3:9, 4:16,
5:25}
• Remove a particular item squares.pop(4)
• Remove an arbitrary item
Programming
squares.popitem()
• Delete a particular item del squares[5]
• Remove all items
Python
squares.clear()
• Delete the dictionary itself del squares
• Throws Error print(squares)
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Methods in
Dictionary
Programming
Python
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Programming
Python
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Programming
Python
Difference between List, Tuples and dictionary
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Programming
Python
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Vidya Vikas Mandal’s
SHREE DAMODAR COLLEGE OF COMMERCE & ECONOMICS
Affiliated to Goa University
Accredited by NAAC with Grade ‘A’
Advanced list processing
list comprehension
29-03-2022
Advanced list processing
List Comprehension:
• It provide a concise way to apply operations on a list.
• It creates a new list in which each element is the result of applying a given
Programming
operation in a list.
• It consists of brackets containing an expression followed by a “for” clause,
then a list.
Python
• The list comprehension always returns a result list.
• Syntax: list=[ expression for item in list if conditional ]
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Advanced list processing
Here is an example to make a list with each item being increasing power of 2.
pow2 = [2 ** x for x in range(10)]
print(pow2) Output: [1, 2, 4, 8, 16, 32, 64,
Programming
128, 256, 512]
Python
This code is equivalent to
pow2 = []
for x in range(10):
pow2.append(2 ** x)
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Advanced list processing
A list comprehension can optionally contain more for or if statements.
An optional if statement can filter out items for the new list.
Here are some examples.
pow2 = [2 ** x for x in range(10) if x > 5]
Programming
pow2
[64, 128, 256, 512]
Python
odd = [x for x in range(20) if x % 2 == 1]
odd
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
[x+y for x in ['Python ','C '] for y in ['Language','Programming']] ['Python
Language', 'Python Programming', 'C Language', 'C Programming']
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Programming
Python
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Advanced list processing
Nested list: List inside another list is called nested list.
Example: a=[56,34,5,[34,57]]
a[0] Output: 56
Programming
a[3] Output: [34, 57]
a[3][0] Output: 34
Python
a[3][1] Output: 57
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Vidya Vikas Mandal’s
SHREE DAMODAR COLLEGE OF COMMERCE & ECONOMICS
Affiliated to Goa University
Accredited by NAAC with Grade ‘A’
Illustrative programs
selection sort, insertion sort,
merge sort, histogram
29-03-2022
SELECTION SORT PROGRAM
A = [20, 18, 12, 14, 3]
for i in range(len(A) -1):
Programming
min = i
for j in range(i+1, len(A)):
Python
if A [min] > A [ j ] :
min = j
A[i], A [min] = A[min], A[i]
print(A)
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
BUBBLE SORT PROGRAM
A = [20, 18, 12, 14, 3]
length = len(A)
Programming
for i in range(0, length -1):
for j in range(0, length - i -1):
Python
if A [ j ] > A [ j+1] :
A[ j ], A [ j+1] = A[ j+1], A[ j ] # swap
print(A)
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
INSERTION SORT PROGRAM
A = [20, 18, 12, 14, 3]
length = len(A)
for i in range(1, length):
Programming
key = A[ i ]
j = i -1
Python
while j > = 0 and key < A [ j] :
A [ j +1] = A [ j]
j = j -1
A[ j+1] = key
print(A)
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
HISTOGRAM PROGRAM
# Method1
Items = [ 5,3,6,8]
for i in Items:
# Method2
Programming
output= “”
Items = [ 5,3,6,8]
times = i
for i in Items:
Python
while times > 0:
for n in range(0,i):
output = output
+ “*” print(“*”,
end=“ ”)
times = times-1
print()
print(A)
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Matrix Program
# Matrix Addition
a=[[1,1],[1,1]]
b=[[2,2],[2,2]]
Programming
c=[[0,0],[0,0]]
for i in range(len(a)):
Python
for j in range(len(b)):
c[i][j] = a[i][j] + b[i][j]
for i in c:
print(i)
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Matrix Program
# Matrix Multiplication
a=[[1,1],[1,1]]
b=[[2,2],[2,2]]
Programming
c=[[0,0],[0,0]]
for i in range(len(a)):
Python
for j in range(len(b)):
for k in range(len(b)):
c[i][j] = a[i][j] + a[i][k]*b[k][j]
for i in c:
print(i)
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Matrix Program
# Matrix Transpose
a=[[1,3],[1,2]]
c=[[0,0],[0,0]]
Programming
for i in range(len(a)):
for j in range(len(a)):
Python
c[i][j] = a[j][i]
for i in c:
print(i)
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Programming
End of Unit III
Thank You
Python
07/08/2025 VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in