Lists
List ek ordered collection hai jisme multiple values store kar sakte hain.
Lists ko [] (square brackets) se banaya jata hai.
# Example of a list
fruits = ["apple", "banana", "mango"]
print(fruits)
Output:
['apple', 'banana', 'mango']
List ke elements access karna
print(fruits[0]) # apple
print(fruits[1]) # banana
List ke elements update karna
fruits[1] = "orange"
print(fruits)
Output:
['apple', 'orange', 'mango']
List me element add karna
fruits.append("grapes")
print(fruits)
Output:
['apple', 'orange', 'mango', 'grapes']
Tuples
Tuple bhi list jaisa hota hai, lekin immutable (change nahi kar sakte).
Tuple ko () (round brackets) se banaya jata hai.
# Example of a tuple
coordinates = (10, 20, 30)
print(coordinates)
Output:
(10, 20, 30)
Tuple access karna
print(coordinates[0]) # 10
Tuple change karne ki koshish
coordinates[0] = 50 # Error aayega
Output:
TypeError: 'tuple' object does not support item assignment
Lists vs Tuples
- List → mutable (badal sakte hain)
- Tuple → immutable (fix ho jata hai)
- List use hota hai jab data change hoga
- Tuple use hota hai jab data fix hai (jaise coordinates, constants)
Next Step
Ab seekhte hain Dictionaries, jahan hum key-value pairs store karenge.