0% found this document useful (0 votes)
64 views37 pages

TUPLES

Tuples are immutable ordered sequences of values of any data type. They are created by enclosing comma-separated values within parentheses. Values in tuples can be accessed using indexing which starts from 0 to n-1. Tuples are immutable so their values cannot be changed, only new tuples can be generated. Built-in functions like len(), max(), min() etc. can operate on tuples. Tuples can be returned from functions containing multiple return values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views37 pages

TUPLES

Tuples are immutable ordered sequences of values of any data type. They are created by enclosing comma-separated values within parentheses. Values in tuples can be accessed using indexing which starts from 0 to n-1. Tuples are immutable so their values cannot be changed, only new tuples can be generated. Built-in functions like len(), max(), min() etc. can operate on tuples. Tuples can be returned from functions containing multiple return values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

TUPLES

• “A tuple is an immutable, ordered sequence


of values of any data types (string, float,
integer, etc.)”
– Values in the tuples are called elements or items.
– The elements in the tuples are immutable and
indexed / ordered.
– The elements in the list are enclosed in
parentheses ( )separated by comma.
CREATING A TUPLE

• Tuple_name = (item1, item2, item3,


item4……., item n)
Syntax
Tuple_variable [index_number]
• Indexing
• Forward indexing: Index starts from 0 to n-1.
Example
>>>subject= (“English”, “Tamil”, “Maths”, “Physics”,
“Botany”, “Zoology”)
>>>subject [0]
‘English’#output
>>>subject [2]
‘Maths’ #output
>>>subject [5]
‘Zoology’ #output
• Negative (backward) indexing: The index of -1 refers to
the last item, -2 to the second last item and so on.
Example
>>>subject= (“English”, “Tamil”, “Maths”, “Physics”,
“Botany”, “Zoology”)
>>>subject [-1]
‘Zoology’ #output
>>>subject [-3]
‘Physics’ #output
>>>subject [-6]
‘English’ #output
VARIABLE-LENGTH ARGUMENTS
TUPLES
• Variable number of arguments can also be
passed to a function.
• A variable name that is preceded by an
asterisk (*) collects the arguments into a tuple
Example
#function definition
>>>def greeting (*t):
i=0
while i <len(t):
print (“Hai”,t[i])
i=i+1
#function call
>>>greeting ("Anu","Kavin","Ram","Krish")
Output
HaiAnu
HaiKavin
Hai Ram
HaiKrish
UPDATING TUPLES ( TUPLES ARE
IMMUTABLE)
• Tuples are immutable, which means cannot
update or change the values of tuple
elements.
• Can generate a new tuple rather than change
the old tuples.
Example
>>>a = (‘parrot’, ’Dove’, ’duck’, ’cuckoo’)
>>>a [3] = “crow”
Output:
TypeError: object doesn’t support item
assignment
DELETEING TUPLE ELEMENTS
• Removing individual tuple elements is not
possible.
• To remove an entire tuple, use the keyword
del.
Syntax
del tuple_name
Example
>>>tup= (‘parrot’, ’Dove’, ’duck’, ’cuckoo’)
>>>tup
(‘parrot’, ’Dove’, ’duck’, ’cuckoo’)
>>>del tup
>>>tup
Output:
NameError: name 'tup' is not defined
(means tup is deleted)
TUPLE OPERATION
• There are many operations that can be
performed with tuple.
• The various tuple operations are,
1. Tuple concatenation
2. Repeating a tuple (replication operator)
3. ‘in’ operator (Tuple Membership test)
4. Iteration through a tuple
Tuple concatenation
• A number of tuple can be combined to form a
single tuple is called tuple concatenation.
• The (+) operator is to join the tuple.
Syntax
“tuple_variable 1” + “tuple _variable
2”+…..+“tuple _variable n”
Example
>>>a = (‘x’, ’y’, ’z’)
>>>b = (5, 10, 15)
>>>c = a + b
>>>print(c)
Output
( ‘x’, ’y’, ’z’, 5, 10, 15)
Repeating a list(Replication Operator)
Group of tuple can be repeated by using the (*)
operator.
Example
>>>a = ( 1, 2, 3 )
>>>print (a* 3)
Output
(1, 2, 3, 1, 2, 3, 1, 2, 3)

The (*) operator is the tuple replication operator.


Syntax
(“tuple_variable” * n)
• Where, tuple_variable is the variable name
• * is the list replication operator
• n is the number of times the list to be
repeated.
Example
>>>a = ( 1, 2, 3 )
>>>print (a* 3)
Output
(1, 2, 3, 1, 2, 3, 1, 2, 3)
The ‘in’ operator (Tuple Membership
test)
• Can test if an element is exists within a tuple
or not, using the keyword ‘in’.
Example
>>>a = ( 1, 2, 3, 4, 5 )
>>>5 in a
True
>>>10 in a
False
Iterating through a Tuple

Traversal using ‘for’ loop


Example
>>>bird = (‘p’, ’a’, ’r’,’ r’, ’o’, ’t’)
>>>for letter in bird:
print(letter)
Output:
Pa r r o t
The most common way to traverse the elements of
a tuple is using a ‘for loop’.
BUILD-IN TUPLE METHODS
• In python count ( ) and index ( ) are the two
methods that works with tuples.
• Tuple Method in python
– count( )
• Returns the count number of items passed as an
argument
– index( )
• Returns the index of the first matched item.
Count (x )
• The count method returns the number of
times xappears in the tuple.
Syntax
tuple_name.count(object)
Example
>>>t = (‘a’, ‘p’, ‘p’, ‘l’, ‘e’)
>>>print(a.count(‘p’)
2
index (x)
• The index method returns the index of the
first matched item. in the tuple.
Syntax
tuple_name.index(object)
Example
>>>t = (‘a’, ‘p’, ‘p’, ‘l’, ‘e’)
>>>print(a.index(‘l’)
3
BUILD-IN FUNCTIONS WITH TUPLES
• Python’s built-in functions can be used with tuple
to obtain needed value and execute various tasks.
len( )
• The length function returns the number of items
on a tuple.
Example
>>>a=(0, 5, 10, 15, 20, 25)
>>>len(a)
6
Max( )
• The Max( ) function returns item with
maximum value from thetuple.
Example
>>>a = (0, 5, 10, 15, 20, 25)
>>>max(a)
25
Min( )
• The Min( ) function returns item with
minimum value from the tuple
Example
>>>a = (0, 5, 10, 15, 20, 25)
>>>min(a)
0
sum( )
• The sum( ) function returns the sum of all
items on a tuple
Example
>>>a = (0, 5, 10, 15, 20, 25)
>>>sum(a)
75
sorted( )

• The sorted function returns a sorted list in


ascending order.
Example
>>>a = (1, 9, 15, 11, 3, 7)
>>>sorted(a)
(1, 3, 7, 9, 11, 15)
tuple( )
• Convert other sequence like list, string, or
dictionary into a tuples
1. Converting string to tuple
Example
>>>str= “python”
>>>tuple(str)
Output:
(‘p’, ‘y’, ‘t’, ‘h’, ’o’, ‘n’)
2. Converting list to tuple
Example
>>>list= [‘red’, ‘green’, ‘blue’, ‘yellow’]
>>>tuple(list)
Output:
(‘red’, ‘green’, ‘blue’, ‘yellow’)
3. Converting dictionary to tuple
Example
>>>dict= {‘Name’ : ‘Raju’ , ‘age’ : 20, ‘color’ :
‘white’}
>>>tuple(dict)
Output:
(‘Name’, ‘age’, ‘color’)
TUPLE ASSIGNMENT
• A programmer can assign multiple variables in
one statement by using tuple assignment.
• Tuple assignment is an assignment with a
sequence on the right side and a tuple of
variables on the left.
• The right side is evaluated and then its
elements are assigned to the variables on the
left
Example
>>>a, b = 1, 2
>>>a
1
>>>b
2
• Where a, b is tuple of expressions
• 1, 2 is tuple of variables
• Each value is assigned to its respective
variables.
• The number of variables on the left and the
number of values on the right have to be
same.
Example
>>>a, b = 1, 2, 3
Value Error: too many values to unpack
TUPLE AS RETURN VALUES
• Tuples can also be returned by the function as
return values.
• A function can return more than one value
using tuple.
Example
#function definition
>>>def div_mod(a,b):
quotient = a / b
remainder = a % b
return quotient, remainder
#function call
>>>x, y = 10, 3
>>>t = div_mod (x, y)
>>>print(t)
(3, 1)#output
• In this example both the quotient and
remainder can be computed at the same time.
• Two values will be returned, i.e., quotient and
remainder by using the tuple as the return
value of the function.

You might also like