Course
Tuple Exercises
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Tuple Exercises
Example 1
Python program to find unique numbers in a given tuple
T1 = (1, 9, 1, 6, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 2)T2 = ()for x in T1: if x not in T2: T2+=(x,)print ("original tuple:", T1)print ("Unique numbers:", T2)
It will produce the following output
original tuple: (1, 9, 1, 6, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 2)Unique numbers: (1, 9, 6, 3, 4, 5, 2, 7, 8)
Example 2
Python program to find sum of all numbers in a tuple
T1 = (1, 9, 1, 6, 3, 4)ttl = 0for x in T1: ttl+=x print ("Sum of all numbers Using loop:", ttl)
ttl = sum(T1)print ("Sum of all numbers sum() function:", ttl)
It will produce the following output
Sum of all numbers Using loop: 24Sum of all numbers sum() function: 24
Example 3
Python program to create a tuple of 5 random integers
import randomt1 = ()for i in range(5): x = random.randint(0, 100) t1+=(x,)print (t1)
It will produce the following output
(64, 21, 68, 6, 12)
Exercise Programs
- Python program to remove all duplicates numbers from a list.
- Python program to sort a tuple of strings on the number of alphabets in each word.
- Python program to prepare a tuple of non-numeric items from a given tuple.
- Python program to create a tuple of integers representing each character in a string
- Python program to find numbers common in two tuples.
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.