ScanSkill

Tuple

The tuple is used to store multiple items sequentially in one variable. It is similar to list but unlike list it is immutable.

Syntax

tuple_name = (item1, item2, item3)
  • Accessing items from the tuple
tuple_name[index]

Note:

  • The tuple is immutable, which means its values at the indices cannot be changed unless there is a value swap.
  • The array index range is from 0 to size minus 1 and negative indices start at the end of the array and work backward.
  • Tuple can be combined using the addition operator.
  • When declaring a tuple, the parentheses are optional.

Example

>>> cities = ("Kathmandu", "Beni", "Pokhara")
>>> print (cities[0])
Kathmandu

>>> print(cities[1])
Beni