KEMBAR78
Tuples in python better understanding with slides | PPTX
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

Tuples in python better understanding with slides

  • 1.
  • 2.
    Why do weneed tuple in Python? A tuple is also used to store multiple data of the same type or different type. 2
  • 3.
    What is atuple 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 isdifferent 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 tuplein 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 tuplein Python? To create a tuple with multiple item, you add comma after every element except the last one. 6
  • 7.
    Creating a tuplein Python? Tuple can also be created using a tuple constructor ‘tuple()’. 7 Output: (‘apple’, ‘banana’, ‘cherry’)
  • 8.
    Accessing item froma 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 updatea 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 addan 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 addan 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 removean 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 unpacka 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 unpacka 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 unpacka 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? Tuplesrespond 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 hasbuilt-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 thislecture, 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 storemultiple 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 theFollowing 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 FollowingPrograms 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.