NANYANG TECHNOLOGICAL UNIVERSITY
School of Electrical & Electronic Engineering
IE2108 Data Structures and Algorithms in Python
Tutorial No. 01 (Sem 1, AY2023-2024)
Before attempting these problems, please install Jupyter Notebook (see installation instructions at
the end of Week 1 Lecture Notes). In Jupyter Notebook, write and run python programs to do the
following:
----
Create the following variables:
p = "Hello Singapore!"
pp = "I'm learning Python."
q = 10
r = 10.2
----
Display the variables, just make sure the variables are as what I expect.
----
Display the data type (or class) of each variable.
----
Display p + pp .
----
Display q + r .
----
Display range(10) .
----
Display list(range(10)) .
----
Modify the above statement to display [1, 3, 5, 7, 9].
----
1
Modify the above statement to display [20, 18, 16, 14, 12].
----
Create a list b with the following elements: 'data', 'and', 'book', 'structure', 'hello', 'st'.
Display it to make sure that your command worked.
----
Append a number 32 to the end of the list and verify your command works.
----
What is the meaning of b[2:3]?
----
Remove the 3rd element of the list.
----
Use a different command to remove the 1st element of the list.
----
Display the number of elements in the list (the length of the list).
----
For the following 2 values, check whether both are greater than 0
a = 32
b = 132
----
For the following 2 values, check whether at least one is greater than 0.
a = 32
b = -32
----
What data type is person defined below?
person = {}
----
Display person after the following.
2
person['firstname'] = 'Jacky'
person['lastname'] = 'Chan'
person['age'] = 69
person['address'] = ['Hong Kong']
----
Display his first name.
----
Write a basic calculator that performs addition, subtraction, multiplication, and division.
----
Write a simple game where the user tries to guess a randomly generated number.
The program first selects a random number without telling you the number.
You make a guess. The program will tell you if too high or too low, until
you guess it right.
Hint: Use the following:
import random (for importing a library on random numbers)
random.randint(1, 100) (for generating a random integer between 1 and 100, including both 1 and
100 themselves.)
----
Generate and print the first n numbers in the Fibonacci sequence
which is 0, 1, ..., with each subsequent number equal to the sum of
the previous 2 numbers.
----
Check if a given string is a palindrome (reads the same forwards and backwards).
----