Tuples
are used for grouping data.A tuple is an immutable list, i.e. a tuple cannot be
changed in any way once it has been created. A tuple is defined analogously to
lists, except that the set of elements is enclosed in parentheses instead of
square brackets. The rules for indices are the same as for lists. Once a tuple
has been created, you can't add elements to a tuple or remove elements from a tuple.
Where is the benefit of tuples?
Where is the benefit of tuples?
Tuples
are faster than lists.
If you know that
some data doesn't have to be changed, you should use tuples instead of lists,
because this protects your data against accidental changes.
Tuples
can be used as keys in dictionaries, while lists can't.
The
following example shows how to define a tuple and how to access a tuple.
Furthermore
we can see that we raise an error, if we try to assign a new value to an
element of a tuple:
Tuple creation
>>>
t = ("tuples", "are", "immutable")
>>>
t[0]
'tuples'
>>>
t[0]="assignments to elements are not possible"
Traceback
(most recent call last):
File "<stdin>", line 1, in
<module>
TypeError:
'tuple' object does not support item assignment
To
create a tuple with a single element, we have to include the final comma:
>>>
t1 = (’a’,)
>>>
type(t1)
<type
’tuple’>
Without
the comma, Python treats (’a’) as a string in parentheses:
>>>
t2 = (’a’)
>>>
type(t2)
<type
’str’>
This
will create an empty tuple
>>>
t=()
>>>
type(t)
<type
'tuple'>
>>>
t=tuple()
>>>
type(t)
<type
'tuple'>
This
will add a new element to the Tuple
>>>t=(10,20,30)
>>>t=t+(40,)
>>>t
(10,20,30,40)
Tuple operations are similar to list
Example
>>>a=(10,20,30)
>>>b=(40,50)
>>>a+b
(10,20,30,40,50)
>>>b*2
(40,50,40,50)
>>>10
in a
True
>>>100
not in a
True
>>>c=a+(30,)
>>>c
(10,20,30,30)
Tuple Functions
>>>min(a)
10
>>>max(a)
30
>>>sum(a)
60
>>>len(a)
3
>>>a.index(20)
1
>>>c.count(30)
2
cmp( )
This is used to check whether the given tuples are same or not. If
both are same, it will
return ‘zero’, otherwise return 1 or -1. If the first tuple is
big, then it will return 1,
otherwise return -1.
>>>a=(10,20)
>>>b=(10,20)
>>>cmp(a,b)
0
>>>a=(100,200)
>>>b=(10,20)
>>>cmp(a,b)
1
>>>cmp(b,a)
-1
since the tuples are immutable you cannot sort the tuple.However
you can use the sequence function sorted() to create a new sorted tuple.
>>>sorted(a)
Tuple packing and unpacking
tuples are used to group data. In tuple packing, the
values on the right are ‘packed’ together in a tuple:
>>>stud=(101,’shilpa’,’kayamkulam’,690535)
In tuple unpacking, the values in a tuple on the right are
‘unpacked’ into the variables/names on the left:
>>>(rno,name,place,pin)=stud
>>>
rno
101
>>>
name
'shilpa'
>>>
place
'kayamkulam'
>>>
pin
690535
Tuple assignments
Once in a while, it is useful to swap the values of two
variables. With conventional assignment statements, we have to use a temporary
variable. For example, to swap a and b:
temp
= a
a
= b
b
= temp
|
Tuple
assignment solves this problem neatly:
(a, b) = (b, a)
|
The left side is a tuple of variables; the right side
is a tuple of values. Each value is assigned to its respective variable. All
the expressions on the right side are evaluated before any of the assignments.
This feature makes tuple assignment quite versatile.
Naturally, the number of variables on the left and the
number of values on the right have to be the same:
We can swap values of two tuples
>>>t1=(10,20,30)
>>>t2=(100,200)
>>>t2,t1=t1,t2
>>>t1
(100,200)
>>>t2
(10,20,30)
Converting tuples into lists and lists into
tuples
>>>L=[10,20,30,40]
>>>T=tuple(L)
>>>T
(10,20,30,40)
>>>
T=(10,20,30)
>>>
L=list(T)
>>>
L
[10,
20, 30]
Sample Program
Program to read numbers and find minimum,
maximum and sum using Tuple
n=input("Enter
how many numbers....")
print
"Enter {} numbers".format(n)
t=tuple()
for i
in range(n):
x=input()
t=t+(x,)
print
"minimum=",min(t)
print
"maximum=",max(t)
print
"sum=",sum(t)
Now you can try following programs using
tuple
1.Read
‘n’ numbers and find their sum,average,min,max and also print a sorted list.
2.Read
two tuples and exchange their values.
3.Find
the variance and standard deviation of ‘n’ numbers.
4.Check
the number of occurrence of an element in a tuple.
5.Find
the positions of occurrences(indices) of an element in a tuple.
Comments
Post a Comment