Tuple Manipulation in Python 2024
Tuple Manipulation in Python 2024
Introduction
Tuples are used to store multiple items in a single
variable.
The Python tuples are sequences that are used to
store a tuple of values of any type. such as
integer, float, string, list or even a tuple.
Python tuples are immutable i.e., you cannot
change the elements of a tuple in place.
Elements of a tuple are enclosed in parenthesis()
(round brackets) and are separated by commas.
Creating a Tuple
Syntax:
Variable_Name =(elements,.,.,….)
(Tuple_Name)
Example: T=(1,2,3,4,5)
Creating a Tuple
T= ( ) empty tuple
T=( 1, 2, 3 ) tuple of integers
T=( ‘a’, ‘b’, ‘c’ ) tuple of characters
T=( ‘one’, ‘two’, ‘three’ ) tuple of strings
T=(1,”RAM”,5.5) tuple of mixed data types
T = tuple(<sequence>)
where <sequence> can be any kind of sequence
object including strings, lists and tuples.
Python creates the individual elements of the
tuple from the individual elements of passed
sequence.
T = tuple(‘hello’)
print(T )
( ‘h’, ‘e’, ‘l’, ‘l’, ‘o’ )
Empty Tuple
a=()
a=a+(33516140,44738484)
print(a)
Nested Tuples
List Tuple
Mutable (we can Change the elements in the Immutable (we Cannot change elements in
list) the tuple)
Elements are enclosed in square brackets [] enclosed in parenthesis ()
[1,2,3] (1,2,3)
We can use the method (like append(), We can not use methods (like append(),
insert(), extend() ) to change the elements insert(), extend() ) to change the element
in the list. in the same Tuple.
st=((101,“Irfan",98),(102,“sai",95),(103,“amy",87),(104,“
ravin",79))
print("S_No"," Roll_No"," Name"," Marks")
for i in range(0,len(st)):
print((i+1),'\t',st[i][0],'\t',st[i][1],'\t',st[i][2])
Output
S_No Roll_No Name Marks
1 101 Irfan 98
2 102 sai 95
3 103 amy 87
4 104 shaji 79
Lab Exercise