Starting from:

$30

COMP1007 Assignment 1 Solved

Part 1: Python Concepts

 

Remark: Please try to answer the questions in Part 1 without running the codes in a real Python environment. You can review the lab sheet to refresh the basic Python concepts before you work on these questions.

 

1.  Fill in the correct Python data type (such as int, float, str, bool, tuple, list, dict) for the following data items: (10 marks)

 

Data Item
Data Type
"COMP1007"
 
3.14159
 
("Jack", 174.0, 62.0, 65789084)
 
["Jack", 174.0, 62.0, 65789084]
 
"False"
 
False
 
'''"Hi, I am Jack!"'''
 
1000
 
{0:"A", 1:"B", 2:"C"}
 
(['a', 'b', 'c'], (1, 2, 3))
 
 

2.  Which of the following Python statements are wrong? (10 marks)

 

Python statements
Correct (√) or Wrong ( X )
PI = 3.14159
 
True = 1
 
TRUE = 1
 
x = ('a', 'b', 'c') x[0] = 'd'
 
x = ['a', 'b', 'c'] x[0] = 'd'
 
 

             

3.  What will be the output of the following Python statements? (20 marks)

 

Python statements
Output
x = (5, 7, 9, 11) print(x[3])
 
y = range(-5, 5, 2) print(list(y))
 
myList = [10, 20, 30, 40] myList.insert(2, 50) print(myList)
 
name = ['Tom', 'Ada', 'Jack', 'Zoe'] name.sort() print(name)
 
dict = {'001':'Tom', '002':'Ada', '003':'Jack'} dict['002'] = 'Zoe' print(dict)
 
 

Part 2: Python Programming

 

4.  Write a segment of Python statements that does the followings:

(1) To prompt the user to input his/her name and read in the name; (2) To prompt the user to input his/her age and read in the age;

(3) Show a message that includes the user’s name and age. An example output of the program is given as follows:

Please input your name: CHAN DAI MAN

 

Please input your age: 30

 

Hello, CHAN DAI MAN! You are 30 years old.
In the above example, “CHAN DAI MAN” and “30” are typed in by the user.

(20 marks)

 

Write your Python program here:

 
 

5. Write a segment of Python statements that does the followings:

(1)             To generate a range myRange that includes the following integers: -100, -96, -92, …, 0, 4, 8, …, 96, 100.

(2)             To generate a list myList from myRange. So myList should also include the integers -100, -96, -92, …, 0, 4, 8, …, 96, 100.

(3)             To generate a slice mySlice by slicing from myList, such that mySlice includes the integers 0, 4, 8, …, 96, 100.

(4)             To generate a slice myNewSlice by slicing from myList, such that myNewSlice includes the integers 0, 8, 16, 24, …, 96.

(20 marks)

 

Write your Python program here:

 

 

6. In this problem, you are required to design a key-value pair structure that can store the useful information of a store in a shopping mall, such as name, contact phone number, and opening hours. You can design a store ID as the key. In your Python program, please create a dictionary to store the information (i.e., key-value pairs) of at least four stores. Then please prompt the user to input a store ID, and your program should then output the contact phone number of that store. We assume the user always inputs a valid store ID.

(20 marks) Write your Python program here:

More products