Tuple is a sequence of values. It is similar to list but there lies difference between tuple and list
Tuples AU : May-19, Dec.-19, Marks 16
Tuple
is a sequence of values. It is similar to list but there lies difference
between tuple and list
Difference
between Tuple and List
Tuple
1.
Tuple use parenthesis
2.
Tuples can not be change
List
1.
List use square brackets
2.
Lists can be changed.
Tuple
is said to immutable. That means once created we can not change the
tuple.
Examples
of tuples
T1
= (10,20,30,40)
T2
= (-a', 'b','c','d')
T3
= (‘A’,10,20)
T4
= ('aaa','bbb’,30,40)
The
empty tuple can be created as
T1
= ( )
How
to Write Tuple ?
Tuple
is written within the parenthesis. Even if the tuple contains a single value,
the comma is used as a separator. For example -
T1
= (10)
The
tuple index starts at 0. For example
>>>
t1 = (10,20, 'AAA','BBB')
>>>
print(t1[0])
10
>>> print(t1[1:31)
(20,
'AAA')
>>>
•
We can create a tuple by using assignment operator. Multiple assignments are possible
at a time using tuple assignment. For example –
>>>
a,b=10,20
>>>
print(a)
10
>>>
print(b)
20
>>>
•
Here the left side is a tuple of variables; the right side is a tuple of
expressions.
•
Each value is assigned to its respective variable.
•
All the expressions on the right side are evaluated before any of the
assignments.
•
Note that the number of variables on the left and right have to be the same.
•
We can also separate out the values in the expression in the tuple by using the
split function. For example
>>>
student = 'AAA, 123'
>>>
name,roll = student.split(',').
>>>
print(name)
AAA
>>>
print(roll)
123
>>>
•
In above example, the expression is split at comma and the two separated
values are collected in two different variables. We have name and roll
variables in which the corresponding values are stored.
Normally
function return a single value, but if the value is tuple, then multiple values
can be returned.
For
example -
Test.py
def
student_Info():
name='AAA
roll=101
return
name,roll
Output
In
python, it is possible to have variable number of arguments. In that case the
parameter name begins with *. Then these variable number of arguments are
gathered into a tuple. For example -
Test.py
def
student_Info(*args):
print(args)
Output
In
above program * accepts the variable number of arguments and inside the
function definition, these arguments are printed.
Review Questions
1. Can function return
tuples ? If yes give example. AU : Dec.-19, Marks 2
2. Demonstrate with
code the various operations that can be performed on tuples. AU : May-19, Marks 16
3. Compare and
contrast tuples and lists in Python. AU : Dec.-19, Marks 8
4. What is tuple in
Python ? How does it differ from list ? AU : Dec.-19, Marks 8
Problem Solving and Python Programming: UNIT IV: Lists, Tuples, Dictionaries : Tag: Engineering Python : Example Program | Python Programming - Tuples
Problem Solving and Python Programming
GE3151 1st Semester | 2021 Regulation | 1st Semester Common to all Dept 2021 Regulation