Notes
Notes
TUPLES
CREATING TUPLES
Tuples can be created by writing comma separated elements/values with or without parenthesis.
EXAMPLES
>>>tup1=('Sunday', 'Monday', 10, 20)
>>>tup2=('a', 'b', 'c', 'd', 'e')
>>> tup3=(1,2,3,4,5,6,7,8,9)
>>> tup4=(10,) # tuple with one element
>>>tup5=() # empty tuple
NOTE:
Here tup4 is a tuple with one single value. The final comma after single value is mandatory
otherwise it is not considered as tuple.
>>> tup6= (100)
>>> type(tup6)
<class 'int'>
tuple() function
SYNTAX
tuple(iterable)
Here, the parameter iterable is optional. The iterable can be a string, list or dictionary. If the
iterable is not passed, empty tuple is created .
EXAMPLES
>>>tup7=tuple()
>>>print(tup7)
()
#creating a tuple from a string
>>>tup8=tuple(“Hello”)
>>>print(tup8)
>>>tup9=tuple([10,20,30])
>>>print(tup9)
(10,20,30)
#creating a tuple from a dictionary
>>>tup10=tuple({1:10,2:20})
>>>print(tup10)
(1, 2)
EXAMPLE
>>> t=tuple()
>>> t=(10,)*10
>>> print(t)
OUTPUT
(10, 10, 10, 10, 10, 10, 10, 10, 10, 10)
INDEXING METHOD
To access an element, present at a particular index square brackets along with the desired index
number is given.
SYNTAX
tuple-name [ index-number]
EXAMPLE 1
>>> t=('A', 'B', 'C')
>>> print(t[2])
OUTPUT
C
EXAMPLE 2
>>> t=('A', 'B', 'C')
>>> print(t[-2])
OUTPUT
B
EXAMPLE 3
>>> t=('A', 'B', 'C')
>>> print(t[2*1])
OUTPUT
C
TUPLE SLICING
Slicing is used to retrieve a subset of values. A slice of a tuple is basically its sub-tuple.
SYNTAX
tuple-name [start: stop: step]
where
NOTE:
If you omit first index, slice starts from “0‟ and omitting of stop will take it to end.
Default value of step is 1.
It includes the first element but excludes the last element.
EXAMPLE
z1[:] (10,20,30,40,50,60)
z1[2:5] (30,40,50)
z1[:5] (10,20,30,40,50)
z1[3:] (40,50,60)
z1[::2] (10,30,50) (from beginning till end in step 2)
z1[::-2] (60,40,20) (from end till beginning in step 2)
z1[-5:-2] (20,30,40)
z1[5:2:-1] (60,50,40)
z1[-2:-5:-1] (50,40,30)
z1[-2:-5:-2] (50,30)
'+' OPERATOR
EXAMPLE 1
>>> t=tuple()
>>> t=t+(10,20)
>>> print(t)
OUTPUT
(10, 20)
EXAMPLE 2
>>> t=('A', 'B', 'C')
>>> t=t+ (10,)
>>> print(t)
OUTPUT
('A', 'B', 'C', 10)
EXAMPLE 3
>>> t=('A', 'B', 'C')
>>> t=t+ 10
OUTPUT
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
t=t+ 10
TypeError: can only concatenate tuple (not "int") to tuple
OUTPUT
(100, 200)
UPDATING TUPLES
Tuples are immutable which means you cannot update or change the values of tuple elements.
To explicitly remove an entire tuple, just use the del statement. For example
>>> del z1
NOTE : If we try to access a tuple after deleting it, it will result in error.
>>> print(z1)
Traceback (most recent call last):
File "<pyshell#66>", line 1, in <module>
print(z1)
NameError: name 'z1' is not defined
TUPLE OPERATIONS
Concatenation(+)
>>> z4=z2+z3
>>> print(z4)
>>> ('true',) * 4
>>> z3*2
>>> 2 in (1, 2, 3)
True
>>> 10 in z2
True
Iteration
print(a,end=" ")
123
Swapping
>>> z2,z3=z3,z2
>>> print(z2)
(30, 40)
>>> print(z3)
EXPLANATION EXAMPLE
len() >>> tuple1=(100, 200, 50, 400, 500, 'Raman', 100,
'Ashwin')
Returns the length of the
tuple >>> len(tuple1)
SYNTAX: 9
len(tuple-name)
max() >>> tuple3=(10,20,30,40)
>>> tuple5=('ashwin','bharat','shelly')
>>> max(tuple5)
'shelly'
here it will return the string which starts with character having
highest ASCII value.
>>> max(tuple5)
'surpreet'
If there are two or more string which start with the same
character, then the second character is compared and so on.
min() >>> tuple3=(10,20,30,40)
>>> tuple5=('ashwin','bharat','shelly')
>>> min(tuple5)
'ashwin'
here it will return the string which starts with character having
smallest ASCII value.
>>> min(tuple5)
'ashwin'
If there are two or more string which start with the same
character, then the second character is compared and so on.
count() >>> tuple6=(10,20,10,30,10,40,50)