Tuples are ordered and immutable collections in Python. They are used to store multiple values that should not be modified after creation. In this Python Tuples practice questions with solutions set, you’ll learn tuple creation, indexing, slicing, packing, unpacking, and commonly used tuple functions through beginner-friendly questions with complete solutions.
1. Python Program to Create and Print a Tuple
Problem Statement
Write a Python program to create a tuple of five programming languages and print it.
Python Solution
languages = ("Python", "Java", "C++", "JavaScript", "SQL")
print(languages)
Sample Output
('Python', 'Java', 'C++', 'JavaScript', 'SQL')
Explanation
A tuple stores multiple values inside parentheses ().
Concepts Covered
- Tuple Creation
- Tuple Syntax
2. Python Program to Access Tuple Elements
Problem Statement
Write a Python program to print the first, second, and last element of a tuple.
Python Solution
fruits = ("Apple", "Banana", "Orange", "Mango")
print("First:", fruits[0])
print("Second:", fruits[1])
print("Last:", fruits[-1])
Sample Output
First: Apple
Second: Banana
Last: Mango
Explanation
Tuple elements are accessed using positive and negative indexing.
Concepts Covered
- Tuple Indexing
- Negative Indexing
3. Python Program to Find the Length of a Tuple
Problem Statement
Write a Python program to find the total number of elements in a tuple.
Python Solution
numbers = (10, 20, 30, 40, 50)
print("Length:", len(numbers))
Sample Output
Length: 5
Explanation
The len() function returns the number of items in the tuple.
Concepts Covered
- len()
4. Python Program to Check Whether an Item Exists in a Tuple
Problem Statement
Write a Python program to check whether "Python" exists in a tuple.
Python Solution
courses = ("Python", "Java", "C++")
if "Python" in courses:
print("Course Found")
else:
print("Course Not Found")
Sample Output
Course Found
Explanation
The in operator checks whether an item exists in the tuple.
Concepts Covered
- Membership Operator
5. Python Program to Count the Occurrences of an Element
Problem Statement
Write a Python program to count how many times 20 appears in a tuple.
Python Solution
numbers = (10, 20, 30, 20, 40, 20)
print(numbers.count(20))
Sample Output
3
Explanation
The count() method returns the total occurrences of an element.
Concepts Covered
- count()
6. Python Program to Find the Index of an Element
Problem Statement
Write a Python program to find the index of "Java" in a tuple.
Python Solution
courses = ("Python", "Java", "C++", "SQL")
print(courses.index("Java"))
Sample Output
1
Explanation
The index() method returns the position of the first matching element.
Concepts Covered
- index()
7. Python Program to Slice a Tuple
Problem Statement
Write a Python program to print the first three elements of a tuple.
Python Solution
numbers = (10, 20, 30, 40, 50)
print(numbers[:3])
Sample Output
(10, 20, 30)
Explanation
Tuple slicing returns a new tuple containing the selected elements.
Concepts Covered
- Tuple Slicing
8. Python Program to Pack and Unpack a Tuple
Problem Statement
Write a Python program to create a tuple using packing and then unpack its values into separate variables.
Python Solution
student = ("Alex", 21, "Python")
name, age, course = student
print(name)
print(age)
print(course)
Sample Output
Alex
21
Python
Explanation
Packing groups multiple values into a tuple, while unpacking assigns them to individual variables.
Concepts Covered
- Tuple Packing
- Tuple Unpacking
9. Python Program to Concatenate Two Tuples
Problem Statement
Write a Python program to join two tuples.
Python Solution
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
result = tuple1 + tuple2
print(result)
Sample Output
(1, 2, 3, 4, 5, 6)
Explanation
The + operator combines two tuples into one.
Concepts Covered
- Tuple Concatenation
10. Python Program to Find the Maximum and Minimum Value in a Tuple
Problem Statement
Write a Python program to find the largest and smallest values in a tuple.
Python Solution
numbers = (25, 10, 85, 42, 60)
print("Maximum:", max(numbers))
print("Minimum:", min(numbers))
Sample Output
Maximum: 85
Minimum: 10
Explanation
The max() and min() functions return the largest and smallest values in a tuple.
Concepts Covered
- max()
- min()
11. Python Program to Find the Tuple with the Maximum Sum
Problem Statement
Write a Python program to find the tuple having the maximum sum from a tuple of tuples.
Python Solution
data = ((4, 8, 2), (10, 5, 1), (6, 6, 6), (9, 9, 3))
max_tuple = max(data, key=sum)
print("Tuple with Maximum Sum:", max_tuple)
Sample Output
Tuple with Maximum Sum: (9, 9, 3)
Explanation
The sum() function calculates the sum of each tuple, while max() returns the tuple with the highest total.
Concepts Covered
- Tuple of Tuples
- sum()
- max()
- Lambda Functions
12. Python Program to Count the Frequency of Elements in a Tuple
Problem Statement
Write a Python program to count the occurrence of each element in a tuple.
Python Solution
numbers = (10, 20, 10, 30, 20, 10, 40, 30)
frequency = {}
for item in numbers:
frequency[item] = frequency.get(item, 0) + 1
print(frequency)
Sample Output
{10: 3, 20: 2, 30: 2, 40: 1}
Explanation
A dictionary stores each tuple element as a key and its frequency as the value.
Concepts Covered
- Tuple Traversal
- Dictionary
- Frequency Counting
13. Python Program to Find Common Elements Between Two Tuples
Problem Statement
Write a Python program to find the common elements between two tuples.
Python Solution
tuple1 = (10, 20, 30, 40, 50)
tuple2 = (30, 40, 60, 70)
common = tuple(set(tuple1) & set(tuple2))
print("Common Elements:", common)
Sample Output
Common Elements: (40, 30)
Explanation
Both tuples are converted into sets and the intersection operator (&) finds the common values.
Concepts Covered
- Tuple
- Set
- Intersection
14. Python Program to Sort a Tuple of Tuples by the Second Element
Problem Statement
Write a Python program to sort a tuple of tuples based on the second element.
Python Solution
students = (
("Rahul", 82),
("Ankit", 75),
("Priya", 91),
("Neha", 88)
)
sorted_data = sorted(students, key=lambda x: x[1])
print(tuple(sorted_data))
Sample Output
(('Ankit', 75), ('Rahul', 82), ('Neha', 88), ('Priya', 91))
Explanation
The sorted() function sorts the tuple using the second value of each nested tuple.
Concepts Covered
- Nested Tuples
- Lambda Function
- sorted()
15. Python Program to Remove Duplicate Elements from a Tuple
Problem Statement
Write a Python program to remove duplicate values from a tuple while preserving their order.
Python Solution
numbers = (10, 20, 10, 30, 20, 40, 50)
unique = tuple(dict.fromkeys(numbers))
print(unique)
Sample Output
(10, 20, 30, 40, 50)
Explanation
dict.fromkeys() removes duplicate values while maintaining the original order.
Concepts Covered
- Tuple
- Dictionary
- Duplicate Removal
16. Python Program to Flatten a Nested Tuple
Problem Statement
Write a Python program to flatten a nested tuple into a single tuple.
Python Solution
nested = ((1, 2), (3, 4), (5, 6))
flat = ()
for item in nested:
flat += item
print(flat)
Sample Output
(1, 2, 3, 4, 5, 6)
Explanation
Each inner tuple is concatenated to create one flat tuple.
Concepts Covered
- Nested Tuple
- Tuple Concatenation
17. Python Program to Find the Pair with the Maximum Product
Problem Statement
Write a Python program to find the tuple that has the maximum product.
Python Solution
pairs = ((2, 5), (4, 6), (3, 10), (8, 7))
maximum = max(pairs, key=lambda x: x[0] * x[1])
print(maximum)
Sample Output
(8, 7)
Explanation
The lambda function calculates the product of each tuple, and max() returns the tuple with the highest product.
Concepts Covered
- Tuple
- Lambda
- max()
18. Python Program to Rotate a Tuple by N Positions
Problem Statement
Write a Python program to rotate a tuple to the left by N positions.
Python Solution
numbers = (10, 20, 30, 40, 50, 60)
n = int(input("Enter rotation: "))
n = n % len(numbers)
rotated = numbers[n:] + numbers[:n]
print(rotated)
Sample Output
Enter rotation: 2
(30, 40, 50, 60, 10, 20)
Explanation
Tuple slicing is used to rotate the tuple efficiently.
Concepts Covered
- Tuple Slicing
- User Input
19. Python Program to Find the Longest Tuple
Problem Statement
Write a Python program to find the longest tuple from a tuple containing multiple tuples.
Python Solution
data = (
(1, 2),
(4, 5, 6),
(7,),
(8, 9, 10, 11)
)
longest = max(data, key=len)
print(longest)
Sample Output
(8, 9, 10, 11)
Explanation
The len() function is used to compare the size of each tuple.
Concepts Covered
- Nested Tuples
- len()
- max()
20. Python Program to Group Elements into Pairs
Problem Statement
Write a Python program to convert a tuple into pairs of two elements.
Python Solution
numbers = (1, 2, 3, 4, 5, 6, 7, 8)
pairs = tuple((numbers[i], numbers[i + 1]) for i in range(0, len(numbers), 2))
print(pairs)
Sample Output
((1, 2), (3, 4), (5, 6), (7, 8))
Explanation
The program groups every two consecutive elements into a new tuple.
Concepts Covered
- Tuple Comprehension
- range()
- Nested Tuples
Frequently Asked Questions (FAQs)
1. What is a tuple in Python?
A tuple is an ordered, immutable collection of elements. Once created, its elements cannot be modified, added, or removed. Tuples are defined using parentheses ().
2. What is the difference between a list and a tuple?
Lists are mutable, meaning their contents can be changed after creation. Tuples are immutable, making them faster and more memory-efficient for storing fixed collections of data.
3. Why should I use tuples instead of lists?
Tuples are ideal when your data should remain constant. They provide better performance, use less memory, and can be used as dictionary keys because they are immutable.
4. Can a tuple contain different data types?
Yes. A tuple can store integers, floats, strings, lists, dictionaries, other tuples, and even user-defined objects in the same collection.
Example:
data = (101, "Python", 95.5, True)
5. Can tuples be nested?
Yes. A tuple can contain other tuples, allowing you to represent structured or multidimensional data.
Example:
student = (
("Rahul", 90),
("Ankit", 85),
("Priya", 95)
)
6. Can a tuple contain mutable objects?
Yes. Although a tuple itself is immutable, it can contain mutable objects like lists or dictionaries. However, modifying those mutable objects changes their contents without changing the tuple structure.
7. Where are tuples used in real-world Python applications?
Tuples are commonly used in:
- Database query results
- Geographic coordinates (latitude, longitude)
- Configuration settings
- Returning multiple values from functions
- Data science and NumPy arrays
- Dictionary keys
- Machine learning datasets
- Scientific computing
8. Why are tuples important for Python interviews?
Tuple-related questions frequently appear in Python interviews because they test your understanding of immutable data structures, nested collections, sorting, unpacking, lambda functions, and efficient data handling. Mastering tuples helps you write cleaner, faster, and more reliable Python programs.
Chapter Summary
After completing this chapter, you have learned:
- Creating tuples
- Tuple indexing
- Negative indexing
- Tuple slicing
- Tuple packing and unpacking
len()count()index()- Tuple concatenation
max()andmin()
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
