MS1008-Tutorial 6

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Introduction to Computational Thinking

MS1008

Week 6
Three Common Data Structures in Python
Lists
A List is an ordered sequence of items.

L = list() Creates an empty list

L = list(arg) Takes an iterable data structure as an


argument and add each item of arg to the
constructed list
Program: Result:

A list is created using square brackets with commas separating items.


Program:

Result:
Lists are mutable:
newList [0] = [1,2,3] or newList[:2] = [27] or newList[:2] = 27
print(newList)  error?
Lists vs Strings
Lists: Similarities with Strings

Lists: Differences with Strings


Lists Structures

Indexing on Lists Lists of Lists


Lists operators and functions
Constructing List

List operators List Functions


Lists as Mutable

Program: Program:

Result:
Result:

Tips: newStr= myStr.replace('a', 'z') #make a new string


List Methods

Example:
Tuples

Lists vs. Tuples


Commas Create Tuples

 Expendable!
Dictionary
In data structure terms, a dictionary is better termed as an associative
array, or associative list, or a map.

Dictionaries are collections but they are not sequences like lists,
strings, or tuples.
• There is no order to the elements of a dictionary.
• In fact, the order (for example, when printed) might change as
elements are added or deleted.
Key:Value
You can think of it as a list of pairs.
- The key, which is the first element of the pair, is used to
retrieve the second element, which is the value.

Thus, we map a key to a value.

The key acts as a “lookup” to find the associated value.

Just like a dictionary, you look up a word by its spelling to find the
associated definition.

A dictionary can be searched to locate the value associated with a


key.
Dictionary
Access to Dictionary

Dictionaries are Mutable


You can change the object via various operations, such as index
assignment.
Dictionary
Dictionary Operations

Other Methods and Operations Iterating on a Dictionary


Problem 1

1) Write a Python code to create a list of the squares


of all numbers between 1 and 20.

2) What will be the return of the following list?


[ (2*n) for n in range(13) if (n%2==0)]

3) Tuples are immutable. Yet, we can modify the


tuple in the list L = [1, (2, 3), 4] to L = [1, (5, 6), 4].
Why?
Solution to Problem 1
1. Write Python code to create a list of the squares of all numbers between
1 to 20.

Ans: [i*i for i in range(1, 21)]

2. What will the following list return?


[ (2*n) for n in range(13) if (n%2==0)]

Ans: [0, 4, 8, 12, 16, 20, 24]

3. Tuples are immutable. Yet, we can modify the tuple in the list L = [1, (2, 3),
4] to L = [1, (5, 6), 4]. Why?

Ans: The tuple (2, 3) in the list is an object on its own. When we change
the tuple in the list, we are not changing the contents of the tuple but rather
replacing it with a new object which is a different tuple (5, 6). Because lists
are mutable, we can change its elements.
Problem 2

You can express a 3D vector as a tuple (x, y, z).


Write a program that:
i. sums two such vectors, and returns the result in a new
vector.
ii. Find the dot product of two such vectors.
Solution to Problem 2
You can express a 3D vector as a tuple (x, y, z).
Write a program that:
i. sums two such vectors, and returns the result in a new vector.
ii. Find the dot product of two such vectors.

Ans:  
v1 = (1, 2, 3) # define two vectors with arbitrary values
v2 = (6, 7, 8)
 
v3 = (v1[0]+v2[0], v1[1]+v2[1], v1[2]+v2[2])
print("The sum of the vectors: ", v3)
 
dot = v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]
print("The dot product is: ", dot)
Problem 3

Write a program that converts a date given in a string in the


short format “dd/mm/yyyy” to a list containing the three fields,
(dd, mm, yyyy).

Solution to Problem 3

Ans: dateString = input("Enter date string dd/mm/yyyy: ")


dateList = dateString.split("/")
print(dateList)
Problem 4(Optional)

Given a date in the short format “dd/mm/yyyy”,


print it in the long form “dd Month, yyyy” where “Month”
is the month in word.

For example, “1/1/2018” is printed as “1 January, 2018”.

Solve this problem using the Python dictionary:

month = {1:“January”, 2:“February”, 3:“March”, 4:“April”,


5:“May”, 6:“June”, 7:“July”, August“, 9:”September“,
10:”October“, 11:”November", 12:"December"}.
Solution to Problem 4
month = {1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 6:"June",
7:"July", 8:"August", 9:"September", 10:"October", 11:"November",
12:"December"}

# Read in a date string dd/mm/yyyy. Split into 3 bits and store in date list
date = input("Enter date string dd/mm/yyyy: ").split("/")

if len(date) != 3:
print("Wrong date format")
elif date[0].isdigit() and int(date[0]) >0 and int(date[0])<32 :
if date[1].isdigit() and int(date[1]) >0 and int(date[1])<13:
if date[2].isdigit():
# right format
print("The long date is {} {}, {}". format(date[0], \
month[int(date[1])], date[2]))
else:
print("Wrong year format")
else:
print("Wrong month format")
else:
print("Wrong day format")

You might also like