Tuesday, 14 April 2026

April Python Bootcamp Day 9

 


Day 9: Mastering Tuples and Sets in Python

In this session, we explore two important Python data structures:

  • Tuples: used for fixed, ordered data
  • Sets: used for unique, unordered data

Understanding these will help you write more efficient and structured programs.


What is a Tuple?

A tuple is an ordered, immutable collection of elements.

t = (10, 20, 30)
print(t)

Key Characteristics:

  • Ordered (supports indexing)
  • Immutable (cannot be modified after creation)
  • Allows duplicate values

Tuple vs List

FeatureTupleList
MutabilityImmutableMutable
Syntax()[]
PerformanceFasterSlightly slower
Use CaseFixed dataDynamic data
# List (mutable)
lst = [1, 2, 3]
lst[0] = 100 # allowed

# Tuple (immutable)
tup = (1, 2, 3)
# tup[0] = 100 # Error

Why Use Tuples?

Tuples are preferred when:

  • Data should not change
  • Performance is important
  • Data integrity must be maintained
coordinates = (23.5, 77.2)
print(coordinates)

Accessing Tuple Elements

t = (10, 20, 30, 40)

print(t[0]) # 10
print(t[-1]) # 40

Tuple Packing and Unpacking

Packing

t = 10, 20, 30

Unpacking

a, b, c = (10, 20, 30)
print(a, b, c)

Extended Unpacking

a, *b = (1, 2, 3, 4, 5)
print(a) # 1
print(b) # [2, 3, 4, 5]

Functions Return Tuples Automatically

def get_values():
return 1, 2, 3

result = get_values()
print(result)

a, b, c = get_values()
print(a, b, c)

Tuple Methods

t = (1, 2, 2, 3)

print(t.count(2))
print(t.index(3))

Tuple Use Cases

  • Returning multiple values from functions
  • Storing fixed configurations
  • Using tuples as dictionary keys
data = {
(1, 2): "Point A",
(3, 4): "Point B"
}

What is a Set?

A set is an unordered collection of unique elements.

s = {1, 2, 3}
print(s)

Key Characteristics:

  • Unordered
  • No duplicate elements
  • Mutable

Why Sets Are Useful?

  • Remove duplicates
  • Fast membership checking
  • Perform mathematical operations
nums = [1, 2, 2, 3, 4]
unique = set(nums)

print(unique)

Creating Sets

s1 = {1, 2, 3}
s2 = set([4, 5, 6])

Adding and Removing Elements

s = {1, 2, 3}

s.add(4)
s.remove(2)
s.discard(10) # no error if element not present

Set Operations

a = {1, 2, 3}
b = {3, 4, 5}

print(a | b) # Union
print(a & b) # Intersection
print(a - b) # Difference
print(a ^ b) # Symmetric Difference

Membership Testing

s = {1, 2, 3}

print(2 in s)

Iterating Over a Set

for i in {10, 20, 30}:
print(i)

Set Methods

s = {1, 2}

s.update([3, 4])
s.clear()

Set Use Cases

  • Removing duplicates
  • Finding common elements
  • Filtering datasets
  • Fast lookups
a = [1, 2, 3]
b = [2, 3, 4]

print(set(a) & set(b))

Tuple vs Set Summary

FeatureTupleSet
OrderOrderedUnordered
MutabilityImmutableMutable
DuplicatesAllowedNot allowed
Use CaseFixed dataUnique elements

Practice Questions

Basic

  1. Create a tuple and print its first and last element
  2. Convert a list into a tuple
  3. Create a set and print all its elements
  4. Add an element to a set
  5. Remove duplicates from a list using a set

Intermediate

  1. Unpack a tuple into three variables
  2. Count occurrences of an element in a tuple
  3. Find common elements between two lists using sets
  4. Check if an element exists in a set
  5. Perform union and intersection on two sets

Advanced

  1. Swap two variables using tuple unpacking
  2. Combine two lists and extract only unique elements
  3. Find elements present in one set but not in another
  4. Use a tuple as a dictionary key and retrieve its value
  5. Remove duplicate words from a sentence using a set

Final Takeaways

  • Tuples are best suited for fixed and unchangeable data
  • Sets are ideal for handling unique elements and performing fast operations
  • Choosing the right data structure improves both performance and code clarity

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (243) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (10) BI (10) Books (262) Bootcamp (4) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (30) Data Analytics (22) data management (15) Data Science (342) Data Strucures (16) Deep Learning (149) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (68) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (282) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) pytho (1) Python (1297) Python Coding Challenge (1128) Python Mistakes (51) Python Quiz (471) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (49) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)