Tuples
Tuples
Tuples
A tuple is same as list, except that the set of elements is enclosed
in parentheses instead of square brackets.
A tuple is an immutable list. i.e. once a tuple has been created, you
can't add elements to a tuple or remove elements from the tuple.
But tuple can be converted into list and list can be converted in to
tuple.
Benefit of Tuple:
Tuples are faster than lists.
If the user wants to protect the data from accidental
changes, tuple can be used.
Tuples can be used as keys in dictionaries, while
lists can't.
Operations on Tuples:
1. Indexing
2. Slicing
3. Concatenation
4. Repetitions
5. Membership
6. Comparison
Tuple methods:
Tuple is immutable so changes cannot be done on the elements
of a tuple once it is assigned.
Tuple comparison
The Comparison operators work with tuples and other sequence.
Python starts by comparing the first element from each sequence. If they are
equal, it goes on to the next element, and so on, until it finds elements that
differ subsequent elements are not considered(even if they are really big)
EXAMPLE:
>>>(1,2,3)< (0,0,0)
False
>>>(1,2,3)<(1,2,4)
True
>>>(1,2)<(10,1)
True
>>>(10,20,30)<(10,1,200)
False
>>>(100,200,300)>(101,500)
False
Tuple Assignment:
Tuple assignment allows, variables on the left of an
assignment operator and values of tuple on the
right of the assignment operator.
Multiple assignment works by creating a tuple of
expressions from the right hand side, and a tuple of
targets from the left, and then matching each
expression to a target.
Because multiple assignments use tuples to work, it
is often termed tuple assignment.
Uses of Tuple assignment:
It is often useful to swap the values of two variables.
Example:
Swapping using temporary variable:
a=20
b=50
temp = a
a=b
b = temp
print("value after swapping is",a,b)
Swapping using tuple assignment:
a=20
b=50
(a,b)=(b,a)
print("value after swapping is",a,b)
Output:
Before using tuple assignment
(20,50)
After using tuple assignment
(50,20)
Another Example:
Swapping two tuples
a=(81,82,83)
b=(23,34,45)
Print(“Before using tuple assignment”)
Print(a)
Print(b)
a,b=b,a
Print(“After using tuple Assignment”)
Print(a)
Print(b)
Output:
>>>(a,b,c)=(1,2,3)
>>>print(a)
1
>>>print(b)
2
>>>print(c)
3
Tuple as return value:
A Tuple is a comma separated sequence of items.
It is created with or without ( ).
A function can return one value. if you want to
return more than one value from a function. we can
use tuple as return value.
Example1:
def div(a,b):
r=a%b
q=a//b
Output:
return(r,q) enter a value:4
a=eval(input("enter a value:")) enter b value:3
reminder: 1
b=eval(input("enter b value:"))
quotient: 1
r,q=div(a,b)
print("reminder:",r)
print("quotient:",q)
Example2:
def min_max(a):
small=min(a)
big=max(a)
return(small,big)
Output: smallest: 1
a=[1,2,3,4,6] biggest: 6
small,big=min_max(a)
print("smallest:",small)
print("biggest:",big)
Tuple as argument:
The parameter name that begins with * gathers
argument into a tuple.
Example:
def printall(*args):
print(args)
printall(2,3,'a')
Output:
(2, 3, 'a')