TaskSheet1 - Computing 1
TaskSheet1 - Computing 1
TaskSheet1 - Computing 1
Task Sheet 1
Over the course of this module you will be given 7 tasksheets to complete. Each task will help to
develop your programming ability with Python and counts towards your continuous assessment.
No prior knowledge of programming or Python is assumed. Please see the demonstrators after
you have completed each of the weekly tasks so that they can record its completion.
NOTE:
1. Do not wait until the end of the module to do this!
2. You may have to look back through all of your lecture notes in order to complete a task.
The intention is not to have each tasksheet only covering material in a single lecture!
Programs usually incorporate a wide range of syntax/methods!
3. If you are already experienced with Python this course will be very easy for you!
However, you still need to complete the tasks to gain marks for the continuous
assessment element of the course. You should be able to do this very quickly. For your
interest I have therefore included some more challenging tasks that you may want to try
“for fun” (see the end of this document).
TODAY:
Getting Started
You will be using the Windows machines in one of the following Computer Clusters for this
course:
Demonstrators will be available in each cluster – they can offer advice on programming issues,
but you will only learn if you attempt to first solve problems yourself.
Scenario 1: You are hosting a dinner party and have invited some friends. One of them doesn’t
know where you live, and would like directions from the University to your house.
Scenario 2: Your dinner party was a great success. One of your friends enjoyed the food so much
that he/she has asked you for a recipe for one of the dinner party courses.
Pick one of these scenarios and write out the instructions that you would need to give to your
friend. Note: Do this as quickly as you can. Accuracy is not important here, so feel free to make up
details if necessary.
NOTE: Before diving into writing code, it is always useful to first take a step back and consider
how you will break up the task into a series of smaller steps/instructions. You will usually find
that important instructions are missed out the first time through this process. Consider also
whether some instructions are overly complicated and whether the task would be simplified if
these were further divided into more specific instructions. Many programmers plan their code
and the sub-tasks that are needed using pencil and paper before they type anything this is
know as writing PSEUDOCODE and can dramatically speed up the process of writing code. This is
because the act of writing it often highlights issues/problems which can then be designed
around.
Click on Spyder to launch. This opens Spyder – the window which opens should look something
like that above. It’s default view consists of 3 “panes” – a Text Editor on the left-hand side (LHS)
for creating Python scripts (programs), an “Object Inspector” in the top right which gives
information about objects in the Editor or Console windows, and a python “Console” in the
bottom right.
Program commands can be typed either into the text editor (which allows you to “save” the
commands in a file for later use), or directly into the console window where each line of code is
executed immediately. Previously saved files can also be loaded into Spyder (and are displayed in
the text editor).
We will first run some code in the console window. The console window contains what is known
as a python interpreter. This is where we can try out programming statements and ideas before
they are incorporated into an actual program.
1. The console window should contain some sort of a prompt. It could be something like
“In [1]:” or “>>>”. First type: print “Hello World” then hit the RETURN key.
You should see “Hello World” appears in the window on the line below.
2. Notice how the text you entered is coloured. This is syntax highlighting and is designed
to help with the writing and reading of code.
3. Experiment with the following Python statements and expressions. In each case, >>>
simply denotes the Python prompt, and should not be typed.
While in the console window, try pressing the up arrow key a few times. Notice how this recalls
previous lines of Python code. You can use the left and right arrow keys to move backwards and
forwards through a recalled line and can delete text or insert new text in the usual way.
Whilst the Python interpreter is suitable for trying out small pieces of code, it cannot be used to
create a long-lasting program – largely because there is no way of saving lines of code to disk and
later recalling them. To create a proper program, we need to put the Python code in a text file,
creating using a text editor of some sort. Note that word processors such as Microsoft Word
cannot be used for this, as they do not produce simple text files!
1. In the Spyder text editor window enter the line: print ‘Hello World!’
2. Save your program as a file called hello_world.py in a suitable directory (create your own
e.g. MyDocuments\PythonExercises – we will refer to this as your PythonExercises
directory from now on).
3. In a File Explorer window navigate to your PythonExercises directory and look at the files
therein. You should see your newly created hello_world.py. Now try running your code.
Make sure that the Spyder window containing your source code has focus and then
choose Run -> Run, or simply press the F5 key.
4. When you click Run in Spyder for the first time a dialog box may appear (see figure
below). It might say that a new Python interpreter needs to be opened. If so, go to the
Spyder menu and choose Consoles->Open an IPython Console. Now click Run again. In
the Console pane a new console window will be opened “hello.py”, which will display the
result of your script/program.
You’ve now seen how the interactive Python interpreter and text editor in the Spyder IDE can be
used separately to help you develop and run Python programs.
An alternative introduction to Spyder can be found on the Computing 1 VLE area – under
Learning Resources -> Getting Started with Python on the Cluster Machines.
Beneath this there is guidance on Getting Python for your own computer.
Exercise 3: Distance Conversion
Create a new empty text window (in the LHS pane of Spyder, not the python interpreter!). Go to
File -> New File to do this. Type the following into it:
miles = 26
yards = 385
print('A marathon is ' + str(miles) + ' miles and ' + str(yards) + ' yards')
print('or in metric ' + str(km) + ' km')
Save the source code as a file called marathon.py in your PythonExercises directory. Run the
program (Run -> Run or click on the green arrow in the Spyder toolbar) and check that the
following output is seen:
Experiment with the program. Try the following modifications and observe what happens:
1. Replace the floating point constant 1760.0 with the integer constant 1760
2. Remove the str() cast operation around the miles variable in the print statement.
3. Remove the miles = 26 line.
Record what happens in these cases and try to explain why. Ask the demonstrator for
confirmation if you are not certain that you understand what is happening.
1. apple
2. 99balloon
3. zza$pg
4. global
5. Global
Which of the following are valid integer constants, which are valid floating point constants, and
which are invalid?
1. 23
2. 23.0
3. 1,000
4. 2e-2
5. e3
Exercise 5: Temperature Conversion
In a file called f2c.py in your PythonExercises directory, write a program that converts a
temperature given in Fahrenheit into its Celsius equivalent. The relevant formula is
5(T F −32)
T C=
9
Give the program an initial value near the top of the source file (e.g. a line fahrenheit = 70).
Once you have this program working, write another in a file named c2f.py that performs the
opposite conversion; that is from Celsius to Fahrenheit.
If you wish to use the degrees symbol in output generated by your program, you might be able to use
the Unicode escape sequence \u00b0 to produce it (this depends on whether the version of Python
you are using is a Unicode build). If so, the code print "28" + u"\u00b0" + “C”
will produce ‘28oC’ as output.
Summary
These exercises have shown you how to write basic programs that make use of values “hard-
wired” into the source file.
Consider the tutorial question from week 1 of semester 1: “A train driver spots an obstacle on a
section of straight track ahead whilst travelling at 100 km/hr. After applying the brakes for 55
seconds the train stops just in front of the obstacle. What was the train’s acceleration during the
period of braking and how far from the obstacle was the train when the brakes were first
applied?”. Write a code to solve this question. Plot a graph of the velocity of the train and the
distance that it has moved as a function of time. Make any further improvements to your code
that you see fit (e.g. prompt the user to enter an initial velocity and a duration of braking etc.).