"Python Tuple Exercises: Practice for Tuple Manipulation"

thumb_up 1  ·  sell Python tuple exercises, Tuple manipulation exercises in Python, Tuple manipulation practice in Python

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 = 0 for 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: 24
Sum of all numbers sum() function: 24

Example 3

Python program to create a tuple of 5 random integers −

 
import random t1 = () 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.

 

 

The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back