How to Create Tuple Variable in Python

You can create tuple variable easily using Python simple methods. You can access each element of the tuple in different ways. Perform Loop through each element of the tuple in Python.

But, what are the rules to create a tuple variable in Python? Well, here are the different methods of the tuple with the example given in the below sections. You can create your own tuple elements and perform certain operations given below.

First of all, you should know what is a tuple in Python. So, let’s start learning different methods given below one-by-one.

What is Tuple Variable in Python

A tuple is a collection of ordered elements and cannot be changed. It is a sequence of elements just like Python list but enclosed within the parenthesis or round brackets.

A tuple can contain elements of different data types including string and integers. You can access each element but cannot add or modify elements using Python.

The elements of the tuples are arranged in sequence with the assigned index starts from zero(0). The tuple elements are enclosed within the round brackets with comma-separation.

How to Create Tuple Variable in Python

To create tuple variable in Python, you have to enclose the elements within the round bracket. You can include the string as well as the integer elements in the tuple.

There is little difference between the list and tuple. The list elements are enclosed within the square bracket. While tuple element enclosed within the round brackets. The element can be placed in the tuple with comma separation.

Output

(‘Goa’, 4, ‘Bihar’, ‘Punjab’, 19, 81)

The above example prints the elements in the output. The output prints all the elements enclosed within the round brackets.

However, you can print only the required elements with the access methods given below.

Access Tuple Elements With Index Operator in Python

The tuple elements are enclosed within round brackets and indexed like the array. In order to access tuple elements, you have to use the index operator([]) of Python.

The elements of the tuple start from the index zero(0). So, if you want to access the first element of the tuple. You have to use the index operator([]) with zero(0) as its argument. Check the example given below accessing 1st and 3rd elements of the tuple.

Output

Goa
Bihar

The above example showing the 1st and 3rd tuple elements in the output. You can access a single element of the tuple using the above method. However, you also access more than one tuple elements using the slice operator.

To learn about the slice operator and access more tuple elements, read further.

Get Element With Slicing of Tuple in Python

Slicing is another method of Python to access more than one element of the tuple. Probably, the above method can be useful if you want to get an only single element. But this method gives you even more elements on a one-time use only.

You have to use the slicing operator([:]) of Python to access elements. There are two arguments you have to pass before and after the colon(:). The first argument is the starting index while the second argument is the end index.

Output

(4, ‘Bihar’, ‘Punjab’)

The above example prints the three elements starting from index 1st to 4th. The output contains the elements enclosed within the round brackets.

Loop Through Each Elements of Tuple in Python

If you want to iterate through each element of the tuple. You can use the Python loop which gives you all the individual elements. Get or print each element without any round brackets of the tuple. To get this, you have to use the below-given example with Python for the loop.

Output

Goa
4
Bihar
Punjab
19
81

The above example showing all the tuple elements prints in the output. Single element prints in every single row in the output.

Find Size of Tuple in Python

You can simply get the size of any tuple variable by using the Python len(). The len function takes the single argument as the tuple variable. If you want to print the length of the tuple variable, you have to use the below-given example.

Output

6

There are six elements in the tuple given in the above example. The above output showing that the tuple variable is of size 6. It counts both the string as well as the integer elements of the tuple.

Add, Update and Delete Elements of Tuple Using Python

A tuple variable of python is unchangeable. That means you cannot add, update and delete the elements of the tuple variable. But you can access or perform other above-given operations with the tuple variable.

If you perform various manipulations like add using the python function append() and insert(). The output will give the same number of elements of tuple without any change.

Also, if you use deletion operations with the tuple elements using the pop(), del() or remove(). It again gives you the same number of tuple elements without any change in the output.

Differences Between Tuple Variable and List Variable

There is the various difference between the tuple elements and list elements. These differences are given below:

  • Tuples are once created cannot be changeable while the list elements are changeable.
  • Tuple elements are enclosed within the round bracets. While the list elements are enclosed within the square brackets([]).
  • You can delete list elements with Python available functions. While you cannot delete tuple elements using the various delete functions of Python.

You may also like to read

Hope, you like this post of how to create a tuple variable in Python. If you have any query regarding the tutorial, please comment below.

Also tell me, which methods you are using to create your tuple variable.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.