SlideShare a Scribd company logo
2
Most read
5
Most read
8
Most read
Tuples in Python
1
Why do we need tuple in Python?
A tuple is also used to store multiple data of the
same type or different type.
2
What is a tuple in Python?
A tuple is a collection which is ordered and
unchangeable.
Tuple items are ordered, unchangeable, and allow
duplicate values
3
How tuple is different from list?
Tuples are immutable objects the lists are mutable.
This means that tuples cannot be changed while the
lists can be modified.
Tuples use parentheses (), whereas lists use square
brackets [].
4
Creating a tuple in Python?
Tuple is creating by placing all the items (elements)
inside a round bracket (), separated by commas.
To create a tuple with only one item, you have to add
a comma after the item, otherwise Python will not
recognize it as a tuple.
5
Output: <class ‘tuple’>
Output: <class ‘string’>
Creating a tuple in Python?
To create a tuple with multiple item, you add comma
after every element except the last one.
6
Creating a tuple in Python?
Tuple can also be created using a tuple constructor
‘tuple()’.
7
Output: (‘apple’, ‘banana’, ‘cherry’)
Accessing item from a tuple?
You can access tuple items by referring to the index
number, inside square brackets same as we access
element from a list.
Example:
tuple1=("apple", "banana", "cherry", "orange", "kiwi")
print(tuple1[1:3])
Output: ('cherry', 'orange', 'kiwi')
8
Output: banana
How to update a tuple?
Tuple is unchangeable but you can convert tuple into list,
change a list, and then convert list back into a tuple.
Example:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
9
Output: ("apple", “kiwi", "cherry")
How to add an item in a tuple?
1. Since tuples are immutable, they do not have a
build-in append() method, but you can convert it into a
list, add your item(s), and convert it back into a tuple.
Example:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
10
Output: ("apple", “banana", "cherry“,”orange”)
How to add an item in a tuple?
2. You are allowed to add tuples to tuples, so if you
want to add one item, (or many), create a new tuple
with the item(s), and add it to the existing tuple:
Example:
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y
print(thistuple)
11
Output: ("apple", “banana", "cherry“,”orange”)
How to remove an item in a tuple?
2. In the same way you add an element in a tuple, you
can remove an element from a tuple.
Example:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)
12
Output: (“banana", "cherry“)
How to unpack a tuple?
When we create a tuple, we normally assign values to it.
This is called "packing" a tuple. But, in Python, we are
also allowed to extract the values back into variables.
This is called "unpacking":
Example Unpacking:
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
13
Output: Apple
Output: banana
Output: cherry
How to unpack a tuple?
Using Asterisk (*): If the number of variables is less
than the number of values, you can add an * to the
variable name and the values will be assigned to the
variable as a list
Example Unpacking:
fruits = ("apple", "banana", "cherry“, “strawberry”)
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
14
Output: Apple
Output: banana
Output: [cherry, strawberry]
How to unpack a tuple?
Using Asterisk (*): If the number of variables is less
than the number of values, you can add an * to the
variable name and the values will be assigned to the
variable as a list
Example Unpacking:
fruits = ("apple", "banana", "cherry“, “strawberry”)
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
15
Output: Apple
Output: banana
Output: [cherry, strawberry]
Basic tuple operations?
Tuples respond to the + and * operators much like
strings; they mean concatenation and repetition here
too, except that the result is a new tuple, not a string.
16
Tuple methods?
Python has built-in methods that you can use on tuples.
count(): Returns the number of times a specified value
occurs in a tuple.
index(): Searches the tuple for a specified value and
returns the position of where it was found.
len(): Gives the total length of a tuple.
tuple(seq): Convert list into tuple.
cmp(tuple1,tuple2): Compares elements of both tuples.
17
Learning Objective
In this lecture, we learnt what is a
tuple, why we need tuple, how to
create and write a tuple in Python and
which operations we can perform on
tuple.
18
Conclusion
● To store multiple data in programming, tuple
is used.
● With the help of tuple methods we can
perform many operations on tuple
respectively.
● Built-In functions can also be used on tuple.
19
Self Assessment
Solve the Following Programs
1. Write a program to print the first element of a
tuple.
2. Write a program to get a last element in a tuple
using negative indexing.
3. Write a program to use a range of indexes to print
the third, fourth, and fifth item in the tuple
20
Self Assessment
Solve Following Programs
1. Write a program to print the numbers of a specified
tuple after removing even numbers from it.
2. Write a program to print a specified tuple after
remvoing the 0th, 4th
and 5th
elements.
Sample list=(”yellow”,”Pink”,”Red”,”Green”,”Blue”,”Purple”)
Output =(”Pink”,”Red”,”Green”)
21

More Related Content

PPTX
tuple in python is an impotant topic.pptx
PPTX
Python Tuples.pptx
PPTX
Tuple in python
PPTX
Chapter 13 Tuples.pptx
PPTX
Chapter 13 Tuples.pptx
PDF
Tuple in Python class documnet pritened.
PDF
Python Is Very most Important for Your Life Time.
PPTX
Tuples class 11 notes- important notes for tuple lesson
tuple in python is an impotant topic.pptx
Python Tuples.pptx
Tuple in python
Chapter 13 Tuples.pptx
Chapter 13 Tuples.pptx
Tuple in Python class documnet pritened.
Python Is Very most Important for Your Life Time.
Tuples class 11 notes- important notes for tuple lesson

Similar to Tuples in python better understanding with slides (20)

PPTX
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
PDF
computer science tuple in python project.pdf
PPTX
Chapter 3-Data structure in python programming.pptx
PPT
List.ppt
PPTX
Python Collections
PDF
updated_tuple_in_python.pdf
PPTX
Chapter 17 Tuples
PPTX
Effective tuples in phyton template.pptx
PDF
Python Tuple.pdf
PPTX
1.10 Tuples_sets_usage_applications_advantages.pptx
PDF
Tuple Operation and Tuple Assignment in Python.pdf
PDF
tuples chapter (1).pdfhsuenduneudhhsbhhd
PPTX
Python Lists.pptx
PPTX
Lecture 09.pptx
PPTX
Lists_tuples.pptx
PPTX
Chapter 2-Programming Basics and Arrays.pptx Chapter 2-Programming Basics and...
PPTX
Tuples-and-Dictionaries.pptx
PPTX
Python Loops, loop methods and types .pptx
PPTX
Decision control units by Python.pptx includes loop, If else, list, tuple and...
PPTX
Learn more about the concepts of Data Types in Python
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
computer science tuple in python project.pdf
Chapter 3-Data structure in python programming.pptx
List.ppt
Python Collections
updated_tuple_in_python.pdf
Chapter 17 Tuples
Effective tuples in phyton template.pptx
Python Tuple.pdf
1.10 Tuples_sets_usage_applications_advantages.pptx
Tuple Operation and Tuple Assignment in Python.pdf
tuples chapter (1).pdfhsuenduneudhhsbhhd
Python Lists.pptx
Lecture 09.pptx
Lists_tuples.pptx
Chapter 2-Programming Basics and Arrays.pptx Chapter 2-Programming Basics and...
Tuples-and-Dictionaries.pptx
Python Loops, loop methods and types .pptx
Decision control units by Python.pptx includes loop, If else, list, tuple and...
Learn more about the concepts of Data Types in Python
Ad

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Computing-Curriculum for Schools in Ghana
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
master seminar digital applications in india
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
RMMM.pdf make it easy to upload and study
PDF
Pre independence Education in Inndia.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
VCE English Exam - Section C Student Revision Booklet
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPH.pptx obstetrics and gynecology in nursing
O7-L3 Supply Chain Operations - ICLT Program
Computing-Curriculum for Schools in Ghana
STATICS OF THE RIGID BODIES Hibbelers.pdf
Basic Mud Logging Guide for educational purpose
TR - Agricultural Crops Production NC III.pdf
Sports Quiz easy sports quiz sports quiz
master seminar digital applications in india
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
RMMM.pdf make it easy to upload and study
Pre independence Education in Inndia.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Ad

Tuples in python better understanding with slides

  • 2. Why do we need tuple in Python? A tuple is also used to store multiple data of the same type or different type. 2
  • 3. What is a tuple in Python? A tuple is a collection which is ordered and unchangeable. Tuple items are ordered, unchangeable, and allow duplicate values 3
  • 4. How tuple is different from list? Tuples are immutable objects the lists are mutable. This means that tuples cannot be changed while the lists can be modified. Tuples use parentheses (), whereas lists use square brackets []. 4
  • 5. Creating a tuple in Python? Tuple is creating by placing all the items (elements) inside a round bracket (), separated by commas. To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple. 5 Output: <class ‘tuple’> Output: <class ‘string’>
  • 6. Creating a tuple in Python? To create a tuple with multiple item, you add comma after every element except the last one. 6
  • 7. Creating a tuple in Python? Tuple can also be created using a tuple constructor ‘tuple()’. 7 Output: (‘apple’, ‘banana’, ‘cherry’)
  • 8. Accessing item from a tuple? You can access tuple items by referring to the index number, inside square brackets same as we access element from a list. Example: tuple1=("apple", "banana", "cherry", "orange", "kiwi") print(tuple1[1:3]) Output: ('cherry', 'orange', 'kiwi') 8 Output: banana
  • 9. How to update a tuple? Tuple is unchangeable but you can convert tuple into list, change a list, and then convert list back into a tuple. Example: x = ("apple", "banana", "cherry") y = list(x) y[1] = "kiwi" x = tuple(y) print(x) 9 Output: ("apple", “kiwi", "cherry")
  • 10. How to add an item in a tuple? 1. Since tuples are immutable, they do not have a build-in append() method, but you can convert it into a list, add your item(s), and convert it back into a tuple. Example: thistuple = ("apple", "banana", "cherry") y = list(thistuple) y.append("orange") thistuple = tuple(y) 10 Output: ("apple", “banana", "cherry“,”orange”)
  • 11. How to add an item in a tuple? 2. You are allowed to add tuples to tuples, so if you want to add one item, (or many), create a new tuple with the item(s), and add it to the existing tuple: Example: thistuple = ("apple", "banana", "cherry") y = ("orange",) thistuple += y print(thistuple) 11 Output: ("apple", “banana", "cherry“,”orange”)
  • 12. How to remove an item in a tuple? 2. In the same way you add an element in a tuple, you can remove an element from a tuple. Example: thistuple = ("apple", "banana", "cherry") y = list(thistuple) y.remove("apple") thistuple = tuple(y) 12 Output: (“banana", "cherry“)
  • 13. How to unpack a tuple? When we create a tuple, we normally assign values to it. This is called "packing" a tuple. But, in Python, we are also allowed to extract the values back into variables. This is called "unpacking": Example Unpacking: fruits = ("apple", "banana", "cherry") (green, yellow, red) = fruits print(green) print(yellow) print(red) 13 Output: Apple Output: banana Output: cherry
  • 14. How to unpack a tuple? Using Asterisk (*): If the number of variables is less than the number of values, you can add an * to the variable name and the values will be assigned to the variable as a list Example Unpacking: fruits = ("apple", "banana", "cherry“, “strawberry”) (green, yellow, red) = fruits print(green) print(yellow) print(red) 14 Output: Apple Output: banana Output: [cherry, strawberry]
  • 15. How to unpack a tuple? Using Asterisk (*): If the number of variables is less than the number of values, you can add an * to the variable name and the values will be assigned to the variable as a list Example Unpacking: fruits = ("apple", "banana", "cherry“, “strawberry”) (green, yellow, red) = fruits print(green) print(yellow) print(red) 15 Output: Apple Output: banana Output: [cherry, strawberry]
  • 16. Basic tuple operations? Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new tuple, not a string. 16
  • 17. Tuple methods? Python has built-in methods that you can use on tuples. count(): Returns the number of times a specified value occurs in a tuple. index(): Searches the tuple for a specified value and returns the position of where it was found. len(): Gives the total length of a tuple. tuple(seq): Convert list into tuple. cmp(tuple1,tuple2): Compares elements of both tuples. 17
  • 18. Learning Objective In this lecture, we learnt what is a tuple, why we need tuple, how to create and write a tuple in Python and which operations we can perform on tuple. 18
  • 19. Conclusion ● To store multiple data in programming, tuple is used. ● With the help of tuple methods we can perform many operations on tuple respectively. ● Built-In functions can also be used on tuple. 19
  • 20. Self Assessment Solve the Following Programs 1. Write a program to print the first element of a tuple. 2. Write a program to get a last element in a tuple using negative indexing. 3. Write a program to use a range of indexes to print the third, fourth, and fifth item in the tuple 20
  • 21. Self Assessment Solve Following Programs 1. Write a program to print the numbers of a specified tuple after removing even numbers from it. 2. Write a program to print a specified tuple after remvoing the 0th, 4th and 5th elements. Sample list=(”yellow”,”Pink”,”Red”,”Green”,”Blue”,”Purple”) Output =(”Pink”,”Red”,”Green”) 21

Editor's Notes

  • #2: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #3: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #4: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #8: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #9: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #10: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #11: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #12: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #13: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #14: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #15: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #16: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #17: Programming sekhny ka maksad hia k ham apni real life problem computer ki madad sy solve ker sakhain. Ham apni roz mara zindagi main bhot sary decision conditions ki base per lyty hain jesy k Agr kal barish hoi to ham pick nick per jain gy Agr aaj school ki chuti hoi to main cartoon daikoun ga Or Agr kal mjy job mill gi to main aap ko party doun ga. In conditions ko programming main handel kerny k leay conditional structure use hoty hain.
  • #18: After this lecture, you shall be able to Explain why we need variables and what is their relation to the memory.
  • #19: Programming main decision making achieve kerny k leay conditional structure use hoty hain, comparision operator jisy ham double equal operator bhe bolyt hian 2 values ko compare kerny k leay use hota hia. Is k elawa if command main likhe jany wali condition ko ham Boolean expression bhe khety hain.