Python Lab 1
Python Lab 1
These exercises are for the most part taken from the book "Python for Everyone" by Cay
Horstmann and Rance Necaise (2014), Sections 1.4, 1.5 and 1.6 (PFE).
1. Getting Started
Create a new folder in your disk space with the name PythonLab1.
If you are in the ITS laboratory MAL 109 then click on the Start icon in the
lower left corner of the screen. A list of files in alphabetical order should appear.
Click on Python 3.4. In the drop down menu click on
A window with the title Python 3.4.4rc1 Shell should appear. This window is the
Shell.
In the Shell click on File. A drop down menu will appear. Click on New File. A
window with the ‘title’ Untitled should appear. This window is the Editor.
In the Editor, click on File, and then in the drop down menu click on Save As… .
A window showing a list of folders should appear. To search any folder on the
list, double click on the folder. Find the folder PythonLab1 and double click on it.
In the box File name at the bottom of the window type HelloWorld.py, and then
click on the button Save in the lower right corner of the window. The title of the
Editor should change to show the location of the file HelloWorld.py.
You are now ready to write the code for your first program. In the Editor type
# My first Python program
print("Hello World!")
1
left justified and on the first line. Notice the colour coding: red for the comment,
purple for the function print() and green for the data "Hello World!".
In the Editor click on Run, then on the drop down menu click on Run Module. A
window will appear with the message Source Must Be Saved. Click on OK. Two
things happen: your program is saved in the file HelloWorld.py and then it is run
to produce the text "Hello World!" in the Shell. The text in the Shell is blue to
show that it is output.
3. Syntax errors
Print(Hello World!)
on a new line and left justified. As before, click on Run Module. A window will
appear. Click on OK.
print("Hello World!)
and click on Run Module. A window will appear. Use a web search engine to find
out if others have obtained the same error message. Click on OK.
4. Print statements
What do the following statements print when they are executed together?
print("Hello")
print("World!")
What do the following statements print when they are executed together. Note the
indentation.
print("Hello")
print("World!")
print(1/0)
2
5. Evaluation of arithmetical expressions
A bank account contains $10,000 and earns interest at the rate of 5 percent per year.
The interest is added to the account. (See PFE Section 1.7).
After one year the bank account contains the original sum of $10,000 plus the
interest, which is 5% of $10,000, namely
10,000 *(5/100).
Find the balance of the account after two years using the code
How many years does it take for the account balance to be at least double the original
balance?
6.1. Write a program that prints the sum of the first 10 positive integers (1+2 +…
+10). Print the sum and also print some text to explain the significance of the
printed number. (PFE P1.2)
6.2. Write a program that displays your name in a box on the screen, like
______
| Dave |
|--------|
(PFE P1.5)
6.3. Write a program that prints a two-column list of your friends’ birthdays. In
the first column, print the names of your friends; in the second column, print their
birthdays. (PFE P1.14)
6.4. In order to estimate the cost of painting the exterior of a house, a painter
needs to know the surface area to be painted. Develop an algorithm for computing
that value. Your inputs are the width, length and height of the house, the number
of windows, the number of doors and their dimensions (assume that the windows
are all the same size and the doors are all the same size). The roof is not painted
and the windows and doors are not painted.
3
Use variables as much as possible. For example, the variables w, l, h could be
used for the width, length and height, respectively. Assign values to the variables
in this style, w = 6, and then calculate using the variables. For example, if a house
has a width 6 and a height 3 then assign values w = 6, h = 3 and then calculate the
surface area of a wall with width w and height h using the formula w*h. (PFE
R1.14)