Tuesday, January 12, 2021

Python Tuples

 Tuples are used to store multiple items in a single variable. It is like list in Python with few differences. A tuple is a collection which is ordered and unchangeable.

Tuples are written using first brackets.

mytuple = ("apple", "banana", "cherry")


Accessing Tuple Elements

Like list, indexes are automatically generated in tuple and index starts from 0. All accessing techniques are same as list. Adding elements, updating elements in tuple are same as list. 

We can multiply the elements in tuple using the following statements.


We can use the * operator:

mytuple = ("Assam", "Banaras", "Chennai")

ptuple = fruits * 2


ptuple will be like ("Assam", "Banaras", "Chennai","Assam", "Banaras", "Chennai")

Tuple Methods

Python has two built-in methods that you can use on tuples.


count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of where it was found

1 comment:

Subscribe via email

Enter your email address:

Delivered by FeedBurner