Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more
Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c
To make everything easy: We refer CPython as Python
In order to store multiple items in a single variable, the concept of a tuple is introduced in Python.
The tuple is pretty much similar to a list but it differs when it comes to changing the elements once it has been assigned
In the list, the elements of a certain list can be changed. In the case of a tuple, the elements cannot be changed
Creating TupleThe tuple is created by allocating all necessary items inside the parentheses (), which can be separated by the comma sign
Example of Tuple
>>>empty_tuple = ()
>>>print(empty_tuple)
()
>>>integer_tuple = (9,8,7)
>>>print (integer_tuple)
(9,8,7)
>>>string_tuple = ("Jack", "Angelina", "Mary")
>>>print (string_tuple)
'Jack', 'Angelina', 'Mary'
>>>nested_tuple=("Django", 2, 0, 5, 4)
>>>print(nested_tuple)
'Django',2,0,5,4
nested_tuple=("Django", (2, 0, 5, 4))
print(nested_tuple)
'Django', (2,0,5,4)
Also in a tuple, the index starts from 0. We can access required elements using indexing. We can access the element from the tuple using the [] operator.
We use nested indexing to access nested tuple elements.
Example
>>>my_tuple = ('Angelina','Luna','Harry','John')
>>>print(my_tuple[0])
Angelina
>>>print(my_tuple[1])
Luna
>>>print(my_tuple[3])
John
#taking nested tuple example
>>>my_tuple = ('James','Richards',[1,2,3],('a','b','c'))
>>>print(my_tuple[0])
James
>>>print(my_tuple[2][1])
2
>>>print(my_tuple[2][2])
3
>>>print(my_tuple[3][1])
a
#Negative indexing
>>>print(my_tuple[-1])
('a','b','c')
>>>print(my_tuple[-4])
James
Slicing in Tuple
Slicing is the way to access a certain range of elements using the slicing operator ':'
Example
my_tuple = ['P', 'y', 't', 'h', 'o', 'n', 'T', 'h', 'e', 'R', 'i', 'g', 'h', 't', 'W', 'a', 'y']
#Beginning to end of a tuple
print(my_tuple[:])
#from 3rd to 6th
print(my_tuple[2:6])
#beginning to 5th
print(my_tuple[:-11])
#7th to end
print(my_tuple[6:])
Modifying a tuple
Tuple is an immutable datatype. That means modifying a tuple element is not possible. However, if the tuple contains mutable data types like lists, we can modify the item inside that list.
Example
my_tuple = (1,2,3,['a','b','c'],4,5)
print("Before: ",my_tuple)
my_tuple[3][0] = 'z'
print("After: ",my_tuple)
#tuple can be reassigned
my_tuple = ('P', 'y', 't', 'h', 'o', 'n')
print("After re-assigning: ",my_tuple)
Output
Before: (1, 2, 3, ['a', 'b', 'c'], 4, 5)
After: (1, 2, 3, ['z', 'b', 'c'], 4, 5)
After re-assigning: ('P', 'y', 't', 'h', 'o', 'n')
We can concatenate two tuples using the '+' operator. The '*' operator can be used to repeat a particular element.
my_tuple1 = (4,5,6)
my_tuple2= (6,7,8)
print(my_tuple1+my_tuple2)
my_tuple = (('Hello',)*4)
print(my_tuple)
Output
(4, 5, 6, 6, 7, 8)
('Hello', 'Hello', 'Hello', 'Hello')
Deleting a Tuple
As tuple is immutable, single elements of the tuple cannot be deleted. So, deleting a tuple entirely is possible as follows,
my_tuple = (1,2,3,4,5)
del my_tuple
print(my_tuple) #gives an error as the tuple is deleted entirely.
Output
line 5, in "module"
print(my_tuple)
NameError: name 'my_tuple' is not defined
Tuple Membership test
We can test whether a particular item is a member of a tuple or not or simply we can check the availability of the item in a particular tuple. This is done using the membership operator 'in'
fav_language = ['p','y','t','h','o','n']
#in operator
print('p' in fav_language)
print('z' in fav_language)
#not in operator
print('p' not in fav_language)
print('z' not in fav_language)
Output
True
False
False
True
Tuple Iteration
We can use for loop to iterate through a tuple.
friends = ['John','Penny','Richards','Albert']
for name in friends:
print("Hello,",name)
Output
Hello, John
Hello, Penny
Hello, Richards
Hello, Albert