Programming Lesson by Slidesgo
Programming Lesson by Slidesgo
(AIL PRESENTATION)
By- Abhishikta Chatterjee
XII-A
DECLARATION BY THE STUDENT
ACKNOWLEDGEMENT
01 03
What is a tuple? Tuple
operations
02 04
Creating tuples Tuple methods
and built in
functions
WHAT IS A TUPLE?
• Tuples are represented as group of comma-separated values of any date type within
parentheses, e.g., following are some tuples:
p = (1, 2, 3, 4, 5)
q = (2, 4, 6, 8)
r= ('a', 'e', 'i', 'o', 'u')
h = (7, 8, 9, 'A', 'B', 'C')
• NOTE
Values of type list are mutable i.e., changeable - one can change/add/delete a list's elements. But
the values of type tuple are immutable i.e., non-changable; one cannot make changes to a tuple.
• The Tuples are depicted through parentheses i.e., round brackets, e.g.,
following are some tuples in Python :
() (7,)
(1, 2, 3)
(1, 2.5, 3.7, 9)
('a', 'b', 'c')
('a', 1, "b', 3.5, 'zero')
('One', 'Two', 'Three')
• You can also use the built-in tuple type object (tuple()) to
create tuples from sequences as per the syntax given below:
T= tuple(<sequence>)
>>> t1 = tuple('hello')
>>> t1
('h', 'e', '1', '1', '0’)
• Traversing a Tuple-
Traversing a tuple means accessing and processing each element
of it. The for loop makes it easy.
• Like strings and lists, you can only use an integer with a *
operator when trying to replicate a tuple.
TUPLE OPERATIONS: SLICING
TUPLES
• Tuple slices, like list-slices or string slices are the sub part of the tuple extracted out. You
can use indexes of tuple elements to create tuple slices as per following format:
seq= T [start: stop]
• Recall that index on last limit is not included in the tuple slice. Consider the following
example:
>>> tpl = (10, 12, 14, 20, 22, 24, 30, 32, 34)
>>> seq=tpl [3:-3]
>>> seq
(20, 22, 24)
• If you want to extract, not consecutive but every other element of the tuple, there is a way
out- the slice steps. The slice steps are used as per following format:
seq=T [start: stop: step]
TUPLE OPERATIONS:
UNPACKING TUPLES
• Creating a tuple from a set of values is called packing and its reverse, i.e.,
creating individual values from a tuple's elements is called unpacking.
Len() Returns the length or the number of elements of the tuple >>> tuple1=(1,2,3,4,5)
passed as the argument. >>> len(tuple1)
5
>>>tuple3= tuple(range(5))
>>>tuple3
(0,1,2,3,4)
METHOD DESCRIPTION EXAMPLE