Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • šŸ“ Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • šŸ“ Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • šŸ“ Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • šŸ“ Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • šŸ“ While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • šŸ“ For Each Assignments
    • šŸ“ All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • šŸ“ Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • šŸ“ Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • šŸ“ Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • šŸ“ Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • šŸ“ Assignments
  • Exception
    • Error vs Exception
    • Exception Handling
    • Types of Exception
    • User Defined Exception
    • Logging Exception
    • šŸ“ Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • šŸ“ Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • šŸ“ Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • šŸ“ Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • šŸ“ Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • šŸ“ Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • šŸ“ Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • šŸ“ Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • šŸ“ Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • šŸ“ Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • šŸ“ Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • šŸ“ Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • šŸ“ Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • šŸ“ Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

šŸ“š Python Tuple: Practice Assignments

Python Tuple Practice Assignments

Comprehensive practice covering tuple creation, access, methods, iteration, unpacking, and more.

Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
Created by experienced Python developers to make programming easy through practical examples and clear explanations.
šŸ“‘ On this page:
  • About
  • Tuple Creation
  • Access & Slicing
  • Tuple Methods
  • Iteration
  • Unpacking
  • Advanced
  • Try It
Share:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link

About These Assignments

This page contains 30+ unique practice assignments covering all tuple concepts in Python. Unlike the topic-specific pages, these problems are designed to test your ability to choose the right tuple technique for each situation and combine tuple operations effectively.

🟢 Beginner 🟠 Intermediate šŸ”“ Advanced
šŸŽÆ Unique Challenge: Each problem is designed to be different from the topic-specific assignments. You'll need to decide which tuple technique (methods, slicing, unpacking, etc.) is most appropriate for each scenario.

šŸ’” Tip: Try each problem yourself first. Click "Show Solution" only after attempting.

1. Tuple Creation Challenges 4 Questions

šŸŽÆ Focus: Create tuples using different techniques.
Beginner Q1: Create Tuple from Range
Create a tuple containing all odd numbers from 1 to 19 using the range function.
šŸ’” Solution:
odds = tuple(range(1, 20, 2))
print(odds) # (1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
šŸ“– Explanation: range(start, stop, step) with tuple() constructor.
Beginner Q2: Tuple from List
Convert a list of numbers into a tuple.
šŸ’” Solution:
numbers = [10, 20, 30, 40, 50]
tup = tuple(numbers)
print(tup) # (10, 20, 30, 40, 50)
šŸ“– Explanation: tuple() constructor converts iterable to tuple.
Intermediate Q3: Nested Tuple
Create a nested tuple (tuple of tuples) representing a 2x3 matrix.
šŸ’” Solution:
matrix = ((1, 2, 3), (4, 5, 6))
print(matrix) # ((1, 2, 3), (4, 5, 6))
šŸ“– Explanation: Tuples can contain other tuples as elements.
Advanced Q4: Single Element Tuple
Create a tuple with a single element (the number 42) and verify its type.
šŸ’” Solution:
single = (42,)
print(f"Value: {single}, Type: {type(single)}")
# Value: (42,), Type:
šŸ“– Explanation: A trailing comma is required for a single-element tuple.

2. Access & Slicing Challenges 4 Questions

šŸŽÆ Focus: Access and slice tuples effectively.
Beginner Q5: Access Elements
Given a tuple, access the first, third, and last elements.
šŸ’” Solution:
tup = (10, 20, 30, 40, 50, 60)
first = tup[0]
third = tup[2]
last = tup[-1]
print(f"First: {first}, Third: {third}, Last: {last}")
šŸ“– Explanation: Indexing works same as lists.
Beginner Q6: Slice Tuple
Extract the middle four elements from a tuple of 8 elements.
šŸ’” Solution:
tup = (1, 2, 3, 4, 5, 6, 7, 8)
middle = tup[2:6]
print(middle) # (3, 4, 5, 6)
šŸ“– Explanation: Slicing works same as lists.
Intermediate Q7: Reverse Tuple
Reverse a tuple using slicing (without using reversed() function).
šŸ’” Solution:
tup = (1, 2, 3, 4, 5)
reversed_tup = tup[::-1]
print(reversed_tup) # (5, 4, 3, 2, 1)
šŸ“– Explanation: Step -1 reverses the tuple.
Advanced Q8: Every Third Element
Extract every third element from a tuple starting from index 0.
šŸ’” Solution:
tup = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
every_third = tup[::3]
print(every_third) # (1, 4, 7, 10)
šŸ“– Explanation: Slicing with step 3.

3. Tuple Methods Challenges 4 Questions

šŸŽÆ Focus: Use built-in tuple methods effectively.
Beginner Q9: Count and Index
Find how many times 5 appears and its first index using count() and index().
šŸ’” Solution:
tup = (1, 5, 3, 5, 7, 5, 9, 5)
count = tup.count(5)
first_index = tup.index(5)
print(f"Count: {count}, First index: {first_index}")
šŸ“– Explanation: count() returns occurrences, index() returns first position.
Beginner Q10: Find Element or Handle Error
Find the index of element 42 in a tuple, handle ValueError if not found.
šŸ’” Solution:
tup = (10, 20, 30, 40, 50)
try:
index = tup.index(42)
print(f"Found at index: {index}")
except ValueError:
print("42 not found in tuple")
šŸ“– Explanation: index() raises ValueError if element not found.
Intermediate Q11: Check Membership
Check if a given element exists in a tuple using the 'in' operator.
šŸ’” Solution:
tup = ("apple", "banana", "cherry", "date")
item = "banana"
if item in tup:
print(f"{item} found in tuple")
else:
print(f"{item} not found")
šŸ“– Explanation: 'in' operator checks membership in tuple.
Advanced Q12: Tuple Length and Min/Max
Find the length, minimum, and maximum of a tuple of numbers.
šŸ’” Solution:
tup = (25, 18, 42, 7, 31, 56, 19)
length = len(tup)
minimum = min(tup)
maximum = max(tup)
print(f"Length: {length}, Min: {minimum}, Max: {maximum}")
šŸ“– Explanation: len(), min(), max() work on tuples.

4. Iteration Challenges 4 Questions

šŸŽÆ Focus: Iterate over tuples effectively.
Beginner Q13: Sum with For Loop
Calculate the sum of all elements in a tuple using a for loop.
šŸ’” Solution:
tup = (10, 20, 30, 40, 50)
total = 0
for num in tup:
total += num
print(f"Sum: {total}")
šŸ“– Explanation: Iterates through each element and accumulates sum.
Beginner Q14: Print Index and Value
Print both index and value for each element using enumerate.
šŸ’” Solution:
tup = ("apple", "banana", "cherry", "date")
for index, fruit in enumerate(tup):
print(f"{index}: {fruit}")
šŸ“– Explanation: enumerate() provides index-value pairs.
Intermediate Q15: Tuple of Tuples Iteration
Iterate over a tuple of tuples and print each inner tuple's elements.
šŸ’” Solution:
data = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
for inner in data:
for item in inner:
print(item, end=' ')
print()
šŸ“– Explanation: Nested for loops to traverse all levels.
Advanced Q16: While Loop Iteration
Iterate over a tuple using a while loop and print each element squared.
šŸ’” Solution:
tup = (1, 2, 3, 4, 5)
i = 0
while i < len(tup):
print(tup[i] ** 2, end=' ')
i += 1
# Output: 1 4 9 16 25
šŸ“– Explanation: While loop with index to access tuple elements.

5. Unpacking Challenges 4 Questions

šŸŽÆ Focus: Unpack tuples effectively.
Beginner Q17: Basic Unpacking
Unpack a tuple of three elements into three variables.
šŸ’” Solution:
tup = (10, 20, 30)
a, b, c = tup
print(f"a: {a}, b: {b}, c: {c}")
šŸ“– Explanation: Tuple unpacking assigns each element to a variable.
Beginner Q18: Swap Variables
Swap two variables using tuple unpacking.
šŸ’” Solution:
x, y = 5, 10
print(f"Before: x={x}, y={y}")
x, y = y, x
print(f"After: x={x}, y={y}")
šŸ“– Explanation: Tuple unpacking for variable swapping.
Intermediate Q19: Extended Unpacking with *
Unpack a tuple into variables using * for the remaining elements.
šŸ’” Solution:
tup = (1, 2, 3, 4, 5)
first, second, *rest = tup
print(f"First: {first}, Second: {second}, Rest: {rest}")
šŸ“– Explanation: * collects remaining elements into a list.
Advanced Q20: Nested Unpacking
Unpack a nested tuple (tuple of tuples).
šŸ’” Solution:
data = ((1, 2), (3, 4), (5, 6))
for a, b in data:
print(f"a={a}, b={b}")
šŸ“– Explanation: Nested unpacking in loop.

6. Advanced Tuple Challenges 4 Questions

šŸŽÆ Focus: Combine multiple tuple techniques.
Advanced Q21: Tuple Concatenation
Concatenate two tuples and find the common elements.
šŸ’” Solution:
t1 = (1, 2, 3, 4, 5)
t2 = (4, 5, 6, 7, 8)
combined = t1 + t2
common = tuple(set(t1) & set(t2))
print(f"Combined: {combined}")
print(f"Common: {common}")
šŸ“– Explanation: + for concatenation, set intersection for common elements.
Advanced Q22: Sort Tuple
Sort a tuple without converting to list (use sorted()).
šŸ’” Solution:
tup = (5, 2, 8, 1, 9, 3)
sorted_tup = tuple(sorted(tup))
print(sorted_tup) # (1, 2, 3, 5, 8, 9)
šŸ“– Explanation: sorted() returns list, tuple() converts back.
Advanced Q23: Tuple of Dictionaries
Create a tuple of dictionaries and access values.
šŸ’” Solution:
data = ({"name": "Alice", "age": 25}, {"name": "Bob", "age": 30})
for person in data:
print(f"Name: {person['name']}, Age: {person['age']}")
šŸ“– Explanation: Tuples can contain dictionaries as elements.
Advanced Q24: Filter Tuple
Create a new tuple containing only even numbers from the original tuple.
šŸ’” Solution:
tup = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
evens = tuple(x for x in tup if x % 2 == 0)
print(evens) # (2, 4, 6, 8, 10)
šŸ“– Explanation: Generator expression with tuple() conversion.

Try It Yourself!

Use the interactive editor below to test your solutions or write your own tuple code.

Loading Pyodide... 0%
Python Code Editor
========================================
TUPLE PRACTICE
========================================
Numbers: (1, 2, 3, 4, 5)
Middle elements: (30, 40, 50)
Count of 'red': 2
Index of 'blue': 2
Unpacked: 1, 2, 3, 4, 5

āœ… Write your tuple solutions here!

šŸ“š Related Tutorials

šŸ“¦ Tuple in Python

Learn about tuples in Python

šŸ”§ Tuple Functions

Explore built-in tuple methods

šŸ“¤ Tuple Unpacking

Master tuple unpacking

šŸ“– Related Tutorials
  • Access Tuple Elements
  • Iterate Tuple
  • List in Python
  • List vs Tuple
Previous: Tuple Comprehension Next: Set in Python →