0% found this document useful (0 votes)
28 views22 pages

Lecture 7a

Uploaded by

d.q.huy791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views22 pages

Lecture 7a

Uploaded by

d.q.huy791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

4/17/2023

Tuple data type for Application


Programming with Python

Lecturer: Nguyen van hong


Ed. 2023

1
4/17/2023

LEARNING OUTCOMES

After studying this lesson, you will be able to:

Understand the need of Tuples;


Solve problems by using Tuples;
 Get clear idea about Tuple functions; and
Understand the difference between list, dictionary and tuples.

WHAT IS TUPLE?
A tuple is a sequence of values, which can be of any type and they
are indexed by integer. Tuples are just like list, but we can’t change values
of tuples in place. Thus, tuples are immutable.
The index value of tuple starts from 0.

A tuple consists of a number of values separated by commas. For example:


>>> T=10, 20, 30, 40
>>> print (T)
(10, 20, 30, 40)
>>> T=(10, 20, 30, 40)
>>> print (T)
(10, 20, 30, 40)

2
4/17/2023

CREATING EMPTY TUPLE

ACCESSING TUPLE

3
4/17/2023

ACCESSING TUPLE USING LOOPS

ACCESSING TUPLE USING LOOPS

OUTPUT

4
4/17/2023

CHECK IF ITEM EXISTS

OUTPUT
9

TUPLE LENGTH

OUTPUT

10

5
4/17/2023

REMOVING A TUPLE

You cannot remove or delete or update items in a tuple.

Tuples are unchangeable, so you cannot remove items from


it, but you can delete the tuple completely:

NOTE: TUPLES ARE IMMUTABLE

11

TUPLE METHODS

12

6
4/17/2023

TUPLE METHODS

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

1. count() Method

2. index() Method

13

TUPLE METHODS

1. count() Method
Return the
number of times the
value appears in the
tuple

14

7
4/17/2023

TUPLE METHODS
2. index() Method

index()
Method

index() Method returns index of “banana” i.e 1

15

16

8
4/17/2023

Tuple

Tuple: an immutable sequence


Very similar to a list
Once it is created it cannot be changed
Format: tuple_name = (item1, item2)
Tuples support operations as lists
 Subscript indexing for retrieving elements
 Methods such as index
 Built in functions such as len, min, max
 Slicing expressions
 The in, +, and * operators

17

18

9
4/17/2023

19

20

10
4/17/2023

DIFFERENCE BETWEEN LIST AND TUPLE


LIST TUPLE
Syntax for list is slightly different Syntax for tuple is slightly
comparing with tuple different comparing with lists

Weekdays=[‘Sun’,’Mon’, ‘wed’,46,67] twdays = (‘Sun’, ‘mon', ‘tue', 634)


type(Weekdays) type(twdays)
class<‘lists’> class<‘tuple’>

List uses [ and ] (square brackets) to Tuple uses rounded brackets ( and
bind the elements. ) to bind the elements.

21

DIFFERENCE BETWEEN LIST AND TUPLE

LIST TUPLE
List can be edited once it is created A tuple is a list which one cannot edit
in python. Lists are mutable data once it is created in Python code. The
structure. tuple is an immutable data structure

More methods or functions are Compare to lists tuples have Less


associated with lists. methods or functions.

22

11
4/17/2023

DIFFERENCE BETWEEN TUPLE AND DICTIONARY

TUPLE DICTIONARY
Order is maintained. Ordering is not guaranteed.

They are immutable Values in a dictionary can be changed.

They can hold any type, and types Every entry has a key and a value
can be mixed.

23

DIFFERENCE BETWEEN TUPLE AND DICTIONARY


TUPLE DICTIONARY
Elements are accessed via numeric Elements are accessed using key's
(zero based) indices values

There is a difference in syntax and Differ in syntax, looks bit complicated


looks easy to define tuple when compare with Tuple or lists

24

12
4/17/2023

PROGRAMS ON
TUPLE

25

PROGRAMS AT EASY LEVEL

1. Write a Python program to create a tuple.


#Create an empty tuple
x = ()
print(x)

#Create an empty tuple with tuple() function


#built-in Python
tuplex = tuple()
print(tuplex)

26

13
4/17/2023

2. Write a Python program to create a tuple with


different data types

#Create a tuple with different data types

tuplex = ("tuple", False, 3.2, 1)


print(tuplex)

27

3. Write a Python program to unpack a tuple in several variables


#create a tuple
tuplex = (4, 8, 3 )# tuplex = 4, 8, 3
print(tuplex)
n1, n2, n3 = tuplex
#unpack a tuple in variables
print(n1 + n2 + n3)
#the number of variables must be equal to the number of items of the tuple
n1, n2, n3, n4= tuplex #

28

14
4/17/2023

29

TUPLE - AVERAGE PROGRAMS

4. Write a Python program to find the index of an item of a tuple.

#create a tuple
tuplex = tuple("index tuple")
print(tuplex)
#get index of the first item whose value is passed as parameter
index = tuplex.index("e")
print("index of e is ", index)
#define the index from which you want to search
index = tuplex.index("e",5)
print("index of e in range from 5 to end is ", index)

30

15
4/17/2023

31

TUPLE - AVERAGE PROGRAMS

print(index)
#define the segment of the tuple to be searched
index = tuplex.index("e", 3, 6)
print(index)
#if item not exists in the tuple return ValueError Exception
index = tuplex.index("y")

32

16
4/17/2023

TUPLE - AVERAGE PROGRAMS

5. Write a Python program to add an item in a tuple.


#create a tuple
tuplex = (4, 6, 2, 8, 3, 1)
print(tuplex)
#tuples are immutable, so you can not add new elements
#using merge of tuples with the + operator you can add an element and it will create a new tuple
tuplex = tuplex + (9,)
print(tuplex)

33

Create new tuple so that Id of new tuple is


different with the original tuple

34

17
4/17/2023

35

36

18
4/17/2023

TUPLE - AVERAGE PROGRAMS

5. Write a Python program to add an item in a tuple.


#adding items in a specific index
tuplex =(4, 6, 2, 8, 3, 1)
tuplex = tuplex[:5] + (15, 20, 25) + tuplex[5:]
print(tuplex)
#converting the tuple to list
listx = list(tuplex)
#use different ways to add items in list
listx.append(30)
tuplex = tuple(listx)
print(tuplex)

37

38

19
4/17/2023

6. Write a Python program to convert a tuple to a string.

7. Write a Python program to get the 4th element and 4th element from last
of a tuple.

8. Write a Python program to find the repeated items of a tuple.

9. Write a Python program to check whether an element exists within a


tuple.

39

40

20
4/17/2023

41

Write a Python program to find repeated items in a tuple.

#create a tuple
tuplex = 2, 4, 5, 6, 2, 3, 4, 4, 7
print(tuplex)
#return the number of times it appears in the tuple.
count = tuplex.count(4) print(count)

42

21
4/17/2023

43

Write a Python program to reverse a tuple


#create a tuple
x = ("w3resource")
# Reversed the tuple
y = reversed(x)
print(tuple(y))
#create another tuple
x = (5, 10, 15, 20)
# Reversed the tuple
y = reversed(x)
print(tuple(y))

44

22

You might also like