Python Tuples

Updated on Jan 07, 2020


In Python Tuples are very similar to list but once a tuple is created, you cannot add, delete, replace, reorder elements.

note:

Tuples are immutable.

Creating a tuple #


1
2
3
4
5
6
7
>>> t1 = () # creates an empty tuple with no data
>>>
>>> t2 = (11,22,33)
>>>
>>> t3 = tuple([1,2,3,4,4]) # tuple from array
>>>
>>> t4 = tuple("abc") # tuple from string

Tuples functions #


Functions like max(), min(), len() and sum() can also be used with tuples.

1
2
3
4
5
6
7
8
9
>>> t1 = (1, 12, 55, 12, 81)
>>> min(t1)
1
>>> max(t1)
81
>>> sum(t1)
161
>>> len(t1)
5

Iterating through tuples #


Tuples are iterable using for loop [ Learn more about for loop here ] .

1
2
3
4
>>> t = (11,22,33,44,55)
>>> for i in t:
...     print(i, end=" ")
>>> 11 22 33 44 55

Slicing tuples #


Slicing operators works same in tuples as in list and string.

1
2
3
>>> t = (11,22,33,44,55)
>>> t[0:2]
(11,22)

in and not in operator #


You can use in and not in operators to check existence of item in tuples as follows.

1
2
3
4
5
>>> t = (11,22,33,44,55)
>>> 22 in t
True
>>> 22 not in t
False

In next chapter we will learn about python data type conversion.


Other Tutorials (Sponsors)