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

Python Tuple

The document talks about tuples in Python. It explains how to create, access elements, loop through and perform operations on tuples. It also discusses built-in functions and methods that can be used with tuples.

Uploaded by

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

Python Tuple

The document talks about tuples in Python. It explains how to create, access elements, loop through and perform operations on tuples. It also discusses built-in functions and methods that can be used with tuples.

Uploaded by

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

TUPLE IN PYTHON

Dr. Smita R. Chavan


Dr. Smita R. Chavan 2

Topics :
• Creating Tuple

• Accessing Elements in a Tuple

• Loop Through a Tuple

• Built-in functions used with Tuple

• Built-in Tuple Methods

• Deleting Tuples
Dr. Smita R. Chavan 3

Tuple in Python
• Tuple is a collection which is ordered and unchangeable.
Allows duplicate members.

• A tuple is just like a list of a sequence of immutable


python objects.

• A tuple is declared in parentheses and cannot be


changed. However, you can take portions of existing
tuples to make new tuples.

Dr. Smita R. Chavan 4

Creating Tuple :
• # empty tuple
• tup1 = ()
• print(tup1)

• month = ('Jan',‘Feb',‘March')
• print(month) Accept a number and create a tuple.

• For writing tuple for a single value,


• tup2 = (‘hi’,)

You need to include a comma, even though there is a single value.


Dr. Smita R. Chavan 5

Tuple can have any number of items and they may be of


different types (integer, float, string,list etc.).

• emp_tuple = (1, “Amit", 40000.00)


• print(emp_tuple)

• Define product tuple consists of product number,name


and quantity.
Dr. Smita R. Chavan 6

The tuple() Constructor


• It is also possible to use the tuple() constructor to make a
tuple.
• # empty tuple
• t1 =tuple ()
• print(t1)

• day =tuple( (‘Mon',‘Tue',‘Wed'))


• print(day)

• Accept a string and create a tuple,using constructor.


Dr. Smita R. Chavan 7

Exercise 1:
• Write a python program which will define tuple of month in
words & accept number from user and print month in
words.

• Write a python program which will define tuple of one digit


number in words & accept number from user and print it in
words using function.
Dr. Smita R. Chavan 8

Accessing Elements in a Tuple

We can use the index operator [] to access an item in a


tuple where the index starts from 0.

• my_tuple = ('p',‘y',‘t',‘h',‘o',‘n')
• print(my_tuple[0])
• # Output: 'p‘

• print(my_tuple[5])
• # Output: ‘n‘
Dr. Smita R. Chavan 9

Accessing Elements in a Tuple:


We can access a range of items in a tuple by using
the slicing operator (colon).

• my_tuple = ('p',‘y',‘t',‘h',‘o',‘n')

• print(my_tuple[3:6])
• o/p : (‘h',‘o',‘n')

• print(my_tuple[:3])
• o/p :('p',‘y',‘t')

• print(my_tuple[3:])
• o/p : (‘h',‘o',‘n')
Dr. Smita R. Chavan 10

• print(my_tuple [:])
o/p :('p',‘y',‘t',‘h',‘o',‘n')

• print(my_tuple[-2:])
• o/p :(‘o',‘n')

• print(my_tuple[:-2])
• o/p :('p',‘y',‘t',‘h')

• print(my_tuple[::2])
• o/p :('p',‘t',‘o')

• print(my_tuple[::-1]) # reverse the tuple


Dr. Smita R. Chavan 11

Loop Through a Tuple

• You can loop through the tuple items by using a for loop:
fruit_tuple = ("apple", "banana", "cherry")

for fruit in fruit_tuple:


print(fruit)
• o/p:
• apple
• banana
• cherry
Dr. Smita R. Chavan 12

Check if Item Exists

if “cherry" in my_tuple:


print("Yes, ‘cherry' is in the fruits tuple")

• Create a tuple of Mobile_brands. Accept a name of mobile


and check whether it is present in tuple or not.
Dr. Smita R. Chavan 13

Built-in functions
T = (25,90,45,12,5)

Method Syntax Description Example


len() len(obj) Returns length of obj. len(T)
o/p : 5
sum() sum(obj) Returns the sum of all sum(T)
values from tuple obj . o/p:177
max() max(obj) Returns the max value from max(T)
tuple obj . o/p:90
min() min(obj) Returns the max value from min(T)
tuple obj . o/p:5

sorted() sorted(obj) Sorts objects of tuple. sorted(T)


o/p: [5, 12, 25, 45, 90]
Dr. Smita R. Chavan 14

Built-in Tuple Methods


Python has two built-in methods that you can use on tuples.

num =(34,67,23,34,89)

Method Syntax Description Example


count() tuple.count(obj) Returns count of how num.count(34)
many times obj occurs in o/p : 2
tuple.
index() tuple.index(obj) Returns the lowest index num.index(89)
in tuple that obj appears. o/p:4
Dr. Smita R. Chavan 15

Deleting Tuples

• Tuples are immutable and cannot be deleted, but deleting

tuple entirely is possible by using the keyword "del."

• fruit = ("apple", "banana", "cherry")

del fruit
print(fruit)

• #this will raise an error because the tuple no longer exists


Dr. Smita R. Chavan 16

You might also like