0% found this document useful (0 votes)
136 views

Python Lab 1

This document provides instructions for completing Python Lab 1 exercises on creating a first Python program. Students are instructed to: 1) Set up a Python development environment and create a file to hold their first program. 2) Write and run a simple "Hello World!" program. 3) Experiment with syntax errors and print statements. 4) Calculate compound interest on a bank account over multiple years using arithmetic expressions. 5) Additional exercises are provided to reinforce concepts.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

Python Lab 1

This document provides instructions for completing Python Lab 1 exercises on creating a first Python program. Students are instructed to: 1) Set up a Python development environment and create a file to hold their first program. 2) Write and run a simple "Hello World!" program. 3) Experiment with syntax errors and print statements. 4) Calculate compound interest on a bank account over multiple years using arithmetic expressions. 5) Additional exercises are provided to reinforce concepts.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Birkbeck College

Department of Computer Science and Information Systems

Introduction to Programming (ITP)


Autumn 2019 and Spring 2020
st
Week 1: 1 October 2019 or 17 January 2020

Python Lab 1 – My First Program

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.

 Launch the Python Integrated Development Environment IDLE. If you are in a


DCSIS laboratory then click on the Search icon (like a magnifying glass) in the
lower left corner of the screen. Search using the keyword Python. In the list of
best matches click on IDLE (Python 3.6 64-bit). A window with the title Python
3.6.2 should appear. This window is the Shell.

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

IDLE(Python 3.4 GUI-64bit)

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.

2. Write and run your first program

 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

 In the Editor type

Print(Hello World!)

on a new line and left justified. As before, click on Run Module. A window will
appear. Click on OK.

 In the Editor, replace Print(Hello World!) with

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.

Remove the erroneous statements from the Editor or correct them.

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!")

 What does the following statement print?

print("My lucky numbers are", 3*4+5, 5*6-1)

 What is the error in the following statement?

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).

Thus after one year the account balance is


10,000+10,000*(5/100)=10,000*(1+(5/100))=10,000*1.05

Print out the value of 10,000*1.05 using the code

print("balance after one year: ", 10000*1.05)

Find the balance of the account after two years using the code

print("balance after two years: ", 10000*1.05*1.05)

How many years does it take for the account balance to be at least double the original
balance?

6. Supplementary questions for private study

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)

You might also like