Python Module 2 (2 Part 2) PPT
Python Module 2 (2 Part 2) PPT
1
Course Outcomes
2
Mutable & Immutable Data Types in Python
list ✅ Yes ❌ No
tuple ❌ No ✅ Yes
dict ✅ Yes ❌ No
set ✅ Yes ❌ No
str ❌ No ✅ Yes
3
List Operations
4
List Slicing
5
List Methods
6
Looping Through a List
7
Tuples
• A tuple is an immutable sequence of
elements.
• Once created, the items in a tuple cannot be
changed (no add, remove, or update).
8
Tuple Assignment
• assign multiple variables from a tuple
9
• return tuples from functions:
10
Indexing & Slicing
11
Tuple Methods
Method Description
Returns the number of times x appears
.count(x)
12
Dictionaries in Python
• A dictionary is a mutable collection of key-
value pairs.
13
Dictionary Operations
14
Iterating Through a Dictionary
15
Common Dictionary Methods
Method Description
16
17
Sets in Python
• A set is an unordered, mutable, collection
of unique elements
18
Operations on Sets
19
Frozen Set
• A frozenset is an immutable version of a
set.
20
Common Set Methods
Method Description
.add(x) Adds element x
.remove(x) Removes x, raises error if not found
.discard(x) Removes x if present, no error if absent
.pop() Removes and returns a random element
.clear() Removes all elements
.update(other_set) Adds elements from another set
21
22
References
• Textbooks:
• T1. Allen B. Downey, “Think Python: How to Think like a Computer Scientist”,
2nd Edition, O’Reilly Publishers, 2016.
• T2. Karl Beecher, “Computational Thinking: A Beginner’s Guide to Problem-
Solving and programming”, 1st Edition, BCS Learning & Development Limited,
2017.
• Reference Book(s):
• R1. Paul Deitel and Harvey Deitel, “Python for Programmers”, Pearson
Education, 1st Edition, 2021.
• R2. G Venkatesh and Madhavan Mukund, “Computational Thinking: A Primer
for Programmers and Data Scientists”, 1st Edition, Notion Press, 2021.
• R3. Eric Matthes, “Python Crash Course, A Hands – on Project Based
Introduction to Programming”, 2nd Edition, No Starch Press, 2019
23
Thank you!!
24