Python Tuple
Python Tuple is a collection which is similar to Python Tuple, it is ordered and unchangeable. This section of Python tutorial will show all the operation of Tuple.
Objectives
-
Python Tuple Syntax
-
Create and assign value in Tuple
-
Python Tuple Access elements
-
Python Tuple all methods and it’s task
Python Tuple Syntax
tuple_data = (1, 2.5, "name")
- Here
-
-
Once a set is created, we cannot change, add, remove its items.
-
Allow Duplicates
-
Ordered
-
Create and assign value in Python Tuple
# Define a tuple
tuple_data = ()
# Assign value to a tuple
tuple_data = ("Touhid Mia", 28, True, 28.5)
Python Tuple Access elements
This part you are going to learn access tuple elements using various way, let’s jump into the codes.
Access element by index
Code
# Access element by index
tuple_data = ("Touhid Mia", 28, True, 28.5)
name = tuple_data[0]
print(name)
Output
Touhid Mia
Process finished with exit code 0
Access element by negative index
Code
# Access element by negative index
tuple_data = ("Touhid Mia", 28, True, 28.5, True)
last_item = tuple_data[-1]
print(last_item)
Output
True
Process finished with exit code 0
Note : Negative index means start from end of the list
Access multiple element by range of index
Code
# Access multiple element by range of index
tuple_data = ("Touhid Mia", 28, True, 28.5, True)
multiple_item = tuple_data[1:3] # [start:end]
print(multiple_item)
Output
(28, True)
Process finished with exit code 0
Note : Range syntax is [start_index : end_index]
Access multiple element by range of index (without end)
Code
# Access multiple element by range of index (without end)
tuple_data = ("Touhid Mia", 28, True, 28.5, True)
multiple_item = tuple_data[1:]
print(multiple_item)
Output
(28, True, 28.5, True)
Process finished with exit code 0
Note : You know that, range syntax is [start_index : end_index] , if end_index not specified it will pick value start_index to last index
Access multiple element by range of index (without start)
Code
# Access multiple element by range of index (without start)
tuple_data = ("Touhid Mia", 28, True, 28.5, True)
multiple_item = tuple_data[:3]
print(multiple_item)
Output
('Touhid Mia', 28, True)
Process finished with exit code 0
Note : If you not specify start_index then it will start from beginning
Access multiple element by range of negative index
Code
# Access multiple element by range of negative index
tuple_data = ("Touhid Mia", 28, True, 28.5, True)
multiple_item = tuple_data[-3:-1]
print(multiple_item)
Output
(True, 28.5)
Process finished with exit code 0
Check list value is exist on it
Code
# Check list value is exist on it
tuple_data = ("Touhid Mia", 28, True, 28.5, True)
if "Bangladesh" in tuple_data:
print("Yes key exist")
else:
print("Key not exist")
Output
Key not exist
Process finished with exit code 0
Print each element of list by loop
Code
# Declare Tuple for access data element
tuple_data = ("Touhid Mia", 28, True, 28.5, True)
# Print each element of list by loop
for item in tuple_data:
print("Item : " + str(item))
Output
Item : Touhid Mia
Item : 28
Item : True
Item : 28.5
Item : True
Process finished with exit code 0
Python Tuple all methods and it’s task
-
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