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 items in the tuple by passing index between [].

Example:

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

Output:

8

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

Example:

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

Output:

2

You get the index of the first occurrence of a specific item by using the 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

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments