What are Python Tuple and How to use it?
Photo by Element5 Digital from Pexels

Python Tuples are often thought of as Read-Only Lists. Tuple can store different datatype elements. It contains things separated by commas and embowered among parentheses ( ( ) ). Tuple is a collection of data which is ordered and unchangeable. Similar to List, Python Tuple also allows duplicate members.

Features of Python Tuples

  1. Ordered Lists
  2. Cannot change values after declaring elements
  3. Allow duplicate members
  4. Uses Parentheses ( )
  5. Can store different datatypes

Declaring Tuples in Python

tuple1 = ("Study", 123, 1.25, "Gyaan", 456)
tuple2 = ('Huzaif', 789)

Note – The plus (+) sign is the concatenation operator and the asterisk (*) is the repetition operator. For example −

tuple1 = ("Study", 123, 1.25, "Gyaan", 456)
tuple2 = ('Huzaif', 789)

print(tuple1)
print(tuple1[1])
print(tuple1[0:3])
print(tuple1[2:])
print(tuple2*2)
print(tuple1+tuple2)

It will produce output like this

>>> print(tuple1)
 ('Study', 123, 1.25, 'Gyaan', 456)
>>> print(tuple1[1])
 123
>>> print(tuple1[0:3])
 ('Study', 123, 1.25)
>>> print(tuple1[2:])
 (1.25, 'Gyaan', 456)
>>> print(tuple2*2)
 ('Huzaif', 789, 'Huzaif', 789)
>>> print(tuple1+tuple2)
 ('Study', 123, 1.25, 'Gyaan', 456, 'Huzaif', 789) 
Python Tuple Video Tutorial

Whats Next – Python Dictionary