Table of Contents
Python Tuple data type
- The tuple is a collection that is ordered, unchangeable, and Allows duplicate members.
- The tuples are written with round brackets.
- The round brackets symbol is ( )
Once a tuple is created, we cannot change its values because tuple is unchangeable.
Example
Create a Tuple
t=("amit", "sumit", "ravi")
print(t)
Output
(‘amit’, ‘sumit’, ‘ravi’)
How to Access Tuple Items
- To access tuple items by using index numbers, inside square brackets.
Example
Create a Tuple
t=("amit", "sumit", "ravi")
print(t[2])
Output
ravi
To access tuple of a member by using for loop.
Example
Create a Tuple
t=("amit", "sumit", "ravi")
for i in t:
print(i)
Output
amit
sumit
ravi
sumit
ravi
Check if control statement in tuple
- Using in keywords to determine if a specified item is present in a tuple.
Example
Create a Tuple
t=("amit", "sumit", "ravi")
if "sumit" in t:
print("Yes, 'sumit' is found in tuple")
else:
print("No, 'sumit' is found in tuple")
Output
Yes, ‘sumit’ is found in tuple