
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
- Ordered Lists
- Cannot change values after declaring elements
- Allow duplicate members
- Uses Parentheses
( )
- 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)
Whats Next – Python Dictionary