Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Programming Lesson by Slidesgo

aza

Uploaded by

aviationforce204
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Programming Lesson by Slidesgo

aza

Uploaded by

aviationforce204
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

TUPLES

(AIL PRESENTATION)
By- Abhishikta Chatterjee
XII-A
DECLARATION BY THE STUDENT
ACKNOWLEDGEMENT

I would like to express my special thanks to


our mentor Kajal Lathwal for her time and
efforts she provided throughout the year. Your
useful advice and suggestions were really
helpful to me during the project's completion.
In this aspect, I am eternally grateful to you.
TABLE OF CONTENTS

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')

• Tuples are immutable sequences i.e., you cannot change elements of a


tuple in place.
CREATING TUPLES
• To create a tuple, put a number of expressions, separated by commas
in parentheses. That is, to create a tuple you can write in the form
given below:
T = ()
T = (value, ...)
• This construct is known as a tuple display construct.

• Creating Empty Tuple


The empty tuple is (). You can also create an empty tuple as:
T = tuple()
CREATING SINGLE ELEMENT
TUPLE
• Making a tuple with a single element is tricky because if you just give
a single element in round brackets, Python considers it a value only,
e.g.,
>>> t = (1)
>>> t
1

• (1) was treated as an integer expression, hence t stores an integer 1,


not a tuple.
CONSTRUCT A TUPLE WITH ONE
ELEMENT
• To construct a tuple with one element just add a comma after the
single element as shown below:
>>> t = 3,
>>> t
(3,)
• To create a one-element tuple, make sure to add comma at the end.
• Now t stores a tuple, not integer.
CREATING TUPLES FROM
EXISTING SEQUENCES

• 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>)

• where <sequence> can be any kind of sequence object


including strings, lists and tuples.
• Consider following examples:

>>> t1 = tuple('hello')
>>> t1
('h', 'e', '1', '1', '0’)

>>> L = ['w, 'e', 'r', 't', 'y']


>>>t2 = tuple(L)
>>>t2
('w, 'e', 'r', 't', 'y')
CREATING TUPLE FROM
KEYBOARD INPUT
• You can use this method of creating tuples of single characters or
single digits via keyboard input.

• Consider the code below:


t1= tuple(input('Enter tuple elements:'))
Enter tuple elements: 234567
>>> t1
('2', '3', '4', '5', '6', '7')
EVAL FUNCTION

• Most commonly used method to input tuples is eval(input())


as shown below:
tuple = eval(input("Enter tuple to be added:"))
print("Tuple you entered :", tuple)

• When you execute it, it will work somewhat like:


Enter tuple to be added: (2, 4, "a", "hjkjl, "[3, 4])
Tuple you entered: (2, 4, "a", "hjkjl", [3, 4])
TUPLE OPERATIONS: TRAVERSING

• Traversing a Tuple-
Traversing a tuple means accessing and processing each element
of it. The for loop makes it easy.

• To traverse or loop over the items in a tuple, as per following


syntax:
for <item> in <Tuple>:
process each item here
• For example, following loop shows each item of a tuple T in separate lines:
T= ('P', 'u', 'r', 'e')
for a in T:
print(T[a])

• The above loop will produce result as:


P
u
r
e
TUPLE OPERATIONS: JOINING
TUPLES
• The + operator, the concatenation operator, when used with
two tuples, joins two tuples. Consider the example given
below:
>>>tpl1 = (1, 3, 5)
>>> tp12 = (6, 7, 8)
>>> tp11 + tp12
(1, 3, 5, 6, 7, 8)
TUPLE OPERATIONS: REPEATING OR
REPLICATING TUPLES

• Like strings and lists, you can use* operator to replicate a


tuple specified number of times ,e.g.,
>>> tp11 * 3
(1, 3, 5, 1, 3, 5, 1, 3, 5)

• 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.

• Unpacking is done as per syntax:


<variable1>, <variable2>, <variable3>, ... =t
• where the number of variables in the left side of assignment must match the
number of elements in the tuple.

• For example, if we have a tuple as:


t = (1, 2, 'A', 'B')
TUPLE METHODS AND BUILT IN FUNCTIONS
METHOD DESCRIPTION EXAMPLE

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

Tuple() Creates an empty tuple if no argument is passed. >>> tuple1=tuple()


>>> tuple1
Creates a tuple if a sequence is passed as an argument. ()
>>> tuple1= tuple(‘aeiou’) #string
>>> tuple1
(‘a’, ‘e’, ‘I’, ‘o’, ‘u’)

>>> tuple 2= tuple([1,2,3]) #list


>>> tuple2
(1,2,3)

>>>tuple3= tuple(range(5))
>>>tuple3
(0,1,2,3,4)
METHOD DESCRIPTION EXAMPLE

Index() Returns the index off the first >>> tuple1=(10,20,30,40,50)


occurrence of the element in the >>> tuple1.index (30)
given tuple. 2
>>>tuple1.index (90)
ValueError: tuple.index(x): x not in
tuple
Sorted() Takes elements in the tuple and >>>tuple1= (“Rama”, “Heena”,
returns a new sorted list. It “Raj”, “Mohsin”, “Aditya”)
should be noted be noted that, >>> sorted(tuple1)
sorted[] does not make any [‘Aditya’, ‘Heena’, ‘Mohsin’, ‘Raj’,
change to the original tuple. ‘Rama’]

Count() Returns the number of times the >>> tuple1= (10,20,30,40,10,50)


given element appears in the >>> tuple1.count (10)
tuple. 3
>>> tuple1.count (30)
0
METHOD DESCRIPTION EXAMPLE

Min() Returns minimum or smallest >>> tuple1=


elements of the tuple. (19,12,56,18,9,87,34)
>>> min(tuple1)
9
Returns maximum or largest
Max() element of the tuple. >>>max (tuple1)
87
Returns sum of the elements
Sum() of the tuple. >>> sum(tuple1)
235
REFERENCES/BIBLIOGRAPHY
1. Sumita Arora - Computer Science with Python Class 12
2. www.google.com
3. https://www.wscubetech.com/
4. https://cbsepython.in/

You might also like