Python Tuples

Chapter 4 • Python
Chapter 4 • Python

Python Tuples

The Immutable Sequence. Ek aisi list jo ek baar ban gayi, toh kabhi change nahi hoti. Data ko secure rakhne ke liye best data structure!

Immutable
Fast Access
Memory Efficient
Easy Traversing
Python Tuples Blueprint

What is a Tuple?

🔒 Tuple ek ordered, immutable (jise change nahi kiya ja sakta) collection hai Python mein. Once created, its contents cannot be modified.

Simple samjho: Tuple bilkul list jaisa hota hai, par ek 'Read-only' lock ke saath. Aap naye items daal sakte hain jab banate hain, lekin banane ke baad kuch delete ya modify nahi kar sakte.

Defining the Immutable Sequence

📦 Ordered

Items index maintain karte hain (0, 1, 2...)

🔒 Immutable

Banane ke baad items KOI change nahi kar sakta

🎨 Mixed Types

Integers, strings, floats — kuch bhi allow hai

⚡ Faster

Memory mein lists se zyada fast aur light hote hain

The Data Matrix: What Can a Tuple Hold?

Tuples ko hamesha () parentheses se darshaya jaata hai aur items , comma se separate hote hain.
The Data Matrix
python
# 1. Empty Tuple
empty_tuple = ()

# 2. Tuple of Integers
int_tuple = (1, 2, 3, 4)

# 3. Tuple of Floating Point Numbers
float_tuple = (1.1, 2.2, 3.3)

# 4. Tuple of Characters
char_tuple = ('a', 'b', 'c')

# 5. Tuple of Strings
str_tuple = ("Delhi", "Baraut")

# 6. Mixed Value Tuple 
mixed_tuple = (11, "Ram", 14.2, "Ravi", 15)

Creating Tuples: Three Architectures

🛠️ Python mein tuple banane ke commonly 3 architectures hote hain — Direct Assignment, Sequence Conversion, aur Dynamic Input.
Three Architectures for Tuple Creation

1. Method 1: Direct Assignment

python
# The standard method -- directly locking values in.
my_tuple = (1, 2, 3)
num = (1, 2, 3, 4, 5, 6)

2. Method 2: From Another Sequence (Conversion)

python
# Transforming an existing list into an immutable tuple
list_a = [1, 2, 3, 4, 5]
tuple_b = tuple(list_a) 

# Acts as a casting mold.
print(tuple_b)  # (1, 2, 3, 4, 5)

3. Method 3: Dynamic Input

python
# Building on-the-fly via keyboard using eval()
dynamic_tuple = eval(input("Enter tuple elements: "))
print(dynamic_tuple)

Accessing The Vault: The Index Ruler

🔍 Tuple ke elements ko individually ya completely dekhne ke liye list ke hi methods applicable hain.
Accessing the Vault: The Index Ruler

Whole Access vs Targeted Access

python
# Whole Access
full_tuple = (3, 2, 1, 4)
print(full_tuple)  # Prints: (3, 2, 1, 4)

# Targeted Access (Individual Element Access)
first_element = full_tuple[0]  # First element (3)
third_element = full_tuple[2]  # Third element (1)
last_element = full_tuple[-1]  # Last element (4)

Structural Operations: Combine & Replicate

➕ Tuple Immutable zaroor hai, par naye tuples banana symbols operations ke through possible hai!
Combine and Replicate Tuples
python
# Concatenation (+) - Combine Tuples
t1 = (1, 2, 3)
t2 = (4, 5, 6)
combined = t1 + t2
print(combined)  # (1, 2, 3, 4, 5, 6)

# Replication (*) - Duplicate Tuples
my_tuple = (1, 2) * 3  
print(my_tuple)  # (1, 2, 1, 2, 1, 2)

# Slicing ([:]) - Structural manipulation
long_tuple = (1, 2, 3, 4, 5)
subset = long_tuple[1:4]  # index 1 to 3
print(subset)    # (2, 3, 4)
⚠️ Dhyan dein: Ye operations existing tuple ko modify nahi karte, balki hamesha **entirely new tuples** return karte hain.

The Mechanics: Tuple Functions

🔧 Functions The Tools: Built-in analytical tools jo tuple ko interrogate karte hain information return karne ke liye (count, sizes, values) bina structure ko alter kiye. Tuple ke paas kewal do specific methods hain (`count` aur `index`), uske sath kuch build-in functions bhi useful hain.
The Mechanics: Operations vs Functions

Tuple Specific Methods

count(item)Counts occurrences

Sirf yeh batata hai ki di hui value kitni baar aayi hai.

python
nums = (1, 2, 3, 2, 2, 4)
print(nums.count(2))  # Print hoga: 3
print(nums.count(5))  # Print hoga: 0
index(item)Finds first occurrence

Pehli baar `item` kis location par mila, uska index batata hai.

python
letters = ('A', 'B', 'C', 'A')
print(letters.index('A'))  # Print hoga: 0
print(letters.index('C'))  # Print hoga: 2

Built-in Python Functions applied to Tuples

len(tuple)Gets totally length

Batata hai tuple mein total kitne items hain.

python
print(len((10, 20, 30)))  # Print: 3
max(tuple)Finds largest item

Tuple mein sabse bada item ya highest value dhundta hai.

python
print(max((5, 12, 3)))  # Print: 12
print(max(('apple', 'zebra')))  # Print: zebra
min(tuple)Finds smallest item

Tuple mein sabse chota item ya lowest value dhundta hai.

python
print(min((5, 12, 3)))  # Print: 3
print(min(('apple', 'zebra')))  # Print: apple
sum(tuple)Calculates total

Sabhi numbers ka addition karta hai.

python
prices = (10, 20, 30)
print(sum(prices))  # Print: 60
sorted(tuple)Sorts but returns LIST

Dhyan dein! Ye tuple ko list banakar sort karke deta hai (original tuple change nahi hota aur na naya tuple wapas aata hai, ek **List** aati hai).

python
t = (9, 1, 4)
sort_list = sorted(t)
print(sort_list)  # Print: [1, 4, 9] (List)
tuple(sequence)Creates new tuple

Kisi bhi dusri chiz ko tuple banata hai (String, List, Sets).

python
new_tup = tuple("HELLO")
print(new_tup)  # ('H', 'E', 'L', 'L', 'O')

Practice Questions — Khud Karo! 💪

🎯 In questions ki practice karein taaki Tuples ka concept perfectly clear ho jaye!
Python Tuples Practice Questions
Hi! 👋
KnoblyAI
Online

Hello! 👋

Your futuristic AI learning companion

KnoblyAI can make mistakes. Double-check important replies.