Tuple
What is Tuple?
A tuple is a sequence of
immutable Python objects. Tuples are sequences, just like lists. The only
difference is that tuples can't be changed ie. tuples are immutable and tuples
use parentheses and lists use square brackets.
Creating a tuple is as simple as
putting different comma-separated values and optionally you can put these
comma-separated values between parentheses also. For example:
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c",
"d";
Tuples
are pretty easy to make. You give your tuple a name, then after that the list
of values it will carry. For example, the months of the year:
Code
Example 1 - creating a tuple
months = ('January','February','March','April','May','June',\
'July','August','September','October','November',' December')
Ø
Note that the '\' thingy at the end of the first
line carries over that line of code to the next line. It is useful way of
making big lines more readable.
Ø
Technically, you don't have to put those
parentheses there (the '(' and ')' thingies), but it stops Python from getting
things confused.
Ø
You may have
spaces after the commas if you feel it necessary - it doesn't really matter.
Python then organises those
values in a handy, numbered index - starting from zero, in the order that you
entered them in. It would be organised like this:
Table 1 - tuple
indicies
|
|
Index
|
Value
|
0
|
January
|
1
|
February
|
2
|
March
|
3
|
April
|
4
|
May
|
5
|
June
|
6
|
July
|
7
|
August
|
8
|
September
|
9
|
October
|
10
|
November
|
11
|
December
|
Basic Tuples Operations:
Tuples respond to the + and * operators much like
strings; they mean concatenation and repetition here too, except that the
result is a new tuple, not a string.
In fact, tuples respond to all of the general sequence
operations we used on strings in the prior chapter :
Python Expression
|
Results
|
Description
|
len((1, 2, 3))
|
3
|
Length
|
(1, 2, 3) + (4, 5, 6)
|
(1, 2, 3, 4, 5, 6)
|
Concatenation
|
['Hi!'] * 4
|
('Hi!', 'Hi!', 'Hi!', 'Hi!')
|
Repetition
|
3 in (1, 2, 3)
|
True
|
Membership
|
for x in (1, 2, 3): print x,
|
1 2 3
|
Iteration
|
Indexing, Slicing, and Matrixes:
Because tuples are sequences, indexing and slicing
work the same way for tuples as they do for strings. Assuming following input:
L = ('spam', 'Spam', 'SPAM!')
Python Expression
|
Results
|
Description
|
L[2]
|
'SPAM!'
|
Offsets start at zero
|
L[-2]
|
'Spam'
|
Negative: count from the right
|
L[1:]
|
['Spam', 'SPAM!']
|
Slicing fetches sections
|
No Enclosing Delimiters:
Any set of multiple objects, comma-separated, written without
identifying symbols, i.e., brackets for lists, parentheses for tuples, etc.,
default to tuples, as indicated in these short examples:
#!/usr/bin/python
print 'abc', -4.24e93, 18+6.6j, 'xyz';
x, y = 1, 2;
print "Value of x , y : ", x,y;
When the above code is executed, it produces following result:
abc -4.24e+93 (18+6.6j) xyz
Value of x , y : 1 2
Built-in Tuple Functions:
Python includes following tuple functions
SN
|
Function with Description
|
1
|
|
2
|
|
3
|
|
4
|
|
5
|
No comments:
Post a Comment