Lecture03 - Basic Grammar (data, list, dic)
Lecture03 - Basic Grammar (data, list, dic)
1
Learning goal
2
Data Type
3
Data Type
4
String
5
How can we save/handle data to a single variable?
Let’s learn
• List
• Tuple
• Dictionary
• Set
6
List
7
List
8
Tuple Tuples are fixed size in nature whereas lists are dynamic.
In other words, a tuple is immutable whereas a list is mutable.
o Performance:
• Tuples are faster than lists. Use a tuple when dealing
with a fixed set of values that will only be iterated
through.
o Dictionary Keys:
• Tuples with only immutable elements (e.g., strings,
numbers, other tuples) can be used as dictionary keys.
• Lists cannot be used as dictionary keys because they
are mutable.
9
Tuple
Quiz:
a = 2, 4, 5, 5, ’x’, ’y’
len(a) Ans:
a.count(5) Ans:
a.index(‘x’) Ans:
10
Dictionary a = dict()
a = {}
11
Dictionary
12
Dictionary
13
Exercise
14
Set
‘Set’ is not commonly encountered but can be very useful. It implements the mathematical
concept of a set. A set can be created from a tuple or list.
set1 set2
15
QUIZ
1. Write the type of ‘a’ 2. How to make a joining sets into one new set,
called set5, using set1, set2, set3, set4?
o a = (1,2,3,4,5)
o a = [1,2,3,4,5]
o a = {1,2,3,4,5}
16
Summary
17