Tuple in python is written in “( )” bracket and are immutable. It can’t be modified as list.

a=('apple','orange','grapes','apple')
type(a)
'Tuple'

To get first element of tuple we need to write name of tuple i.e a in this case inside square bracket enter zero(0).Â

a[0]

Output:’apple’

It will return first element in tuple in python

 

Â

Methods of Tuple in Python:

It has two functions that can be performed

Tuple.count:

It will return number of occurrences of value.

Syntax: a.count(value)

value:It is element present in tuple which occurrence needs to find.

a.count('apple')
Output:2

Tuple.index:

It returns first index of value and return value error if value is not present in tuple.

Syntax of Tuple in Python:

a.index(value, start, stop)

Value: It is element which index is to find.

Start: Starting number from which count should start by default it begins from 0th index position.

stop: Ending number of index.

a.index('orange')
Output:1
a.index('apple',1)
Output:3
a.index('apple')
Output:0

As shown in code when we enter start value it started from index one and went up to last index then outputs index of value.

By SC