Loading...

Python tutorial

Tuples in python

Tuple is an ordered collection of items that can not modeified.

Tuples are indicated by parentheses

num=(1,2,3)
print(num)

Output:

(1, 2, 3)

Tuples can be accessed by using indexes

num=(1,2,3)

print(num[0])
print(num[-1])
print(num[:2])

Output:

1
3
(1, 2)

You can also use loop for a tuple:

num=(1,2,3)

for x in num:
    print(x)

Output:

1
2
3

NOTE: if you try to modify an element in a tuple, you will get an error.Tuples only usefull when you have a collection of items that can not be change throughout the life of your program