Python Program to Create a Tuple

Write a Python program to create a Tuple with an example. In this Programming language, there are two ways to create a Tuple. The first option is using the () brackets, and the other option is the tuple() function.

The following example creates an empty tuple using both options.

x = ()

print(x)

y = tuple()
print(y)

Output of an Empty Tuple

()
()

In this example program, we are creating a tuple of items. We all know that the tuple function accepts any iterator and converts it into a tuple. So, we declared a fruit list and used the tuple function to convert it into a Python tuple. Please refer to the Python tutorial page.

x = (10, 20, 30, 40, 50)
print(x)
print("Datatype of y      = ", type(x))

fruits = ['Kiwi', 'Banana', 'Apple', 'Orange']
y = tuple(fruits)

print(y)
print("Datatype of Fruits = ", type(fruits))
print("Datatype of y      = ", type(y))
Python Program to Create a Tuple

Python Program to Create Tuple of Different Types

Write a Python Program to Create Different Types of Tuples and print them. This example shows the creation of integer, string, boolean, float, mixed tuple, tuple inside a tuple (nested tuple), and list tuple. 

# Different Type Tuples
 
numericTuple = (10, 20, 30, 40, 50)
print("Numeric Tuple Items = ", numericTuple )
 
floatTuple = (10.25, 11.20, 19.37, 41.598)
print("Float Tuple Items = ", floatTuple )
 
stringTuple = ('orange', 'Mango', 'Grape', 'Apple')
print("String Tuple Items = ", stringTuple )
 
booleanTuple = (True, False, False, True, True)
print("Boolean Tuple Items = ", booleanTuple )
 
mixedTuple = ('orange', 25, 'Mango', 36.75, False, 10)
print("Mixed Tuple Items = ", mixedTuple )
 
nestedTuple = (10, 20, ('orange', 'Mango'), 30)
print("Nested Tuple Items = ", nestedTuple )
 
listTuple = (10, 20, ['Grape', 'Apple'], 70)
print("List Tuple Items = ", listTuple )
Python Program to Create Tuple of Different Types

Also Read

  1. Python Program to Find Tuple Length
  2. Python Program to Remove an Item from Tuple