Explain Tuples in Python with example

OR Is tuple mutable ? Demonstrate any two method of tuple


No tuple is not mutable(changeble)
A tuple in Python is similar to a list.
we cannot change the elements of a tuple once it is assigned
A Tuple is a collection of Python objects separated by commas.
Creating Tuples Program:
tup = ‘python’, ‘geeks’ print(tup)
Output: (‘python’, ‘geeks’)

Tuple Method: count():

returns occurrences of element in a tuple
count() method searches the given element in a tuple and returns how many times the element has occurred in it.
Syntax​: tuple.count(element)
Program​:

vowels = ('a', 'e', 'i', 'o', 'i', 'o', 'e', 'i', 'u') 
x = vowels.count('i')
print('The count of i is:', x)

Output​: The count of i is: 3

Tuple Method: Len():

The len() function returns the number of items (length) in an object.

Syntax​: len(s)

Program​:

est = [1, 2, 3] print(‘Length of test is: ’) 
print(len(testList))

Output​: Length of test is: 3

To Learn more about Python, Click Here

Click here To Follow on pinterest

Leave a Comment

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