🔹PYTHON....... "Lists, Tuples & Dictionaries Explained — by Sam Sami"

 

💻 Python Data Structures – by sam sami.

1. 🔢 Lists (فہرستیں)

List kya hoti hai?

List ek ordered aur change hone wali (mutable) collection hoti hai. Ismein hum mukhtalif types ke data (numbers, strings, boolean) store kar sakte hain.

Khasoosiyat:

  • Ordered hoti hai (elements ka sequence matter karta hai)

  • Mutable hoti hai (baad mein update kar sakte hain)

  • Har element ka index hota hai (0 se start hota hai)

  • Duplicate values allow hoti hain

List banana:

fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True]

Access aur update:

print(fruits[0])      # Output: apple
fruits[1] = "mango"   # banana ko mango se replace
print(fruits)         # ["apple", "mango", "cherry"]

Common operations:

fruits.append("kiwi")        # akhir mein add
fruits.insert(1, "pear")     # index 1 par insert
fruits.remove("apple")       # apple ko remove
fruits.pop(0)                # first item hatao
print(len(fruits))           # length batata hai
fruits.sort()                # sort karta hai

2. 📦 Tuples (غیر قابلِ تغیر مجموعے)

Tuple kya hoti hai?


Tuple bhi list ki tarah hoti hai, lekin yeh immutable hoti hai (baad mein change nahi kar sakte).

Khasoosiyat:

  • Ordered hoti hai

  • Immutable (change nahi hoti)

  • Faster hoti hai

  • Hashable hoti hai (dictionary ke keys mein use hoti hai)

Tuple banana:

colors = ("red", "green", "blue")
coordinates = 45.2, 30.1
single_item = (42,)   # akeli value ke liye comma zaroori hai

Kab use karein?

  • Jab aapko data change nahi karna ho

  • Jaise: RGB colors, configuration values, etc.


3. 🔑 Dictionaries (لغات)

Dictionary kya hoti hai?


Yeh key-value pair ki form mein data store karti hai. Har key unique hoti hai aur uske sath ek value judi hoti hai.

Khasoosiyat:

  • Python 3.7 se ordered hoti hai

  • Mutable hoti hai (badal sakte ho)

  • Fast lookup (key se value jaldi milti hai)

Dictionary banana:

student = {
  "name": "Ali",
  "age": 21,
  "courses": ["Math", "Physics"]
}

Common operations:

print(student["name"]) 
      # key se value nikalo
student["grade"] = "A+"      # add/update value
del student["age"]           # key delete karo
print(student.keys())        # sab keys
print(student.values())      # sab values
print(student.items())       # key-value pairs

Use cases:

  • User profiles

  • Configuration files

  • JSON data storage


🧠 Summary Table

FeatureListTupleDictionary
Mutable✅ Yes❌ No✅ Yes
Ordered✅ Yes✅ Yes✅ (3.7+)
Indexed✅ Yes✅ Yes❌ No
Duplicates✅ Allowed✅ Allowed❌ Not allowed
Syntax[](){}
Use CaseDynamic collectionsFixed dataKey-value storage

Comments

Popular Posts