PYnative

Python Programming

  • Tutorials
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks

Python Tuple Exercise with Solutions

Last updated on July 21, 2020

TweetF  sharein  shareP  Pin

A tuple object is immutable in Python. Tuples are also sequences, just like Python lists. The tuples cannot be changed. This Python Tuple exercise aims to help Python developers to learn and practice tuple operations. Here I am providing 10 tuple programs to help you in brushing up your coding skills. All questions are tested on Python 3.

Further Reading:

  • Explore All Python Exercises and Python Quizzes to practice Python
  • Also, try to solve a Python Tuple Quiz

What included in this Tuple exercise?

The exercise contains 10 questions and solutions provided for each question. This Python tuple coding exercise is nothing but Python tuple assignments to solve, where you can solve and practice different tuple programs, questions, problems, and challenges.

Each question includes a specific tuple related topic you need to learn. 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.

Exercise Question 1: Reverse the following tuple

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

Expected output:

(50, 40, 30, 20, 10)

Solution:

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

Exercise Question 2: Access value 20 from the following tuple

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

Expected output:

20

Solution:

aTuple = ("Orange", [10, 20, 30], (5, 15, 25))
print(aTuple[1][1])
Show Solution
Hide Solution

Exercise Question 3: Create a tuple with single item 50

Solution:

aTuple = (50, )
print(aTuple)

If you want to create a single value tuple, you must indicate it by adding a comma just before the closing parentheses.

Example: Not a tuple

aTuple = (50)
print(type(aTuple ))  # <type 'int'>
Show Solution
Hide Solution

Exercise Question 4: Unpack the following tuple into 4 variables

aTuple = (10, 20, 30, 40)

Expected output:

aTuple = (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

Solution:

aTuple = (10, 20, 30, 40)
a, b, c, d = aTuple
print(a)
print(b)
print(c)
print(d)
Show Solution
Hide Solution

Exercise Question 5: Swap the following two tuples

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

Expected output:

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

Solution:

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

Exercise Question 6: Copy element 44 and 55 from the following tuple into a new tuple

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

Expected output:

tuple2 = (44, 55)

Solution:

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

Exercise Question 7: Modify the first item (22) of a list inside a following tuple to 222

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

Expected output:

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

Solution:

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

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

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

Expected output:

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

Solution:

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

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

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

Expected output:

2

Solution:

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

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

tuple1 = (45, 45, 45, 45)

Expected output:

True

Solution:

def check(sampleTuple):
    return all(i == sampleTuple[0] for i in sampleTuple)

tuple1 = (45, 45, 45, 45)
print(check(tuple1))
Show Solution
Hide Solution

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

TweetF  sharein  shareP  Pin

Is this article/website helpful?

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!

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
Show All Exercises

 Show All Quizzes  

Keep Reading Python

Python Input & Output Python MySQL Python PostgreSQL Python SQLite Python JSON Python Quizzes Python Exercises Python Generate random data

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your code </pre>

19 Comments

Practice Python


Practice Python using our 15+ Free Topic-specific Exercises and Quizzes

All exercises and Quizzes are tested on Python 3

Exercises
Quizzes

 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
TweetF  sharein  shareP  Pin

About PYnative

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

Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow PYnative

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter   Facebook
  • RSS | Sitemap

Legal Stuff

  • About Us
  • Privacy Policy
  • Cookie Policy
  • Terms Of Use
  • Contact Us
DMCA.com Protection Status

Copyright © 2018-2021 · [pynative.com]

This website uses cookies to ensure you get the best experience on our website.Privacy PolicyGot it!