/    /  Python – Tuples

Python – Tuples:

Tuples are generally used to store the heterogeneous data which immutable. Even Tuple looks like Lists but Lists are mutable.

To create a tuple, we need to use the comma which separates the values enclosed parentheses.

Example:

# Creating an empty tuple

>> tup1=()         

>>> tup1
()
>>> tup1=(10)

>>> tup1
10
>>> tup1=(10,20,30);

>>> tup1
(10, 20, 30)
>>> tup1=tuple([1,1,2,2,3,3])

>>> tup1
(1, 1, 2, 2, 3, 3)
>>> tup1=("tuple")

>>> tup1
'tuple'

Using Functions with Tuples:

>>> tup1=(10,20,30);

>>> max(tup1)
30
>>> min(tup1)
10
>>>len(tup1)
3

Operators with Tuples:

>>>20 in tup1
True
>>> 30 not in tup1
False

Slicing in Tuples:

>> tup1[0:4]

(10, 20, 30)

>>> tup1[0:1]

(10,)

>>> tup1[0:2]

(10, 20)