Python Editor

Python | Lesson3 | Tuples

 

# Declaring a tuple
employee = ("David", 25, "Engineer")

# Accessing elements in a tuple
print(employee[0]) # Output: "David"
print(employee[1]) # Output: 25

# Tuple elements are immutable
# employee[0] = "JACK"  # This will raise a TypeError

# Concatenating tuples
new_employee = employee + ("London",)
print(new_employee) # Output: ("David", 25, "Engineer", "London")

# Tuple unpacking
name, age, profession, location = new_employee
print(name) # Output: "David"
print(age) # Output: 25
print(profession) # Output: "Engineer"
print(location) # Output: "London"

No comments:

Post a Comment