# Tuples in python

## Python- Tuples

• Tuple is a sequence of immutable objects (Values in the tuple cannot be changed)
• Elements in the tuple are enclosed in round brackets.

### Creating Tuples

``````tup1=( ) creates a empty tuple

Tup1 = (val 1,val 2,…..) where val (or values) can be an integer, a floating number, a character, or a string.``````
• Any set of multiple comma-separated values written without an identifying symbol like brackets [] or parenthesis (), is treated as tuples by default.
• If we want to create a tuple with a single element, then we must add a comma after the element.
``````1] Tup=(10,)
print(type(Tup))

Output:
<type 'tuple' >``````
``````

2] Tup1=(10)
print(type(Tup))

Output:
<type 'int' >
<type 'tuple' >``````

There are some built in functions which return tuple

• Eg:divmod()
• divmod() function returns two values – quotient as well as the remainder after performing the divide operation.
• a,b=divmod(100,3)
print(“Quotient = “,a)#a is 33
print(“Remainder= “,b)#b is 1

ACCESSING VALUES IN TUPLE

• We can use slice, concatenate etc on a tuple
``````tup1=(1,2,3,4,5)
print("tup[2:4] = ",tup1[2:4])
print("tup[:4] = ",tup1[:4])
print("tup[3:] = ",tup1[3:])
print("tup[:] = ",tup1[:])
Output:
tup[2:4] = (3,4)
tup[:4] = (1, 2, 3, 4)
tup[3:] = (3,4)
tup[:] = (1, 2, 3, 4, 5)``````
• Concatenation, repetition can be done on tuples
• New tuple should be created when we need to change the exisiting tuple

## Some tuple operations

• Length of the tuple can be finded using len()
• Ex: t=(1,2,3,4,5) len(t) gives 5

• Tuples can be concatenated using + operator
• ex: t=(1,2,3) p=(4,5,6)
• g=t+p
• g is (1,2,3,4,5,6)

• Membership operator even works tuples (in and not in)
• max(), min are used to find maximmum and minimmum element of the tuples
• count() is used to find no of times a element repeated in the tuple.
• index() is used to know the index of specified element in the tuple.

#### zip( ) function in python

``````tup=(1,2,3,4)
lis=[‘a’,’b’,’c’,’d’,’e’]
print(list((zip(tup,lis))) #[(1,’a’),(2,’b’),(3,’c’),(4,’d’)]``````

We will be happy to hear your thoughts Hello!👋