Python Tuple Data Type
 

Python Tuple Data Type

 

Python tuple data type is similar to the Python list in various ways. Like lists, you can use tuples to store different types of data. The only thing that differs is that tuples are immutable that means once a tuple is created you can’t update data inside it.

The items or elements of a tuple are generally enclosed within the parentheses but it is not always necessary.

In this article, you will learn about Python tuples.

Examples of Python tuples

The following are some of the examples of tuples –

# Example of empty tuple
mytuple=()
# Tuple contains only numbers
numbers=(1,5,8,9,7)
# Given tuple contains string elements
days=('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
# Given tuple has mixed elements
mixed_tuple = (1, 2, 'Hi', 3,'abc')

Creating tuple in Python

A tuple is created when you assign elements to a variable. Using parentheses is optional but this is a good practice to use them.

Creating empty tuple

my_tuple = ()
print(my_tuple)

Creating tuple without parentheses

my_tuple = 1, 2, 'Hi'
print(my_tuple)

Creating tuple with parentheses

my_tuple = (1, 2, 3, 'Hi')
print(my_tuple)

Creating a tuple with a single element

While creating a tuple that has a single element always use a comma otherwise it will not be considered as tuple type.

my_tuple= ("Hi",)
my_tuple1=("Hi")
print(type(my_tuple))
print(type(my_tuple1))

Now when you execute this program you will see –

Tuple unpacking

You can unpack or extract the elements of a tuple into variables. Remember always to use variables equal to the number of tuple elements otherwise, you will be prompted with an error.

For example –

x=(5,8,9)
a,b,c=x
print(a)
print(b)
print(c)

Here 5, 8, 9 will be assigned to variables a, b, c respectively.

Looping in tuple

Using in operator with for loop you can loop in a tuple. By using len() function you can find the length of a tuple then you can use a loop that iterates from the index 0 to the length-1.

x=(1, 2, 3, 4, 5) 
for i in x:
     print(i)

OR use while loop, as given below, to iterate through the tuple –

x=(1, 2, 3, 4, 5)
i=0
while i < len(x):
     print(x[i])
     i=i+1

Accessing tuple elements

You can access the tuple element using positive or negative indexing.

For example –

x=(1,5,7,6,3)

To print the element at index 1 we will use –

print(x[1])

OR print the element at index -1 with the following statement –

print(x[-1])

If you want to access a range of items of a tuple then use slicing. For example, to print elements from index 1 to 3 use –

print(x[1:4])

Here, the tuple element at index 4 will be excluded. When you run this program you will see the following output –

Tuple methods

Python tuples are immutable so there is no method to add or remove elements in the tuple. It provides the following two methods –

Using count() method –

You can use this method to know how many times an element occurs in a tuple. For example –

x=(1,5,7,6,3,5,3,5)
print(x.count(5))

You can see the output in the given image –

Using index() method –

The index method returns the index of an element if an element occurs multiple times it returns the index of element which occurs first.

In the above example you can check the index of an element for example you can check 5 or 7 as given in the image –

Conclusion

Since tuples are immutable, operations such as iteration through a tuple are faster than with a list. So you get a performance boost.

Now if you have a query on the given topic then write us in the comments below.

 

Previous Python List Data Type
Next Python Dictionary Data Type
 

About author

You might also like

Programming 0 Comments

Using Python pass keyword

The Python pass keyword which is a statement in itself basically does nothing. It can be used as a placeholder for future code. As empty code is not allowed in

Python Programming 0 Comments

Python Boolean Data Type

Boolean or bool is a built-in data type in the python programming language. It can have two possible values –   True  False These are equivalent to 1 and 0

Programming 0 Comments

Python Frozensets

Python frozensets are same as the set data type in Python the only difference is that frozensets are immutable which means you can’t add or remove the elements once it

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply