Topic 10
Topic 10
and Dictionary)
Python Programming
CT108-3-1-PYP
TOPIC LEARNING OUTCOMES
Tuple
Create Adding an element Accessing
Set
Create Accessing
Dictionary
• Tuples are like lists except they are immutable like strings.
• Once created, elements cannot be changed.
• If the elements of a list in the application do not change, use a tuple
to prevent data from being modified accidentally.
• Tuples are more efficient than lists.
• IMPORTANT! tuples can be accessed like lists. However, tuples are
NOT list.
• Collection of objects which is ordered and unchangeable
• In Python tuples are written with round brackets (parentheses), rather
than square brackets
• The parentheses are optional but is a good practice to write it.
Python Programming Tuple, Set and Dictionary SLIDE 4
CT108-3-1-PYP
Tuples are “immutable”
• Unlike a list, once you create a tuple, you cannot alter its contents - like
a string
x = (3, 2, 1)
x.sort()
print(x)
AttributeError: 'tuple' object has no attribute 'sort’
x.reverse()
AttributeError: 'tuple' object has no attribute 'reverse’
x.append(5)
AttributeError: 'tuple' object has no attribute 'append
l = list()
print(dir(l))
['append', 'count', 'extend', 'index', 'insert', 'pop',
'remove', 'reverse', 'sort’]
t = tuple()
print(dir(t))
['count', 'index']
tuple = ()
Empty tuple
print(tuple)
tuple = (1, 2, 3)
Tuple with elements
print(tuple)
• Slicing
fruits = (“Cherry”, “Guava”, “Mango”)
print(fruits[1:3]) (Guava, Mango)
print(fruits[:-1]) (Cherry, Guava)
print(fruits[2:]) (Mango)
print(fruits[:])
(Cherry, Guava, Mango)
• Tuples are unchangeable, so you cannot remove items from it, but
you can delete the tuple completely
• Python provides two built-in methods that you can use on tuples.
1. count() Method
2. index() Method
Sets can be used to perform mathematical set operations like union, intersection, symmetric
difference etc.
string = list(string)
for element in unique:
string.remove(element)
• Sets can be used to carry out mathematical set operations like union,
intersection, difference and symmetric difference.
• It can be done with operators or methods
Method Operator Description
setA = {1,2,3,4,5}
setB = {3,4,5,6,7}
List Dictionaries
A linear collection of values that stay in A “bag” of values, each with its own
order. label
A key
Each pair of entries contains
A value
• A={1:"one",2:"two",3:"three"}
1 one
A= 2 two
3 three
KEY VALUES
Python Programming
S
Tuple, Set and Dictionary SLIDE 32
CT108-3-1-PYP
Creating a Dictionaries
Can add new items or change the value of existing items using assignment
operator.
This method removes as item with the provided key and returns the value.
All the items can be removed at once using the clear() method.
• Use for loop to iterating through each of keys and values in the
dictionaries:
marks = {"python": 87, "OS": 80, "DB": 70}
freq = {}
for ch in text:
freq[ch] = freq.get(ch,0) + 1
Function Description
len() Return the length (the number of items) in the
dictionary
marks = {"python": 87, "OS": 80, "DB": 70}
print(len(marks))
sorted() Return a new sorted list of keys in the dictionary
print(sorted(marks))
• Dictionaries can contain key: value pairs where the values are again
are dictionaries
Malaysia = {"Johor": {"capital": "Johor Bahru"},
"Kedah":{"capital": "Alor Setar"}}
Tuple
Create Adding an element Accessing
Set
Create Accessing
Dictionary