0% found this document useful (0 votes)
2 views4 pages

Module 3 Exam Notes

Module 3 covers tuples, sets, and dictionaries in Python, detailing their definitions, core methods, and key differences. It provides examples of operations and built-in functions for each data structure, along with practical programming applications. Additionally, it includes a question bank checklist for exam preparation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Module 3 Exam Notes

Module 3 covers tuples, sets, and dictionaries in Python, detailing their definitions, core methods, and key differences. It provides examples of operations and built-in functions for each data structure, along with practical programming applications. Additionally, it includes a question bank checklist for exam preparation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MODULE 3 — EXAM NOTES

Tuples • Sets • Dictionaries


Based on the uploaded SNPSU Module 3 material and Module 3 Question Bank. The question bank covers tuple
built-ins/methods, set methods/operations, and dictionary methods/programs. ■filecite■turn17file0■L11-L38■

1. QUICK COMPARISON
Feature Tuple Set Dictionary

Example (1, 2, 3) {1, 2, 3} {'a': 1}

Main idea Ordered collection Unique elements Key → value

Mutable? No Yes Yes

Duplicates Allowed Not allowed Keys unique

Indexing Yes No By key

Core methods count(), index() add(), remove(), discard(), pop(), get(), keys(), values(), items(), setdefault(),
clear() update(), pop()

2. TUPLES
Definition: A tuple is an ordered, immutable sequence. Items can be accessed by indexing, but cannot be added, removed or
replaced. The module explicitly contrasts immutable tuples with mutable lists. ■filecite■turn16file2■L164-L169■

Creation and indexing


t = (10, 20, 30)
single = (5,) # comma is essential
t[0] # 10
t[-1] # 30
t[1:3] # (20, 30)

Tuple operations
t1 + t2 # concatenation
t1 * 3 # repetition
20 in t # membership
20 not in t # membership

Built-in functions
Function Use Example

len() Number of items len((1,2,3)) → 3

max() Largest value max((2,5,1)) → 5

min() Smallest value min((2,5,1)) → 1

sum() Sum of numeric items sum((2,5,1)) → 8

sorted() Sorted copy; returns a list sorted((3,1,2)) → [1,2,3]

tuple() Creates a tuple from a sequence tuple('abc') → ('a','b','c')

Important: sorted() does not modify the original tuple; it returns a sorted list. ■filecite■turn16file2■L138-L163■

Tuple methods
t = (10, 20, 20, 30)
[Link](20) # 2
[Link](30) # 3

Packing and unpacking


t = 10, 20, 30 # packing
a, b, c = t # unpacking
3. SETS
Definition: A set stores unique elements. It is mutable, but does not support indexing/slicing.
■filecite■turn17file4■L229-L260■
s = {1, 2, 3, 3, 4}
# duplicates disappear
# {1, 2, 3, 4}

empty = set() # NOT {}

Core methods
Method Meaning Important point

add(x) Adds x Adds one item

remove(x) Removes x KeyError if absent

discard(x) Removes x if present No error if absent

pop() Removes and returns an arbitrary item KeyError if empty

clear() Removes all items Result: set()

update(B) Adds items from B Changes the original set

Set operations
Operation Method Operator Meaning

Union [Link](B) A|B Everything from both

Intersection [Link](B) A&B Common elements

Difference [Link](B) A-B In A, not B

Symmetric difference A.symmetric_difference(B) A^B In either, but not both


A = {1, 2, 3}
B = {3, 4, 5}

[Link](B) # {1,2,3,4,5}
[Link](B) # {3}
[Link](B) # {1,2}
A.symmetric_difference(B) # {1,2,4,5}

Must remember: remove() is strict; discard() is safe. Set pop() removes an arbitrary item, not a defined last item.
■filecite■turn16file4■L253-L270■

4. DICTIONARIES
Definition: A dictionary is a collection of key-value pairs. Keys are unique and values are accessed by key.
■filecite■turn17file1■L49-L64■
student = {'name': 'Anjali', 'age': 20}
student['name'] # 'Anjali'

Important methods
Method Purpose Example

keys() All keys [Link]()

values() All values [Link]()

items() Key-value pairs [Link]()

get() Safely gets a value [Link]('age', 0)

setdefault() Adds key if absent [Link]('age', 20)

update() Adds/updates pairs [Link]({'city':'Delhi'})

pop() Removes key and returns value [Link]('age')

get() vs direct access: d['x'] raises KeyError if x is missing; [Link]('x', default) safely returns the default. The module notes
get() as safe access and setdefault() as adding a key only when it is not present. ■filecite■turn17file1■L81-L91■

items() in a loop
for k, v in [Link]():
print(k, v)
The module explains that items() provides key-value pairs that can be unpacked into two loop variables.
■filecite■turn17file2■L124-L143■
5. HIGH-VALUE PROGRAMS
Tuple frequency using count()
t = (1, 2, 2, 3, 3, 3)
for x in set(t):
print(x, [Link](x))

Maximum and minimum in tuple


t = (10, 5, 20, 2)
print('Maximum:', max(t))
print('Minimum:', min(t))

Union and intersection


A = {1, 2, 3}
B = {3, 4, 5}
print([Link](B))
print([Link](B))

Remove duplicates from a list


a = [1, 2, 2, 3, 3, 4]
a = list(set(a))
print(a)

Dictionary using get() and update()


d = {'name': 'Anjali'}
print([Link]('age', 0))
[Link]({'age': 20})
print(d)

Dictionary frequency
text = 'banana'
freq = {}
for ch in text:
freq[ch] = [Link](ch, 0) + 1
print(freq)

6. EXAM-READY DIFFERENCES
Concept Remember

List vs tuple List is mutable; tuple is immutable.

remove() vs discard() remove() raises KeyError if absent; discard() does not.

Set pop() vs list pop() Set pop() removes an arbitrary item; list pop() uses an index.

get() vs d[key] get() safely handles a missing key; d[key] raises KeyError.

union vs intersection Union = everything; intersection = common.

sorted(tuple) Returns a sorted list; original tuple is unchanged.

keys / values / items Keys = keys; values = values; items = key-value pairs.

7. QUESTION BANK CHECKLIST


• Tuple built-ins: len(), max(), min(), sum(), sorted(), tuple()

• Tuple methods: count(), index()

• Tuple membership: in, not in

• Set methods: add(), remove(), discard(), pop(), clear(), update()

• Set operations: union(), intersection(), difference(), symmetric_difference()

• Dictionary: keys(), values(), items(), get(), setdefault(), update(), pop()

• Programs: tuple frequency, max/min tuple, set union/intersection, duplicate removal, dictionary frequency

You might also like