Python Tutorial - Tuples
π Tuples - Unchangeable Lists
π Real Life Mein:
Permanent data jo change nahi hona chahiye:
π "Coordinates (Lat, Long)" - Fixed location
π "Date of Birth (DD, MM, YYYY)" - Kabhi change nahi
π "RGB Colors (255, 0, 0)" - Fixed values
π
"Days of week" - Hamesha 7 hi rahenge
Tuple = Locked list, change nahi kar sakte!
π¦ Tuple vs List
| Feature | List | Tuple |
|---|---|---|
| Syntax | [] | () |
| Mutable? | β Yes | β No |
| Speed | Slower | Faster |
| Use Case | Dynamic data | Fixed data |
Creating Tuples
# Tuple with ()
coordinates = (28.6139, 77.2090) # Delhi
rgb_red = (255, 0, 0)
days = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
# Access same as list
print(coordinates[0]) # 28.6139
print(days[0]) # Mon
# Cannot change!
# coordinates[0] = 30 # ERROR!
πΊοΈ GPS Coordinates Example
City Coordinates
# Famous cities (lat, long)
delhi = (28.6139, 77.2090)
mumbai = (19.0760, 72.8777)
bangalore = (12.9716, 77.5946)
# Store in dictionary
cities = {
"Delhi": delhi,
"Mumbai": mumbai,
"Bangalore": bangalore
}
# Get coordinates
city = "Mumbai"
lat, long = cities[city] # Tuple unpacking
print(f"{city}: {lat}, {long}")
Example
point = (10, 20)
print(point[0])