0% found this document useful (0 votes)
12 views6 pages

Python Data Structure

The document provides an overview of Python data structures, including Lists, Tuples, Dictionaries, and Sets, highlighting their characteristics, syntax, and key operations. It explains mutability, indexing, slicing, and common operations for each data structure. A summary table compares features like mutability, duplicates, access methods, and ordering across these data structures.

Uploaded by

ayushshrivas110
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)
12 views6 pages

Python Data Structure

The document provides an overview of Python data structures, including Lists, Tuples, Dictionaries, and Sets, highlighting their characteristics, syntax, and key operations. It explains mutability, indexing, slicing, and common operations for each data structure. A summary table compares features like mutability, duplicates, access methods, and ordering across these data structures.

Uploaded by

ayushshrivas110
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

🐍 Python Data Structures: –

1. Lists [] (The Flexible Sequence)


List ek ordered collection hai jo mutable (badalne layak) hoti hai.
●​ Syntax: my_list = [10, "Aman", 3.14, True]
●​ Mutability: Haan, aap elements change kar sakte hain.
●​ Indexing: 0 se shuru hoti hai.
●​ Slicing: list[start:stop:step]

🛠 Key Operations & Programs:


Python

l = [10, 20, 30, 40, 50]

# 1. Slicing (Deep Dive)


print(l[1:4]) # [20, 30, 40] (4th index excluded)
print(l[::-1]) # [50, 40, 30, 20, 10] (Reverse list)

# 2. Mutability
l[0] = 99 # Index 0 par 10 ki jagah 99 aa jayega

# 3. Methods
[Link](60) # Last mein add karega
[Link](1, 15) # Specific position par add karega
[Link]() # Last element delete karega
[Link](30) # Value 30 ko delete karega
2. Tuples () (The Protected Sequence)
Tuple list jaisa hi hai lekin ise banne ke baad badla nahi ja sakta
(Immutable).
●​ Syntax: t = (1, 2, 3)
●​ Why use? Ye List se fast hote hain aur data safe rakhte hain.
●​ Important: Agar ek element ka tuple banana hai toh comma

🛠 Key Operations:
zaroori hai: t1 = (5,).

Python

t = (10, 20, 30, 20)

# Indexing & Slicing (Same as List)


print(t[0]) # 10

# Counting & Finding


print([Link](20)) # Output: 2 (20 kitni baar aaya)
print([Link](30)) # Output: 2 (30 kis position par hai)

# Note: t[0] = 50 --> Ye ERROR dega (Immutable)


3. Dictionary {key:value}
Ye unordered hoti hai (Python 3.7+ mein order yaad rakhti hai). Isme
indexing ki jagah Keys hoti hain.
●​ Syntax: d = {"name": "Rahul", "age": 25}
●​ Keys: Hamesha unique aur immutable honi chahiye.

🛠 Key Operations:
Python

std = {"roll": 1, "name": "Amit", "marks": 85}

# Accessing
print(std["name"]) # Amit

# Updating (Mutable)
std["marks"] = 90 # Value change ho gayi
std["city"] = "Pune" # Naya pair add ho gaya

# Methods
print([Link]()) # Sari keys milengi
print([Link]()) # Sari values milengi
print([Link]()) # (key, value) pairs milenge
4. Sets {} (The Unique Collection)
Sets ka use tab hota hai jab humein sirf unique values chahiye aur order
se matlab nahi hota.
●​ Syntax: s = {1, 2, 3, 3, 4} (Duplicate 3 apne aap hat jayega)
●​ Feature: Isme Indexing kaam nahi karti (s[0] is an error).

🛠 Mathematical Operations:
Python

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

print(a | b) # Union: {1, 2, 3, 4, 5, 6}


print(a & b) # Intersection: {3, 4} (Common elements)
print(a - b) # Difference: {1, 2} (Jo sirf A mein hai)
5. Summary Table

Feature List Tuple Dictionary Set

Brackets [] () {Key:Val} {}

Mutability Mutable Immutable Mutable Mutable

Duplicates Allowed Allowed Keys No, Val Yes Not Allowed

Access Indexing Indexing Key-based No Indexing

Ordering Ordered Ordered Ordered (3.7+) Unordered

6. Slicing, Indexing aur Concatenation (Common


terms)
1.​Indexing: Position find karna. data[i]
2.​Slicing: Hissa nikalna. data[start:stop:step]
3.​Concatenation: Do sequences ko jodna. + sign ka use sirf List,
Tuple aur String mein hota hai (Dictionary aur Set mein nahi).
○​ Example: [1, 2] + [3, 4] $\rightarrow$ [1, 2, 3, 4]
4.​Repetition: * sign ka use sequence ko repeat karne ke liye.
○​ Example: ("Hi",) * 3 $\rightarrow$ ('Hi', 'Hi', 'Hi')
7. Concept of Mutability (The Final Logic)
Jab aap koi object banate hain, Python memory mein ek space deta hai.
●​ Mutable: Aap usi memory location par value overwrite kar sakte
hain.
●​ Immutable: Aap value change nahi kar sakte. Agar aapne change
karne ki koshish ki (jaise String ya Tuple mein), toh Python memory
mein ek Naya Object banayega, purana wahi rahega ya delete ho
jayega.

Pro Tip: List mutable hai isliye ye memory zyada leti hai aur slow
hoti hai. Tuple immutable hai isliye ye memory kam leta hai aur
faster hota hai.

You might also like