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 changedCreating Tuple
The tuple is created by allocating all necessary items inside the parentheses (), which can be separated by the comma signExample 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
Slicing in Tuple
>>>my_tuple = ('Angelina','Luna','Harry','John') >>>print(my_tuple) Angelina >>>print(my_tuple) Luna >>>print(my_tuple) John #taking nested tuple example >>>my_tuple = ('James','Richards',[1,2,3],('a','b','c')) >>>print(my_tuple) James >>>print(my_tuple) 2 >>>print(my_tuple) 3 >>>print(my_tuple) a #Negative indexing >>>print(my_tuple[-1]) ('a','b','c') >>>print(my_tuple[-4]) James
Slicing is the way to access a certain range of elements using the slicing operator ':'Example
Modifying a tuple
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:])
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 = 'z' print("After: ",my_tuple) #tuple can be reassigned my_tuple = ('P', 'y', 't', 'h', 'o', 'n') print("After re-assigning: ",my_tuple)
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)
Deleting a Tuple
(4, 5, 6, 6, 7, 8) ('Hello', 'Hello', 'Hello', 'Hello')
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.
Tuple Membership test
line 5, in "module" print(my_tuple) NameError: name 'my_tuple' is not defined
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)
True False False True
We can use for loop to iterate through a tuple.
friends = ['John','Penny','Richards','Albert'] for name in friends: print("Hello,",name)
Hello, John Hello, Penny Hello, Richards Hello, Albert