0% found this document useful (0 votes)
4 views

Tuple

Uploaded by

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

Tuple

Uploaded by

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

Python – List & Tuples

Farhana Sarker, PhD. Associate Professor


Highlight

• Tuples – what is it!!??


• Why we use tuples?!.

• Construction (memory and index) of a tuple.

• Tuple operations (basic).


• Tuple operations (build in functions).

• Tuple operations (tuple functions).


tup1 = (1, 2, 3, 4, 5)

tup1

Tuple 1 2 3 4 5
Tuple – What is it?!
▪ A tuple is a sequence of immutable (that cannot be changed) Python objects.
▪ Tuples are sequences, just like lists.
▪ The main difference between the tuples and the lists is that
▪ the tuples cannot be changed
▪ unlike lists. Tuples use parentheses, whereas lists use square brackets.

Example:
Memory
tup1 = ('physics', 'chemistry', 1997)
tup2 = (1, 2, 3, 4, 5 )
tup2 1 2 3 4 5
tup3 = "a", "b", "c", "d"
Tuple – Declare
Output
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
('physics', 'chemistry', 1997, 2000)
tup3 = "a", "b", "c", "d"
(1, 2, 3, 4, 5)
('a', 'b', 'c', 'd')
print(tup1)
print(tup2)
print(tup3)

The empty tuple is written as two parentheses containing nothing.

tup_empty = ()
Empty tuple: ()
print("Empty tuple: ", tup_empty)

To write a tuple containing a single value you have to include a comma, even though there is only
one value.
(50,)
tup4 = (50,)
print(tup4)
Tuple – Indexing index or indices

Tuple indices start at 0 (Left to right). Memory


Memory
Tuple indices start at -1 (right to left). Location
Left to right
0 1 2 3 4

tup1 = (1, 2, 3, 4, 5,6) tup1 1 2 3 4 5


print(tup1)
-5 -4 -3 -2 -1
print(tup1[1])
Output Memory
print(tup1[1:3]) Location
Right to left
print(tup1[2:]) (1, 2, 3, 4, 5, 6)
2
print(tup1[-2])
(2, 3)
print(tup1[-3:]) (3, 4, 5, 6)
5
print(tup1[-4:-1]) (4, 5, 6)
print(tup1[-4:]) (3, 4, 5)
(3, 4, 5, 6)
Tuple – Why?
▪ To store ordered list of items that requires no change, e.g., employee id, name, gender.

▪ To carry out different useful operations on the list, e.g., sorting the marks, find min/max
marks or price

▪ Immutable data, meaning accidental change / modification are not possible, e.g.,
coordinates of a location.

▪ Tuples are stored in a single block of memory.

▪ Tuples are immutable so; It doesn't require extra space to store new objects.

▪ It is the reason creating a tuple is faster than List.

▪ It also explains the slight difference in indexing speed is faster than lists, because in
tuples for indexing it follows fewer pointers.
Tuple – Update
Tuples are immutable, which means you cannot update or change the values of tuple elements.
You can take portions of the existing tuples to create new tuples.

Output

tup1 = (12, 34.56) (12, 34.56, 'abc', 'xyz’)


tup2 = ('abc', 'xyz')
Traceback (most recent call last):
# let's create a new tuple as follows File "c:\Users\M SYEED\OneDrive - American
tup3 = tup1 + tup2 International University-
print (tup3) Bangladesh\Desktop\Lab19\tuple.py", line 11,
in <module>
tup1[0] = 100
# Following action is not valid for tuples TypeError: 'tuple' object does not support item
tup1[0] = 100 assignment
Tuple – Delete
Use the del statement if you know exactly which element(s) you are deleting.

Output???
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
Traceback (most recent call last):
# delete entire tuple is allowed File "c:\Users\M SYEED\OneDrive - American International
del(tup2) University-Bangladesh\Desktop\Lab19\tuple.py", line 8, in
<module>
# delete single item is not allowed del(tup1[0])
del(tup1[0])
TypeError: 'tuple' object doesn't support item deletion
print('After deleting..')
print(tup1)
Tuple – Basic Operations
Tuple – Basic Operations
tup = ('physics', 'chemistry', 1997, 2000, 'Raining!', 1976)
print ('Total items in tuple: ', len(tup))

intuple = 'chemistry' in tup


Output???
print('is chemistry in the tuple: ', intuple)

for item in tup: Total items in tuple: 6


print(item, end =' ')
is chemistry in the tuple: True
print(‘\n tup id: ', id(tup)) physics chemistry 1997 2000 Raining! 1976

tup2 = (1,2) list id: 2414962255968


tup = tup + tup2 ('physics', 'chemistry', 1997, 2000, 'Raining!', 1976, 1, 2)

print(tup) tup id: 2414962623424


print('tup id: ', id(tup)) tup2 id: 2414962615616
print('tup2 id: ', id(tup2))
(1, 2, 1, 2, 1, 2)
tup2 = tup2*3
list2 id: 2414962257312
print(tup2)
print('list2 id: ', id(tup2))
Built in Functions
Tuple – Build in function
Tuple – Build in function
tup1, tup2 = ('C++','Java', 'Python'), (456, 700, 200)
Output???
print ('Total items in list: ', len(tup1))
print ("Max value element : ", max(tup1))
print ("Max value element : ", max(tup2)) Total items in list: 3
print ("Min value element : ", min(tup1)) Max value element : Python
print ("Min value element : ", min(tup2)) Max value element : 700
Min value element : C++
aList = [123, 'C++', 'Java', 'Python']
print("Before conversion: ", aList) Min value element : 200

convertedToTuple = tuple(aList) Before conversion: [123, 'C++', 'Java', 'Python']


print ("After conversion: ", convertedToTuple) After conversion: (123, 'C++', 'Java', 'Python')
Before conversion: Hello World
str="Hello World"
After Conversion : ('H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd')
print("Before conversion: ", str)

strToTuple=tuple(str)
print ("After Conversion : ", strToTuple)
Tuple
Exercise
Check Your understanding
Output???
tup = ('apple', 75, 4.89, 'Abdullah', 'Dhaka')

tuple1 =tuple()

tuple1 = tup

print('tup: ', tup)


print('tuple1: ', tuple1)

print('id of tup: ', id(tup), '\nId of tuple1: ',id(tuple1))

tup2 = (5, 6, 'hello')

tup = tup + tup2

print('id of tup: ', id(tup), '\nId of tuple1: ',id(tuple1))

tuple1[1] = 'pineapple'
print('tuple1: ', tuple1)
tup = ('physics', 2001, 'chemistry', 1997, 'Rainy day!', 1976)
print (len(tup))
intuple = 'programming' in tup
print('Is programming in the tuple: ', intuple)

for item in tup:


print(item, end =' ‘)

print(tup)
tup2 = (1,2)
tup = tup + tup2
print(tup)
tup2 = tup2*3
print(tup2)

tup_merged = tup[-4:] + tup2[3:5]


print(tup_merged)
print(tup_merged[2:5])
Write a Python program to accomplish the followings:

1. Create a tuple with name numbers and insert 10 numbers in it.

2. Create a tuple with name cities and insert 5 name of the cities.

3. Create a tuple with name mixed and insert following values: apple, 75, 4.89, Abdullah, Dhaka

4. Now print values from all the tuples.

5. Find the minimum, maximum and avg from the tuple numbers, and print them.

6. Get the summation of the numbers from tuple mixed and print the summation result.

7. Change the value “apple” in mixed tuple to “organge”.

8. Merge the two tuples numbers and cities together and print values of the new list.

9. How many items are there in the new tuple? Show the total number of items.

You might also like