Assignment 01
Assignment 01
General Instructions
Make sure that you read and then follow the instructions for each task carefully.
Please make sure that you save all of your work to a safe place and that you make regular back-ups.
You should begin all tasks with the following steps unless otherwise stated:
• Create a new Python file with the Project Name specified in the file name – for example, the
first task would have a file called hello_world.py.
• It is suggested that you save the file with your completed code (and/or any other files
specified) in a folder named for the tutorial assignment (assignment01, assignment02, etc.),
which should itself be in a root folder called Python. However, you can use whatever
method you wish to organise your work – just make sure that each file can be located
quickly and efficiently based on the assignment number and the project name.
If you are in any doubt please check with your tutor.
Hello World
Project Name: hello_world
Step 1: Create a new folder
• Create a folder called python
• Create a subfolder within python called assignment01
Folder names can be as long as you like but it is best to choose short but
meaningful names. By convention you should use lower-case letters with
underscores where clarity requires.
NB: The following examples assume that you have created a folder called
python containing a subfolder called assignment01.
Step 2: Create a Python project using a suitable Python editor such as
PyCharm or IDLE
• Using the information given in the lecture create a new file within
your assignment01 folder called hello_world.py
Step 3: Write source code
• Type in the following code
# output Hello World
print("Hello World")
print("Welcome to Python")
• Get into the habit of putting a comment at the start of the program
to explain what the application does.
Step 4: Run the program
• Using the information given in the lecture run the hello_world
program after correcting any errors that may occur.
Step 5: Take a screen shot of the output
1
Introduction to Python Assignment 1
Debugging code
Project Name: debug
Step 1: Write the source code
• Add a new Python file called debug.py
• Copy and paste the code below
# exercise to debug code
print("I am " + str(age) + " years of age with a", activity level, "activity level"
# add one character and replace one character
2
Introduction to Python Assignment 1
Simple Calculator
Project Name: simple_math
Step 1: Create a new Python script file
• Create a new file called simple_math.py in your assignment01 folder
• Type the following code into your file
age = 2
print(age)
age = 21
print(age)
age = age + 1
print("Age next Birthday: " + str(age))
Step 2: Run your program
• Run the program and make sure that you are getting the output that
you are expecting.
Step 3: Modify your code
• Change the type of age on the third line to a float (you are not to do
this by changing 21 to 21.0 directly – find another way).
3
Introduction to Python Assignment 1
Breakeven
Project Name: breakeven
The break-even point for a business is defined as the point where the total
revenue equals the business expenses (fixed costs). The following values are
required to calculate this:
• What is the cost to produce each item
• What is the sale price per item (item cost + profit per item)
• What are the fixed costs
Dividing the fixed costs by the profit per item (the difference between the cost
to produce each item and the sale price) gives the number of items that must
be sold before "breaking even".
For example, if it costs £50.00 to produce an item which is then sold at
£100.00, there is £50.00 profit per item. If the fixed costs are £1000.00, the
company would need to sell 20 items to break even.
Exercise data:
• Cost to produce each item = £20.00
• The sale price per item (item cost + profit per item) = £40.00
• Fixed costs = £50000.00
Write a program to output the data with a meaningful label for each item and
calculate and output the number of items we need to sell to breakeven. The
output should look similar to the following (cropped for convenience).
4
Introduction to Python Assignment 1
𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓_𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐
Hint: 𝑏𝑏𝑏𝑏𝑏𝑏𝑏𝑏𝑏𝑏_𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒 =
𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠_𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝 − 𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖_𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐
Sweet Tooth
Project Name: sweet_tooth
A teacher has bought a packet of 40 sweets that she is going to share out
equally between her 14 students. Because a single sweet cannot be shared,
the teacher will keep what is not given to the children.
The teacher will keep the minimum number of sweets possible (obviously this
scenario is not based on real life).
Write a program to determine and output the number of sweets each child
will receive. As a single sweet cannot be shared between pupils, the program
must calculate and output the number of sweets per child and the number
that the teacher keeps for herself. The calculation must be done using the
remainder (%) operator.
This will give you the amount left over after the division has been carried out.
For example: 5 % 2 = 1 (5 divided by 2 is 2 with 1 left over)
Take a screen shot of the finished output saved as sweet_tooth.jpg in your
w01_t2 folder.
5
Introduction to Python Assignment 1
Cans of Paint
Project Name: cans_of_paint
To help you complete this task you should use the Python math module. You
can consult the online documentation to see what methods this will make
available to you:
https://docs.python.org/3.0/library/math.html
To use a module within your program you must import it using the import
keyword. By convention, import statements occur at the beginning of a file.
import math
You will need some of the methods from that module.
For example, to call the ceil() method, you type:
math.ceil(a_number_or_variable)
e.g.: math.ceil(num)
Write a program that uses methods from the math module to solve the
following problem.
A can of paint covers 5.1 m2 of wall. Each can of paint has a diameter of 15 cm
and a height of 30 cm. The shop that sells the paint helps customers by
packing the cans into boxes with internal measurements of 0.60 x 0.30 x 0.35
metres (L x W x H).
Solve and output the following:
1. The minimum number of cans that must be bought to paint the
walls of a hall whose floor measures 40 x 30 metres, and whose
ceiling is 3.4 metres above the floor (use an appropriate method
from the Math class).
2. The number of full boxes given to the customer who buys this
quantity of paint (use an appropriate method from the Math class
rather than integer division).
3. The number of cans not packed into boxes.
You should get the following amounts
6
Introduction to Python Assignment 1
2. debug.py, debug.jpg
3. datatypes.txt
4. simple_math.py, simple_math.jpg
5. breakeven.py, breakeven.jpg
6. sweet_tooth.py, sweet_tooth.jpg
7. cans_of_paint.py, cans_of_paint.jpg