You are on page 1of 5

FIT1053 Algorithms and Programming Fundamentals in Python –

Workshop 1.

Objectives
After this workshop, you should be able to:
• Use the Python interpreter to run code.
• Execute a file using IDLE.

• Use experimentation as a learning tool.


• Manipulate numbers and strings using basic numerical and string operators.
• Import packages.

• Identify and fix common Python coding errors.


MARKS: For FIT1045 students: Task 1: Maths, Task 2: Booleans, Task 3: Temperature Conversion, and
Task 4: Name Factoids are each worth 0.5 marks. For FIT1053 students: Tasks 1-4 are worth 1 mark together,
Task 5 is worth 1 mark.
To receive the allocated mark you must make a genuine attempt at the task. You must download the Python
file workshop01.py from Moodle and make and save the changes outlined in the tasks.

There is lots to read in this Workshop, and it is important that you read all of it. If you feel overwhelmed
by how much information there is, try working through the workshop lesson on Moodle. The same information
is given, but in bite-sized chunks.

Assessment
As part of this unit, workshops are assessed.
• Every workshop contributes 2 marks to a maximum of 16 marks (for a total of 16% of the final grade).
There are nine assessed workshops. Thus, getting the full mark for eight out of the nine workshops will
get you the full 16%. Or, getting the full mark for seven of the workshops and 1 mark for the remaining
two will get you the full 16%.

• From Workshop 2, your mark will be based on an automated tester that you will be given each week on
Moodle. For workshop 1, the mark is based on your attempt at the given workshop tasks.
• From Workshop 2, each assessed workshop will also contain a challenge question worth 3 marks (for
3% of the final grade). The marks will be broken down as 1 mark for the correct implementation, and 2
marks for your performance in a short interview. In the interview you will be expected to answer detailed
questions on your code. You may attempt the extension question in as many weeks as you wish, and
your final mark will be the best mark you receive. You may work with your peers to solve the extension
question, but you will be interviewed and graded individually.
• If you do not finish all tasks in the workshop during class you are encouraged to continue working on the
tasks individually or with your peers from class. The workshop sheet is assessed during the workshop of
the following week; with the exception of workshops for Weeks 6, 11 and 12 which will not be assessed.

Useful Material
Introduction to Python: https://docs.python.org/3/tutorial/introduction.html

1
Getting Started: This website contains a guide for installing and running Python and IDLE under Win-
dows: https://wiki.python.org/moin/BeginnersGuide/Download. You do not need to install Python on
the univeristy computers.

Online Shell: Python, being an interpreted language, has an interactive shell. This allows us to quickly and
easily test small snippets of code from within a Python environment without having to worry about complicated
files. We will sometimes use an online Python shell, found here: http://www.pythontutor.com/visualize.
html

Task 0
Setup
The workshops for this unit will run in bring your own device format. You will need to have Python 3 installed
on your laptop.

• For information on bringing your own device, see here: https://www.monash.edu/esolutions/students/


bring-your-own-device-byod
• To download and install Python 3, see here: https://www.python.org/downloads/

Experiment
A good method for learning about different Python commands is through experimenting in the interpreter (also
known as the shell).
• Use experimentation in the interpreter to determine what the following list of symbols do in Python: +,
-, *, /, **, //, and %.
• In your interpreter create a variable x that is equal to 3, and a variable y that is equal to 5.
• Using only the symbols (, ), +, -, *, /, **, //, and %, and the variables x and y, try to come up with as
many different ways as possible to evaluate to the numbers 0 to 10.

Modules
One of the great things about programming is that we are able to use other people’s code. Not only is this
allowed - it is encouraged! Clever and dedicated programmers have put time and thought into writing code to
do simple tasks as cleanly and efficiently as possible. In this unit you will often be asked to write your own code
to do many of these tasks - this is so you can develop an appreciation of what is going on behind the scenes,
and understand the limitations of some solutions. However, you should also understand how to go about using
code written by others.
One way we use other people’s code is by importing modules. We can import the math module like this:
>>> import math
>>> math.sqrt(4)
Try experimenting with the square root function1 we have now imported. Try using the square root function
without typing math before it. You should get an error! Restart the interpreter, and try typing math.sqrt(4).
Is the error the same?
If we know we only want to use the sqrt() function, we can import it like this:

>>> from math import sqrt


>>> sqrt(4)
If we know we want to import multiple definitions (e.g. functions; variables such as pi) at once, we can do this:
>>> from math import sqrt, ceil, floor, factorial, cos, sin, exp, log, pow, pi
>>> sqrt(4)
>>> exp(2)
There is another way of importing definitions you will occasionally see:
>>> from math import *

2
Import module Import definition Import all definitions (do not do)
>>> import math >>> from math import sqrt >>> from math import *
>>> math.sqrt(4) >>> sqrt(4) >>> sqrt(4)

Table 1: Importing

We do not import like this in this unit. In later units you will discuss the pros and cons of different methods
for importing from modules. For more information, see the Python Tutorial: Modules.
You should familiarlise yourself with the following definitions from math:

• sqrt • factorial • exp • pi


• ceil • cos • log • e
• floor • sin • pow • inf

Experiment using different values for a and b, experiment with the difference between a**b, pow(a, b), and
math.pow(a, b); and with the difference between math.e**a and math.exp(a). For more information on how
these functions work and why you may get different results, see the Python docs for math.

Q: How many programmers does it take to screw in a light bulb?

A:

3 5 3 12 6 0 ’ 9 10 4 10 11 25 -1 10 11 12 8 11 5 1 7 12 2
Translate the answer by evaluating all of the below expressions using the Python interpreter, and using int().
For the purpose of this exercise, x=5 and y=3.
A B D E H

elog(10) sin( 12 π) xy+1 5!
10 x + cos(3π)

I L M N O
3 log(343)
32
1000 de y e x cos(20) x sin(xy) d 8y
x e

P R S T W

27
x+x−y+1 4x − y 2x − 1 log( x5 ) bsin(x)c

Errors
When programming it is common to make mistakes. Often the computer can help us figure out where we went
wrong. Try running the lines of code given to you in Table 2 in the interpreter, and record what error message
is reported. Try to determine a corrected version of each code snippet. You made need to experiment, or do
some Googling.

Task 1: Maths
In the given Python file, replace the '' in each line with one of [+, -, *, //, %, /, **] to create a statement
that evaluates to True. For example, (a) can be changed to: 0 == (x + x) % x.
The used variables have the following assignments:
x = 3
y = 5
1 Functions will be discussed in later lectures. For now it is enough to know that sqrt() is an example of a function.

3
Code Error Message Solution
>>> A=7 NameError >>> a=7
>>> a+27 >>> a+27
>>> ‘7’+27 >>> int(‘7’)+27
>>> 1/0 No solution - rethink equation
>>> import maths >>> import math
>>> print7) >>> print(7)
>>> ‘I have ’ +1+ ‘ laptop.’
>>> name + ‘ dies in Endgame.’
>>> import Random
>>> ‘Cats are awesome
>>> x=3.0+7.0
>>> int(‘x’)

Table 2: Errors

a) 0 == (x ''x) '' x
b) 4 == x ''(y '' x) '' y

c) 7.5 == (x ''y) ''(y '' x)

Task 2: Booleans
In the given Python file, replace the '' in each line with one of [==, !=, <=, >=, <, >] to create a statement
that evaluates to True. For example, (a) can be changed to: 10 <= 10.
a) 10 '' 10
b) 10%4 '' 12//7
c) 3**2 '' 10-3

Task 3: Temperature Conversion


In the given Python file, replace the ‘’ with a numerical expression that converts the temperate in Fahrenheit
(temp f) to the temperature in Celsius (temp c). For instance, if Celsius were 3 degrees more than Fahrenheit
(incorrect!), the implementation would be: temp c = temp f + 3

NOTE: The conversion from F degrees Fahrenheit to C degrees Celsius is: C = (F − 32) × 5/9.

Task 4: Name Factoids


Before beginning this task, read section §3.1.2 Strings from The Python Tutorial (1083 words). If you get stuck,
the first thing you will be asked is whether you read this resource.
The following urls provide useful information on tools that will help you complete this task:
• + (string concatenation: §3.1.2 Strings)

• str.format() (string formatting method: §7.1.2. The String format() Method)


• len() (Python built-in function to get the length of an object: Built-in Functions)
• string name[] (string indexing: §3.1.2 Strings)
Optional: If you want to read all about strings, start here:
https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str

In the given Python file, replace the ‘’ with an expression that evaluates to a string with the following
pattern:

4
‘<name> has <number of letters>. It starts with <letter> and ends with <letter>.’
For example, if the variable name held the value ‘Jane’, factoids would evaluate to:
‘Jane has 4 letters. It starts with J and ends with e.’

NOTE: You must implement factoids so that it would work for any value assigned to name. You cannot
hard code a string.

Task 5: Coin Flip


Before beginning this task, read section §4.6 Defining Functions from The Python Tutorial, and read page
random from The Python Standard Library.
As you become an experienced programmer, you will learn that much of programming is reading - whether that
be formal documentation, other people’s code, or your own code. Being able to read efficiently and effectively
is an important skill.

The object of this task is to create a Python function, flip, that takes a float bias as an argument, and
that returns the sentence: ‘The flipped coin with bias <bias> has a value of heads: <boolean>’.
The value <bias> should reflect the float that was passed to the function in the function call, and the value
<boolean> should have a (bias*100)% chance of saying True.
For instance, if after implementing this functionality we were to run workshop01.py, we would then be able
to type in the resulting shell and observe the following behavior:
>> flip(0)
‘The flipped coin with bias 0 has a value of heads: False’
>> flip(1)
‘The flipped coin with bias 1 has a value of heads: True’
>> flip(0.5)
‘The flipped coin with bias 0.5 has a value of heads: False’

As we are dealing in probabilities, the final function call (flip(0.5)) could have gone either way. If we
repeatedly made the same function call, over time it should average out to saying True half the time, and
saying False half the time.
To complete this task, in the given Python file, replace the ‘’ with any import statements you feel are
necessary, and replace None with the string to be returned. You may wish to add additional lines of code before
the return statement; this is allowed, but not necessary.2

2 This task can be implemented in less than 134 characters. For a challenge, see how efficiently you can implement it.

You might also like