SlideShare una empresa de Scribd logo
1 de 24
TUPLE IN PYTHON
COMPUTER SCIENCE(083)
XII
What is tuple?:
A tuple is a collection of values or an ordered sequence of values
similar to list.
Elements of a tuple enclosed in a parenthesis ( ), separated by
commas (,) .
Syntax:
tuple_name= (value1, value2,……..,valueN)
Example:
tup = (10, 20, 30, 40, 50 )
HOW TO CREATE AND INITIALIZE TUPLE
Method 1: If tuple is declare empty.
tup1=( )
Method 2: Initialize tuple with value:
If we want to store the numbers in a tuple.
tup2= (1, 2, 30, 4, 15)
If we want to store the words or string in a tuple.
tup3= (‘MON’, ‘TUE’, ’WED’, ’THU’)
HOW TO CREATE AND INITIALIZE TUPLE
Example: If we want to store the characters in a tuple.
tup4= (‘A’,’E’,’I’,’O’,’U’)
Example: If we want to store the mixed information in a tuple.
tup4= (“Kapil”, 13,”Class-IX”, 40)
TO DISPLAY THE TUPLE
Example:
tup2= (10, 20, 30, 40, 50)
print(tup2)
----------------Output-------------
(10, 20, 30, 40, 50)
Example:
tup= (“Mango”, “Apple”,”Grapes”,”Oranges”)
print(tup)
----------------Output-------------
(‘Mango’, ‘Apple’, ’Grapes’, ’Oranges’)
HOW TO CREATE AND INITIALIZE THE TUPLE
USING TUPLE() CONSTRUCTOR
Syntax:
tuple_name=tuple(sequence or string)
Example:
tup1=tuple()
Print(tup1)
----------output----------
( )
tup4= tuple((‘A’,’E’,’I’,’O’,’U’))
print(tup4)
If we want to store the characters in a list
tup4= tuple(“AEIOU”)
print(tup4)
---------Output----------
(‘A’, ‘E’,’I’,’O’,’U’)
HOW TO CREATE AND INITIALIZE THE TUPLE
USING TUPLE() CONSTRUCTOR
Example:
If we want to store the mixed information in a list.
tup4= tuple((“Kapil”, 13,”Class-IX”, 40))
print(tup4)
----------------Output-------------
(‘Kapil’, 13,’Class-IX’, 40)
Example: If we want to store the numbers in a tuple.
tup2=tuple((10, 20, 30, 40, 50))
print(tup2) ----------------Output-------------
(10, 20, 30, 40, 50)
HOW USER ENTER THE VALUES IN A TUPLE
AT RUN TIME.
We can use eval( ) method, which identifies the data type
and evaluate them automatically.
Example:
no=eval(input(“Enter the no:”))
print(no)
-------Input-----------
Enter the no: 1,2,3,4,5,6,7,8,9
-----------Output-------------
(1,2,3,4,5,6,7,8,9)
Example:
tup1=()
x=1
while x<=5:
no=int(input(“Enter the no:”))
tup1=tup1+(no,)
x=x+1
print(tup1)
====OUTPUT=====
Enter the no:10
Enter the no:20
Enter the no:30
Enter the no:40
Enter the no:50
(10, 20, 30, 40, 50)
Accessing Tuple Elements
Example: Let’s store no’s in a tuple
no=(10,20,30,40,50,60,70,80)
0 1 2 3 4 5 6 7
10 20 30 40 50 60 70 80
-8 -7 -6 -5 -4 -3 -2 -1
Positive index
Negative index
Now To access these tuple let us discuss
operations of tuple
Tuple operations
Indexing Slicing Repetition Concatenation Membership
Testing
Indexing
Indexing specify the position of elements in a tuple or sequence and
help us to access the value from the sequence separately.
For Example:
if we want to access the number 60 from a tuple given below using
positive index number and 20 using negative number
0 1 2 3 4 5 6 7
10 20 30 40 50 60 70 80
-8 -7 -6 -5 -4 -3 -2 -1
Positive index
Negative index
Indexing 0 1 2 3 4 5 6 7
10 20 30 40 50 60 70 80
-8 -7 -6 -5 -4 -3 -2 -1
no=(10,20,30,40,50,60,70,80)
print(no[4])
--------output--------
50
No=(10,20,30,40,50,60,70,80)
print(no[-6])
--------output-------------
30
Slicing
Slicing is an operation in which you can slice a particular range
from a sequence.
Syntax: tupname [start : stop : step]
Where, start is the starting point
Stop is the stopping point
Step is the step size—also known as stride, and is
optional. Its default value is 1
Slicing Now let Us take one Example:
print ( no [-3 : ] ) 60, 70, 80
print ( no [ 1 : 4 ] ) 20,30,40
Items from 1 to 3
0 1 2 3 4 5 6 7
10 20 30 40 50 60 70 80
-8 -7 -6 -5 -4 -3 -2 -1
no=(10,20,30,40,50,60,70,80)
Concatenation
It is a process in which tuple can be combine together with the help
of ‘+’ operator.
Example:
t1=(10,20,30)
t2=(1,2,3)
In this t1 we add t2 and original t1 overwritet1=t1+t2
------------output--------------
(10, 20, 30, 1, 2, 3)
print(t1)
Repetition
Multiply ( * asterisk) operator replicates the tuple for specified
number of times.
Example: tup1=(1,2)
print(tup1*3)
------------output--------------
(1, 2, 1, 2,1,2)
It check whether a particular element or item is a member of that
sequence or tuple or not.
There are two operator:
1. in operator 2. not in operator
Membership Testing:
in operator:
It returns true if element appears
in the tuple, otherwise returns
false.
Example:
tup1=(10,20,30,40)
print(30 in tup1)
----------output-----------
True
Note: it will give True if
value not exists inside
the tuple
not in operator:
It returns true if element not appears in the
tuple, otherwise returns false.
Example:
tup1=(10,20,30,40)
print(50 not in tup1)
----------output-----------
True
Tuple functions
len()
count() It count number of times a specified value occurs in a tuple
It returns the length of the tuple means count total number of elements
in a tuple.
tup=(1,2,3,4,5,6,7,8,9)
print(len(tup))
-----Output------
9
tup=(1,2,3,2,4,5,6,2,7,2,8)
print(tup.count(2))
--------OUTPUT-------
4
any()
index() Searches the tuple for a specified value and returns the position of
where it was found
It return True, if a tuple is having at least one item.If the tuple is
empty, it will return False.
Tuple functions
tup=(1,2,3,2,4,5,6,2,7,2,8)
print(tup.index(2))
-----Output------
1
tup=(1,2,3)
print(any(tup))
-----Output------
True
tup=()
print(any(tup))
-----Output------
False
If there are
elements
inside it
display
true
If the tuple is empty
it display False
min() and max()
sorted() It is used to sort the elements in a tuple.
tup=(-10,25,-5,1,6,19,7)
print(sorted(tup))
-----Output------
(-10, -5, 1, 6, 7, 19, 25)
tup=(10,25,5,1,6,19,7)
print("Max:",max(tup)," Min:",min(tup))
Max: 25 Min: 1
Traversing Tuple or how to display the tuple
elements using loops
tup=(1,2,3,4,5,6,7,8,9)
for x in range(0,len(tup)):
print(tup[x])
-----Output------
1
2
3
4
5
6
7
8
9
tup=(1,2,3,4,5,6,7,8,9)
x=0
while x<len(tup):
print(tup[x])
x=x+1

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Python
PythonPython
Python
 
Python set
Python setPython set
Python set
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
List in Python
List in PythonList in Python
List in Python
 
Python If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | EdurekaPython If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | Edureka
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaWhat is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 
Python-Tuples
Python-TuplesPython-Tuples
Python-Tuples
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
PART 1 - Python Tutorial | Variables and Data Types in Python
PART 1 - Python Tutorial | Variables and Data Types in PythonPART 1 - Python Tutorial | Variables and Data Types in Python
PART 1 - Python Tutorial | Variables and Data Types in Python
 
Python basics
Python basicsPython basics
Python basics
 
Tuple in python
Tuple in pythonTuple in python
Tuple in python
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 

Similar a Tuple in python

Similar a Tuple in python (20)

PRESENTATION ON TUPLES.pptx
PRESENTATION ON TUPLES.pptxPRESENTATION ON TUPLES.pptx
PRESENTATION ON TUPLES.pptx
 
Pytho_tuples
Pytho_tuplesPytho_tuples
Pytho_tuples
 
TUPLE.ppt
TUPLE.pptTUPLE.ppt
TUPLE.ppt
 
updated_tuple_in_python.pdf
updated_tuple_in_python.pdfupdated_tuple_in_python.pdf
updated_tuple_in_python.pdf
 
Python Tuple.pdf
Python Tuple.pdfPython Tuple.pdf
Python Tuple.pdf
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
 
Python tuples and Dictionary
Python   tuples and DictionaryPython   tuples and Dictionary
Python tuples and Dictionary
 
Tuple.py
Tuple.pyTuple.py
Tuple.py
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
Tuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptxTuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptx
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
07012023.pptx
07012023.pptx07012023.pptx
07012023.pptx
 
Lecture2
Lecture2Lecture2
Lecture2
 
STRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfSTRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdf
 
LIST IN PYTHON-PART 3[BUILT IN PYTHON]
LIST IN PYTHON-PART 3[BUILT IN PYTHON]LIST IN PYTHON-PART 3[BUILT IN PYTHON]
LIST IN PYTHON-PART 3[BUILT IN PYTHON]
 
LIST IN PYTHON PART-2
LIST IN PYTHON PART-2LIST IN PYTHON PART-2
LIST IN PYTHON PART-2
 
Slicing in Python - What is It?
Slicing in Python - What is It?Slicing in Python - What is It?
Slicing in Python - What is It?
 
Python programming for Beginners - II
Python programming for Beginners - IIPython programming for Beginners - II
Python programming for Beginners - II
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
 

Más de vikram mahendra

Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemvikram mahendra
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shopvikram mahendra
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMvikram mahendra
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Pythonvikram mahendra
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONvikram mahendra
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONvikram mahendra
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONvikram mahendra
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1vikram mahendra
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2vikram mahendra
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONvikram mahendra
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]vikram mahendra
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONvikram mahendra
 

Más de vikram mahendra (20)

Communication skill
Communication skillCommunication skill
Communication skill
 
Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop system
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shop
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEM
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Python
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
 
DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
 
GREEN SKILL[PART-2]
GREEN SKILL[PART-2]GREEN SKILL[PART-2]
GREEN SKILL[PART-2]
 
GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
 

Último

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 

Último (20)

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 

Tuple in python

  • 1. TUPLE IN PYTHON COMPUTER SCIENCE(083) XII
  • 2. What is tuple?: A tuple is a collection of values or an ordered sequence of values similar to list. Elements of a tuple enclosed in a parenthesis ( ), separated by commas (,) . Syntax: tuple_name= (value1, value2,……..,valueN) Example: tup = (10, 20, 30, 40, 50 )
  • 3. HOW TO CREATE AND INITIALIZE TUPLE Method 1: If tuple is declare empty. tup1=( ) Method 2: Initialize tuple with value: If we want to store the numbers in a tuple. tup2= (1, 2, 30, 4, 15) If we want to store the words or string in a tuple. tup3= (‘MON’, ‘TUE’, ’WED’, ’THU’)
  • 4. HOW TO CREATE AND INITIALIZE TUPLE Example: If we want to store the characters in a tuple. tup4= (‘A’,’E’,’I’,’O’,’U’) Example: If we want to store the mixed information in a tuple. tup4= (“Kapil”, 13,”Class-IX”, 40)
  • 5. TO DISPLAY THE TUPLE Example: tup2= (10, 20, 30, 40, 50) print(tup2) ----------------Output------------- (10, 20, 30, 40, 50) Example: tup= (“Mango”, “Apple”,”Grapes”,”Oranges”) print(tup) ----------------Output------------- (‘Mango’, ‘Apple’, ’Grapes’, ’Oranges’)
  • 6. HOW TO CREATE AND INITIALIZE THE TUPLE USING TUPLE() CONSTRUCTOR Syntax: tuple_name=tuple(sequence or string) Example: tup1=tuple() Print(tup1) ----------output---------- ( ) tup4= tuple((‘A’,’E’,’I’,’O’,’U’)) print(tup4) If we want to store the characters in a list tup4= tuple(“AEIOU”) print(tup4) ---------Output---------- (‘A’, ‘E’,’I’,’O’,’U’)
  • 7. HOW TO CREATE AND INITIALIZE THE TUPLE USING TUPLE() CONSTRUCTOR Example: If we want to store the mixed information in a list. tup4= tuple((“Kapil”, 13,”Class-IX”, 40)) print(tup4) ----------------Output------------- (‘Kapil’, 13,’Class-IX’, 40) Example: If we want to store the numbers in a tuple. tup2=tuple((10, 20, 30, 40, 50)) print(tup2) ----------------Output------------- (10, 20, 30, 40, 50)
  • 8. HOW USER ENTER THE VALUES IN A TUPLE AT RUN TIME.
  • 9. We can use eval( ) method, which identifies the data type and evaluate them automatically. Example: no=eval(input(“Enter the no:”)) print(no) -------Input----------- Enter the no: 1,2,3,4,5,6,7,8,9 -----------Output------------- (1,2,3,4,5,6,7,8,9)
  • 10. Example: tup1=() x=1 while x<=5: no=int(input(“Enter the no:”)) tup1=tup1+(no,) x=x+1 print(tup1) ====OUTPUT===== Enter the no:10 Enter the no:20 Enter the no:30 Enter the no:40 Enter the no:50 (10, 20, 30, 40, 50)
  • 11. Accessing Tuple Elements Example: Let’s store no’s in a tuple no=(10,20,30,40,50,60,70,80) 0 1 2 3 4 5 6 7 10 20 30 40 50 60 70 80 -8 -7 -6 -5 -4 -3 -2 -1 Positive index Negative index
  • 12. Now To access these tuple let us discuss operations of tuple Tuple operations Indexing Slicing Repetition Concatenation Membership Testing
  • 13. Indexing Indexing specify the position of elements in a tuple or sequence and help us to access the value from the sequence separately. For Example: if we want to access the number 60 from a tuple given below using positive index number and 20 using negative number 0 1 2 3 4 5 6 7 10 20 30 40 50 60 70 80 -8 -7 -6 -5 -4 -3 -2 -1 Positive index Negative index
  • 14. Indexing 0 1 2 3 4 5 6 7 10 20 30 40 50 60 70 80 -8 -7 -6 -5 -4 -3 -2 -1 no=(10,20,30,40,50,60,70,80) print(no[4]) --------output-------- 50 No=(10,20,30,40,50,60,70,80) print(no[-6]) --------output------------- 30
  • 15. Slicing Slicing is an operation in which you can slice a particular range from a sequence. Syntax: tupname [start : stop : step] Where, start is the starting point Stop is the stopping point Step is the step size—also known as stride, and is optional. Its default value is 1
  • 16. Slicing Now let Us take one Example: print ( no [-3 : ] ) 60, 70, 80 print ( no [ 1 : 4 ] ) 20,30,40 Items from 1 to 3 0 1 2 3 4 5 6 7 10 20 30 40 50 60 70 80 -8 -7 -6 -5 -4 -3 -2 -1 no=(10,20,30,40,50,60,70,80)
  • 17. Concatenation It is a process in which tuple can be combine together with the help of ‘+’ operator. Example: t1=(10,20,30) t2=(1,2,3) In this t1 we add t2 and original t1 overwritet1=t1+t2 ------------output-------------- (10, 20, 30, 1, 2, 3) print(t1)
  • 18. Repetition Multiply ( * asterisk) operator replicates the tuple for specified number of times. Example: tup1=(1,2) print(tup1*3) ------------output-------------- (1, 2, 1, 2,1,2)
  • 19. It check whether a particular element or item is a member of that sequence or tuple or not. There are two operator: 1. in operator 2. not in operator Membership Testing: in operator: It returns true if element appears in the tuple, otherwise returns false. Example: tup1=(10,20,30,40) print(30 in tup1) ----------output----------- True
  • 20. Note: it will give True if value not exists inside the tuple not in operator: It returns true if element not appears in the tuple, otherwise returns false. Example: tup1=(10,20,30,40) print(50 not in tup1) ----------output----------- True
  • 21. Tuple functions len() count() It count number of times a specified value occurs in a tuple It returns the length of the tuple means count total number of elements in a tuple. tup=(1,2,3,4,5,6,7,8,9) print(len(tup)) -----Output------ 9 tup=(1,2,3,2,4,5,6,2,7,2,8) print(tup.count(2)) --------OUTPUT------- 4
  • 22. any() index() Searches the tuple for a specified value and returns the position of where it was found It return True, if a tuple is having at least one item.If the tuple is empty, it will return False. Tuple functions tup=(1,2,3,2,4,5,6,2,7,2,8) print(tup.index(2)) -----Output------ 1 tup=(1,2,3) print(any(tup)) -----Output------ True tup=() print(any(tup)) -----Output------ False If there are elements inside it display true If the tuple is empty it display False
  • 23. min() and max() sorted() It is used to sort the elements in a tuple. tup=(-10,25,-5,1,6,19,7) print(sorted(tup)) -----Output------ (-10, -5, 1, 6, 7, 19, 25) tup=(10,25,5,1,6,19,7) print("Max:",max(tup)," Min:",min(tup)) Max: 25 Min: 1
  • 24. Traversing Tuple or how to display the tuple elements using loops tup=(1,2,3,4,5,6,7,8,9) for x in range(0,len(tup)): print(tup[x]) -----Output------ 1 2 3 4 5 6 7 8 9 tup=(1,2,3,4,5,6,7,8,9) x=0 while x<len(tup): print(tup[x]) x=x+1