Python Program to Perform Operations on Tuple

Computer Bits Daily
2 min readJun 30, 2023

Tuples are a type of data structure in Python that are similar to lists, but unlike lists, tuples are immutable, meaning their contents cannot be changed once they are created. In this blog post, we will discuss how to create a tuple with different data types, print tuple items, convert a tuple into a list, remove data items from a tuple, convert a list into a tuple, and print tuple items again.

Write a python program to perform below operations on tuple:
 Create a tuple with different data types.
 Print tuple items.
 Convert tuple into a list.
 Remove data items from a list.
 Convert list into a tuple.
 Print tuple items.

Table of Contents

  • Creating a Tuple with Different Data Types
  • Printing Tuple Items
  • Converting a Tuple into a List
  • Removing Data Items from a Tuple
  • Converting a List into a Tuple
  • Printing Tuple Items Again

| Learn Python with “ Computer Courses — All in One “

Creating a Tuple with Different Data Types

To create a tuple with different data types, we can simply enclose the items in parentheses and separate them with commas. Here’s an example:

my_tuple = (1, “hello”, 3.14, True)

In this example, we have created a tuple called my_tuple with four items of different data types: an integer, a string, a float, and a boolean.

Printing Tuple Items

To print the items in a tuple, we can use a for loop to iterate over the tuple and print each item. Here’s an example:

my_tuple = (1, “hello”, 3.14, True)

for item in my_tuple:

print(item)

This will output:

1

hello

3.14

True

Converting a Tuple into a List

To convert a tuple into a list, we can use the list() function. Here’s an example:

my_tuple = (1, “hello”, 3.14, True)

my_list = list(my_tuple)

In this example, we have converted the my_tuple tuple into a list called my_list.

Sign up to discover human stories that deepen your understanding of the world.

Computer Bits Daily
Computer Bits Daily

Written by Computer Bits Daily

Learning Computer skills and Computer technology by learning and sharing

No responses yet

Write a response