Tuple in Python

In this tutorial we will learn Tuple in Python. In last tutorial we learned about List. Tuple are same like List expect one difference that we cannot change elements of tuple after assigning them. In the case of list we know we can change List elements very easily. It mean you can say Tuple is sequence of immutable Python objects. Like List in Tuple we can add any number of elements and various kind of elements like(String, Integer, float, List).

Tuple in Python


Creating a Tuple in Python:

In list we use square brackets to create List. But in the case of Tuple we use parentheses. We can also create a Tuple without parentheses. Here we will learn about both ways to Create a Tuple in Python. Check this example program below.

Example

# Creating a tuple
first_tuple = ()

# Output: ()
print(first_tuple)  

# Initializing Tuple
first_tuple = (9, 8, 7)

# Output: (9, 8, 7) 
print(first_tuple) 

# Tuple with mixed datatypes
first_tuple = (9, "Owlbuddy", 5.5)

# Output: (9, "owlbuddy", 5.5) 
print(first_tuple) 


Creating Table without parentheses:

Example

# Creating a tuple without parentheses
first_tuple = 5, "owlbuddy",10.5

# Output: ()
print(first_tuple)  


In case you tuple have only one element then you have to put a comma at the end of element to use it as a tuple check this example below.

Example

first_tuple = ("owlbuddy")
print(type(first_tuple))  

# Creating a tuple
first_tuple = ("owlbuddy",)  
print(type(first_tuple))  

# Creating tuple without parentheses
first_tuple = "owlbuddy",
print(type(first_tuple))     



Accessing elements from Tuple:

We can access elements of Tuple using index number. Like List index number of Tuple elements start from 0. Here is an example to show how we can access elements of Tuple.

Example

# Creating a tuple
first_tuple = ('O','w','l','b','u','d','d','y')  

# output: O
print(first_tuple[0]) 

# output: u 
print(first_tuple[4])     

Negative Indexing:

Like negative indexing in List. We access elements of Tuple using negative indexing. The last element of Tuple would have negative index -1 and second last element have negative index -2.
Tuple in Python
Example

# Creating a tuple
first_tuple = ('O','w','l','b','u','d','d','y')  
# output: y
print(first_tuple[-1]) 
# output: u 
print(first_tuple[-4])     



Slicing of Tuple:

We can access a specific range of elements from Tuple using colon(:) operator. Here is an example to understanding slicing.

Example

# Creating a tuple
first_tuple = ('O','w','l','b','u','d','d','y') 
 
# output: ('l','b','b')
print(first_tuple[2:5]) 

# elements beginning to 2nd
# Output: ('O','w')
print(first_tuple[:-6])

# elements 5th to end
# Output: ('u','d','d','y')
print(first_tuple[4:])

# elements beginning to end
# Output: ('O','w','l','b','u','d','d','y')
print(first_tuple[:])   



Changing a Tuple:

We know Tuples are immutable. Mean we can not change elements after once they defined. But if elements of a Tuple are of mutable type like list. It mean we can change nested elements of Tuple. Here is an example for this.

Example

first_tuple = (10, 5, 6, [8, 2])

# TypeError: 'tuple' object does not support item assignment
first_tuple[2] = 10

# But if we change item of mutable element it will not show error
first_tuple[3][0] = 12  

# Output: (10, 5, 6, [12, 2])
print(first_tuple)



Deleting a Tuple:

We know Tuples are immutable it mean we can not change element of tuple once they assigned. we also can not delete elements from tuple. But there is a del keyword available in Python. We can use this del keyword to delete entire tuple. Check out this example.

Example

first_tuple = ('O','w','l','b','u','d','d','y')

# we can not delete elements of Tuple it will show an error
del first_tuple[3]

# we can an delete an entire Tuple
del first_tuple

# NameError: name 'first_tuple' is not defined
print(first_tuple)



Use of + and * Operator in Tuple:

Like Lists these two operators are also present in Tuple. we can use + operator to concatenate two Tuples and we can use * operator to repeat a Tuple a give number of times. Check this example

Example

first_tuple=(5,6,7)
second_tuple=(8,9,10)

# Output: (5,6,7,8,9,10)
print(first_tuple+second_tuple)

# Output: ('owlbuddy', 'owlbuddy', 'owlbuddy')
print(("owlbuddy",) * 3)



Some other Tuple Methods:

Method Description
count(x) Returns how many times a given element present in Tuple
index(x) Returns first index of given element in Tuple.


Tuple in PythonClick Here to Download Jupyter Notebook file of This Tutorial.


Share this tutorial with your friends on your social Media..

Tuple in Python Tuple in Python Tuple in Python Tuple in Python



Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.


Owlbuddy.com