Create a tuple in Python

By: On:

In this Python tutorial, we will discuss how to create a tuple in Python, we will learn about the Tuple in Python with examples.

  • What is a tuple in Python
  • How to create an empty tuple in python
  • How to access tuple items in python
  • Add items in the tuple python
  • Length of a tuple in python
  • Remove item from tuple python
  • Change tuple values in Python
  • Python Join two tuples
  • Python check if element exists in a tuple
  • Loop through a tuple in python
  • Reverse a tuple in python

How to create a tuple in Python? To create a tuple in Python, add items with round brackets “()” like my_tuple = (“red”, “blue”, “green”) and to create an empty tuple, use empty round brackets ” () ” with no items in it.

Tuple in Python

In python, a tuple is a collection of items that are ordered and immutable(unchangeable), tuples are written with round brackets “()” and it can contain mixed data types.

Example:

my_tuple = ("red", "blue", "green")
print(my_tuple)

After writing the above code (tuple in Python), Ones you will print “my_tuple” then the output will appear as a “ (‘red’, ‘blue’, ‘green’) ”. Here, a tuple is created with some object.

You can refer to the below screenshot tuples in Python.


Tuple in Python
Tuple in Python

How to create an empty tuple in python

To create an empty tuple in Python, use a empty round brackets ” () “ with no items in it.

Example:

my_tuple = ()
print(my_tuple)

After writing the above code (create an empty tuple in python), Ones you will print “my_tuple” then the output will appear as a “ () ”. Here, an empty tuple is created with no object in it.

You can refer to the below screenshot how to create an empty tuple in python.

How to create an empty tuple in python
How to create an empty tuple in python

Access tuple items in Python

In python, to access tuple items we can use the index number inside the square brackets for getting the specified items from the tuple.

Example:

my_tuple = ("red", "blue", "green")
print(my_tuple[2])

After writing the above code (access tuple items in python), Ones you will print “my_tuple[2]” then the output will appear as a “ green ”. Here, we want to print the third item in the tuple so the index number is ” [2] ” for green.

You can refer to the below screenshot access tuple items in python.

Access tuple items in python
Access tuple items in python

Add items in the tuple python

In python, you cannot add items in the tuple ones it is created and tuples are unchangeable which means we cannot change.

Example:

my_tuple = ("red", "blue", "green")
my_tuple[3] = "pink"
print(my_tuple)

After writing the above code (add items in the tuple python), Ones you will print “my_tuple” then the output will appear as a “ TypeError: ‘tuple’ object does not support item assignment ”. Here, we cannot add items in tuples and this will raise an error.

You can refer to the below screenshot add items in the tuple python.

Add items in the tuple python
Add items in the tuple python

Length of a tuple in python

In python, to get the length of a tuple we will use len() method and it will return the number of items in a tuple.

Example:

my_tuple = ("red", "blue", "green")
print(len(my_tuple))

After writing the above code (length of a tuple in python), Ones you will print “len(my_tuple)” then the output will appear as a “ 3 ”. Here, the len() method will give the length of an item in a tuple.

You can refer to the below screenshot length of a tuple in python.

Length of a tuple in python
Length of a tuple in python

Remove item from tuple python

In python, we cannot change the item as tuples are unchangeable, so removing the particular items is not possible but we can delete the tuple completely by using the del() method in python.

Example:

my_tuple = ("red", "blue", "green")
del my_tuple
print(my_tuple)

After writing the above code (remove the item from tuple python), Ones you will print “(my_tuple)” then the output will appear as a “ NameError: name ‘my_tuple’ is not defined ”. Here, the del() method will delete the tuple completely and it will throw an error.

You can refer to the below screenshot remove item from tuple python.

Remove item from tuple python
Remove item from tuple python

Change tuple values in Python

In python, as we know one’s tuple created, it cannot change its value as a tuple is immutable but we can change by converting the tuple into a list and converting the list back to a tuple.

Example:

my_tuple = ("red", "blue", "green")
a = list(my_tuple)
a[1] = "black"
my_tuple = tuple(a)
print(my_tuple)

After writing the above code (change tuple values in Python), Ones you will print “(my_tuple)” then the output will appear as a “ (‘red’, ‘black’, ‘green’) ”. Here, the elements of a tuple cannot be changed but it can convert a tuple into a list, then change the list and convert it back into a tuple.

You can refer to the below screenshot change tuple values in Python.

Change tuple values in Python
Change tuple values in Python

Python Join two tuple

In python, we will use ‘+’ operator to join two or more tuples.

Example:

my_tuple1 = ("red", "blue", "green")
my_tuple2 = (2, 4, 6)
my_tuple3 = my_tuple1 + my_tuple2
print(my_tuple3)

After writing the above code (python Join two tuple), Ones you will print “(my_tuple3)” then the output will appear as a “(‘red’, ‘blue’, ‘green’, 2, 4, 6)”. Here, we assign the value of two existing tuples to a new tuple by using the ‘ +’ operator.

You can refer to the below screenshot python Join two tuple

Python Join two tuple
Python Join two tuple

Python check if element exists in a tuple

In python, to check if the specified element is present in the tuple we use the” in “ keyword to check and if the element is not present then it will not return anything.

Example:

my_tuple = ("red", "blue", "green")
if "blue" in my_tuple:
print("Yes, blue is present")

After writing the above code (python check if element exists in a tuple), Ones you will print then the output will appear as a “ Yes, blue is present ”. Here, if the specified element is present in the tuple then it will return the output.

You can refer to the below screenshot python check if element exists in a tuple.

Python check if element exists in a tuple
Python check if element exists in a tuple

Loop through a tuple in python

In python, we use for loop to iterate through the tuples elements to print the values.

Example:

my_tuple = ("red", "blue", "green")
for a in my_tuple
print(a)

After writing the above code (loop through a tuple in python), Ones you will print “(a)” then the output will appear as a “ red blue green”. Here, by using for loop it can iterate through the tuple items.

You can refer to the below screenshot python Join two tuple.

Loop through a tuple in python
Loop through a tuple in python

Reverse a tuple in python

In python, we will use the built-in method called reversed() to reverse the tuple and the reversed tuple is converted to tuple before printing.

Example:

my_tuple = ("red", "blue", "green")
reverse = reversed(my_tuple)
print(tuple(reverse))

After writing the above code (reverse a tuple in python), Ones you will print “tuple(reverse)” then the output will appear as a “ (‘green’, ‘blue’, ‘red’) ”. Here, by using the reversed() method the element gets reversed.

You can refer to the below screenshot reverse a tuple in python.

Reverse a tuple in python
Reverse a tuple in python

You may like the following Python tutorials:

In this Python tutorial, we learned how to create a tuple in Python and also we have seen like:

  • What is a tuple in Python
  • How to create an empty tuple in python
  • How to access tuple items in python
  • Add items in the tuple python
  • Length of a tuple in python
  • Remove item from tuple python
  • Change tuple values in Python
  • Python Join two tuples
  • Python check if element exists in a tuple
  • Loop through a tuple in python
  • Reverse a tuple in python

Leave a Comment