-
Hajipur, Bihar, 844101
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets ()
.
fruits = ("apple", "banana", "cherry")
print(fruits)
Tuples keep the order in which items are added.
print(fruits[1]) # banana
print(fruits[-1]) # cherry
print(len(fruits)) # 3
Add a comma ,
to create a tuple with one item.
single = ("apple",)
print(type(single)) # <class 'tuple'>
mixed = ("apple", 5, True)
t = tuple(("a", "b", "c"))
for item in fruits:
print(item)
if "banana" in fruits:
print("Yes")
Tuples are unchangeable. Convert to list → change → convert back.
x = ("apple", "banana", "cherry")
temp = list(x)
temp[1] = "orange"
x = tuple(temp)
print(x)
a = (1, 2)
b = (3, 4)
c = a + b
Create and access tuples, check membership, and combine tuples.
Q1. Write a Python program to create a tuple of 5 programming languages and print it.
Q2. Write a Python program to print the second item of a tuple.
Q3. Write a Python program to print the last item of a tuple using negative indexing.
Q4. Write a Python program to check if "Java"
exists in a tuple using the in
keyword.
Q5. Write a Python program to convert a tuple to a list, change a value, and print the updated list.
Q6. Write a Python program to create a tuple with only one item and print its type.
Q7. Write a Python program to join two tuples together and print the result.
Q8. Write a Python program to loop through a tuple using a for
loop and print each item.
Q9. Write a Python program to find the length of a tuple using the len()
function.
Q10. Write a Python program to create a tuple with mixed data types (e.g., int, string, float) and print it.
Q1: What symbol is used to define a tuple?
Q2: What is the key difference between a list and a tuple?
Q3: How do you create a single-item tuple?
Q4: What will len(("a", "b", "c")) return?
Q5: Can tuples have duplicate items?
Q6: Which function is used to create a tuple?
Q7: What will ("a", "b") + ("c",) return?
Q8: Can tuples contain different data types?
Q9: How do you loop through a tuple?
Q10: What method converts a tuple to a list?