PYnative

Python Programming

  • Learn Python
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks
Home » Python Exercises » Python Tuple Exercise with Solutions

Python Tuple Exercise with Solutions

Updated on: December 8, 2021 | 73 Comments

A tuple is an immutable object in Python that can’t be changed. Tuples are also sequences, just like Python lists.

This Python Tuple exercise aims to help you to learn and practice tuple operations. All questions are tested on Python 3.

Also Read:

  • Python Tuples
  • Python Tuple Quiz

This Tuple exercise includes the following: –

  • It contains 10 questions and solutions provided for each question.
  • It has questions to practice Python tuple assignments, programs, and challenges.
  • This tuple exercise covers tuple creation, operations, unpacking of a tuple.

When you complete each question, you get more familiar with Python tuple. Let us know if you have any alternative solutions. It will help other developers.

  • Use Online Code Editor to solve exercise questions.
  • Read the complete guide on Python Tuples to solve this exercise.

Table of contents

  • Exercise 1: Reverse the tuple
  • Exercise 2: Access value 20 from the tuple
  • Exercise 3: Create a tuple with single item 50
  • Exercise 4: Unpack the tuple into 4 variables
  • Exercise 5: Swap two tuples in Python
  • Exercise 6: Copy specific elements from one tuple to a new tuple
  • Exercise 7: Modify the tuple
  • Exercise 8: Sort a tuple of tuples by 2nd item
  • Exercise 9: Counts the number of occurrences of item 50 from a tuple
  • Exercise 10: Check if all items in the tuple are the same

Exercise 1: Reverse the tuple

Given:

tuple1 = (10, 20, 30, 40, 50)

Expected output:

(50, 40, 30, 20, 10)
Show Hint

Use tuple slicing to reverse the given tuple. Note: the last element starts at -1.

Show Solution
tuple1 = (10, 20, 30, 40, 50)
tuple1 = tuple1[::-1]
print(tuple1)

Exercise 2: Access value 20 from the tuple

The given tuple is a nested tuple. write a Python program to print the value 20.

Given:

tuple1 = ("Orange", [10, 20, 30], (5, 15, 25))

Expected output:

20

Show Hint

The given tuple is a nested tuple. Use indexing to locate the specified item.

Show Solution
tuple1 = ("Orange", [10, 20, 30], (5, 15, 25))

# understand indexing
# tuple1[0] = 'Orange'
# tuple1[1] = [10, 20, 30]
# list1[1][1] = 20

print(tuple1[1][1])

Exercise 3: Create a tuple with single item 50

Show Solution
tuple1= (50, )
print(tuple1)

Exercise 4: Unpack the tuple into 4 variables

Write a program to unpack the following tuple into four variables and display each variable.

Given:

tuple1 = (10, 20, 30, 40)

Expected output:

tuple1 = (10, 20, 30, 40)
# Your code
print(a) # should print 10
print(b) # should print 20
print(c) # should print 30
print(d) # should print 40
Show Solution
tuple1 = (10, 20, 30, 40)

# unpack tuple into 4 variables
a, b, c, d = tuple1
print(a)
print(b)
print(c)
print(d)

Exercise 5: Swap two tuples in Python

Given:

tuple1 = (11, 22)
tuple2 = (99, 88)

Expected output:

tuple1: (99, 88)
tuple2: (11, 22)
Show Solution
tuple1 = (11, 22)
tuple2 = (99, 88)
tuple1, tuple2 = tuple2, tuple1
print(tuple2)
print(tuple1)

Exercise 6: Copy specific elements from one tuple to a new tuple

Write a program to copy elements 44 and 55 from the following tuple into a new tuple.

Given:

tuple1 = (11, 22, 33, 44, 55, 66)

Expected output:

tuple2: (44, 55)
Show Solution
tuple1 = (11, 22, 33, 44, 55, 66)
tuple2 = tuple1[3:-1]
print(tuple2)

Exercise 7: Modify the tuple

Given is a nested tuple. Write a program to modify the first item (22) of a list inside a following tuple to 222

Given:

tuple1 = (11, [22, 33], 44, 55)

Expected output:

tuple1: (11, [222, 33], 44, 55)
Show Hint

The given tuple is a nested tuple. Use indexing to locate the specified item and modify it using the assignment operator.

Show Solution
tuple1 = (11, [22, 33], 44, 55)
tuple1[1][0] = 222
print(tuple1)

Exercise 8: Sort a tuple of tuples by 2nd item

Given:

tuple1 = (('a', 23),('b', 37),('c', 11), ('d',29))

Expected output:

(('c', 11), ('a', 23), ('d', 29), ('b', 37))
Show Solution
tuple1 = (('a', 23), ('b', 37), ('c', 11), ('d', 29))
tuple1 = tuple(sorted(list(tuple1), key=lambda x: x[1]))
print(tuple1)

Exercise 9: Counts the number of occurrences of item 50 from a tuple

Given:

tuple1 = (50, 10, 60, 70, 50)

Expected output:

2

Show Hint

Use the count() method of a tuple.

Show Solution
tuple1 = (50, 10, 60, 70, 50)
print(tuple1.count(50))

Exercise 10: Check if all items in the tuple are the same

tuple1 = (45, 45, 45, 45)

Expected output:

True

Show Solution
def check(t):
    return all(i == t[0] for i in t)

tuple1 = (45, 45, 45, 45)
print(check(tuple1))

Filed Under: Python, Python Basics, Python Exercises

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

Related Tutorial Topics:

Python Python Basics Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Posted In

Python Python Basics Python Exercises
TweetF  sharein  shareP  Pin

 Python Exercises

  • Python Exercises Home
  • Basic Exercise for Beginners
  • Input and Output Exercise
  • Loop Exercise
  • Functions Exercise
  • String Exercise
  • Data Structure Exercise
  • List Exercise
  • Dictionary Exercise
  • Set Exercise
  • Tuple Exercise
  • Date and Time Exercise
  • OOP Exercise
  • Python JSON Exercise
  • Random Data Generation Exercise
  • NumPy Exercise
  • Pandas Exercise
  • Matplotlib Exercise
  • Python Database Exercise

All Python Topics

Python Basics Python Exercises Python Quizzes Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy.

Copyright © 2018–2023 pynative.com