exercise1.py
= ["cat", "dog", "elephant", "minnow", "beaver"]
animal_list
print(animal_list)
['cat', 'dog', 'elephant', 'minnow', 'beaver']
This page contains a number of exercises to give you a chance to practise what you have learned this session. You should create a new .py
Python file for each of them.
Create a list containing 5 different animal types, for example:
Print out the list to the screen.
Start by copying the code that you used to create the list of animals in the last exercise. Write a loop which will print out each of the animals, prefixed with Species:
. For example the output could look like:
Species: cat
Species: dog
Species: elephant
Species: minnow
Species: beaver
Create a list containing 10 different numbers, ranging from 0 to 100. For example: 65, 54, 17, 78, 66, 24, 32, 80, 79, 95.
Write a loop which will print out only those numbers which are larger than 50.
Create a loop which iterates over the numbers from 1 to 20 (inclusive). Inside the loop: - if the number is divisible by three then print “ook”, - if the number is divisible by both three and five then print “foo”, - and if the number is not divisible by either then just print the number.
Hint: You can use the %
operator to find the remainder from a division. Also, take care in the order that you do your if
-else
.
When creating a chain of if
-elif
-else
, you should put the most specific checks first. Otherwise they will be swamped by the more general checks.
Let’s end this session with a more difficult challenge. We’re going to make a calculator which can read numerical operations from the input and print the answers to the screen.
For example, it should print out an input prompt like
>
into which you can type a calculation as
> 4 * 6
and it should then print out the answer like
> 4 * 6
4 * 6 is 24
Examples of inputs it should be able to accept are:
4 * 6
5 + 6
457 - 75
54 / 3
4 + 6
Notice that all the input strings have spaces around the operator, this will make your life much easier if you assume this.
One extra function you will need to do this is the split
function which takes a string and returns a list containing the string, split by spaces. Also, remember the int
function which can convert a string into an integer.
calc.py
could look like:
calc.py
calculation = input("> ")
parts = calculation.split() # Split e.g. "4 * 6" into ["4", "*", "6"]
lhs = int(parts[0]) # Extract e.g. "4" and turn it into 4
operation = parts[1] # Extract e.g. "*"
rhs = int(parts[2]) # Extract e.g. "6" and turn it into 6
if operation == "+":
print(calculation, "is", lhs + rhs)
elif operation == "-":
print(calculation, "is", lhs - rhs)
elif operation == "*":
print(calculation, "is", lhs * rhs)
elif operation == "/":
print(calculation, "is", lhs / rhs)
> 4 * 6
4 * 6 is 24
> 5 + 6
5 + 6 is 11
> 457 - 75
457 - 75 is 382
> 54 / 3
54 / 3 is 18.0
The code above works fine, but there’s always more than one way to approach a problem like this.
One thing that we could improve would be the repetition in the print
lines. Each of them are almost the same as each other and if we wanted to change the output from
4 * 6 is 24
to something like
4 * 6 = 24
then we’d have to edit all four lines of code.
Remembering our three-part pattern from earlier in the course of input→calculation→output, it’s a good idea to split out the calculation of data from the printing and display of data. In our case we could change it to look like:
calc.py
calculation = input("> ")
# Prepare the parts
parts = calculation.split()
lhs = int(parts[0])
operation = parts[1]
rhs = int(parts[2])
# Calculate the answer
if operation == "+":
result = lhs + rhs
elif operation == "-":
result = lhs - rhs
elif operation == "*":
result = lhs * rhs
elif operation == "/":
result = lhs / rhs
# Output the result
print(calculation, "is", result)
> 4 * 6
4 * 6 is 24
> 5 + 6
5 + 6 is 11
> 457 - 75
457 - 75 is 382
> 54 / 3
54 / 3 is 18.0