SlideShare a Scribd company logo
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

More Related Content

PPTX
Tuple in python
PDF
Python-03| Data types
PPTX
PDF
8 python data structure-1
PPT
standard template library(STL) in C++
PPT
Strings in c
PPTX
Strings in C language
PPTX
STRINGS IN PYTHON
Tuple in python
Python-03| Data types
8 python data structure-1
standard template library(STL) in C++
Strings in c
Strings in C language
STRINGS IN PYTHON

What's hot (20)

PPTX
Chapter 17 Tuples
PPTX
Pointer in c program
PPTX
Functions in c++
PPT
Python List.ppt
PDF
Set methods in python
PDF
Tuples in Python
PPTX
Dynamic memory allocation
PPTX
Python-List.pptx
PPTX
List in Python
PPTX
Looping statements in C
PPTX
Templates in C++
PPTX
C++ string
PPTX
DataFrame in Python Pandas
PDF
PDF
Python strings
PDF
Python file handling
PPTX
Strings in c++
PPTX
Inline function
Chapter 17 Tuples
Pointer in c program
Functions in c++
Python List.ppt
Set methods in python
Tuples in Python
Dynamic memory allocation
Python-List.pptx
List in Python
Looping statements in C
Templates in C++
C++ string
DataFrame in Python Pandas
Python strings
Python file handling
Strings in c++
Inline function
Ad

Similar to Tuple in python (20)

PPTX
PRESENTATION ON TUPLES.pptx
PPTX
Tuplemathspresentationonteupleshhhh.pptx
PPT
PPTX
Tuples class 11 notes- important notes for tuple lesson
PPTX
58. Tuples python ppt that will help you understand concept of tuples
PDF
updated_tuple_in_python.pdf
PDF
Python Tuple.pdf
PPTX
Python programming -Tuple and Set Data type
PDF
Python-Tuples
PDF
tuples chapter (1).pdfhsuenduneudhhsbhhd
PPTX
Tuples in Python 2 Object Oriented Programming.pptx
PDF
Python programming : List and tuples
PPTX
Python Lecture 11
PDF
Python tuples and Dictionary
PPTX
Tuple.py
PDF
python_avw - Unit-03.pdf
PPTX
Tuples-and-Dictionaries.pptx
PDF
An Introduction to Part of C++ STL
PPTX
PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT-4
PRESENTATION ON TUPLES.pptx
Tuplemathspresentationonteupleshhhh.pptx
Tuples class 11 notes- important notes for tuple lesson
58. Tuples python ppt that will help you understand concept of tuples
updated_tuple_in_python.pdf
Python Tuple.pdf
Python programming -Tuple and Set Data type
Python-Tuples
tuples chapter (1).pdfhsuenduneudhhsbhhd
Tuples in Python 2 Object Oriented Programming.pptx
Python programming : List and tuples
Python Lecture 11
Python tuples and Dictionary
Tuple.py
python_avw - Unit-03.pdf
Tuples-and-Dictionaries.pptx
An Introduction to Part of C++ STL
PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT-4
Ad

More from vikram mahendra (20)

PPTX
Communication skill
PDF
Python Project On Cosmetic Shop system
PDF
Python Project on Computer Shop
PDF
PYTHON PROJECT ON CARSHOP SYSTEM
PDF
BOOK SHOP SYSTEM Project in Python
PPTX
FLOW OF CONTROL-NESTED IFS IN PYTHON
PPTX
FLOWOFCONTROL-IF..ELSE PYTHON
PPTX
FLOW OF CONTROL-INTRO PYTHON
PPTX
OPERATOR IN PYTHON-PART1
PPTX
OPERATOR IN PYTHON-PART2
PPTX
USE OF PRINT IN PYTHON PART 2
PPTX
DATA TYPE IN PYTHON
PPTX
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
PPTX
USER DEFINE FUNCTIONS IN PYTHON
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
PPTX
INTRODUCTION TO FUNCTIONS IN PYTHON
PPTX
Python Introduction
PPTX
GREEN SKILL[PART-2]
PPTX
GREEN SKILLS[PART-1]
PPTX
Dictionary in python
Communication skill
Python Project On Cosmetic Shop system
Python Project on Computer Shop
PYTHON PROJECT ON CARSHOP SYSTEM
BOOK SHOP SYSTEM Project in Python
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
FLOW OF CONTROL-INTRO PYTHON
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART2
USE OF PRINT IN PYTHON PART 2
DATA TYPE IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
INTRODUCTION TO FUNCTIONS IN PYTHON
Python Introduction
GREEN SKILL[PART-2]
GREEN SKILLS[PART-1]
Dictionary in python

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
master seminar digital applications in india
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Structure & Organelles in detailed.
PPTX
Lesson notes of climatology university.
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
01-Introduction-to-Information-Management.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Final Presentation General Medicine 03-08-2024.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Cell Types and Its function , kingdom of life
master seminar digital applications in india
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Presentation on HIE in infants and its manifestations
Microbial diseases, their pathogenesis and prophylaxis
Cell Structure & Organelles in detailed.
Lesson notes of climatology university.
Abdominal Access Techniques with Prof. Dr. R K Mishra
Microbial disease of the cardiovascular and lymphatic systems
01-Introduction-to-Information-Management.pdf
GDM (1) (1).pptx small presentation for students
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Complications of Minimal Access Surgery at WLH
Chinmaya Tiranga quiz Grand Finale.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf

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