Tuples in python

Tuples are used to store a list of items. Tuples are defined using parenthesis. Tuples are immutable. This means that tuples cannot be modified. Only information can be accessed about tuples.

Example:

numbers = (4, 8, 3, 7)
print(numbers)

Output:

(4, 8, 3, 7)

Accessing the tuples:

You can access item in tuple by passing index between [].

Example:

numbers = (4, 8, 3, 7)
print(numbers[1])

Output:

8

You can get occurrence of specific item from tuple by using count() method.

Example:

numbers = (4, 8, 3, 7, 4)
print(numbers.count(4))

Output:

2

You get index of first occurrence of specific item by using index() method.

Example:

numbers = (4, 8, 3, 7, 4)
print(numbers.index(4))

Output:

0

Check out Recommended Posts:

Lists in python

Dictionary in python

Functions in python

Leave a Reply

Your email address will not be published. Required fields are marked *