Getting started¶
Welcome!¶
This is an informal course for complete beginners. Our aim will be to build confidence in programming, using the Python programming language and turtle graphics. Itâs a training course, so thereâs no assessment, and no course credit!
I assume that participants have no programming experience at all. While I will talk through the materials, the course is focused on practical exercises. Questions are always welcome at any time
How to use this course¶
If you get stuck with the exercises, feel free to ask for help from a tutor. You can also help each otherâit would be great if you could introduce yourself to the people at your table and talk about the exercises! Of course, itâs ok to work on your own, if thatâs what you prefer.
You can also read ahead and try the next exercise if that helps. If youâve finished the exercises you can always move on to the next one.
Computer programming and failure¶
Computer programming involves lots of failureâoften you have to fail several times to succeed once, editing the code until it works! This is ok and happens to everyone, even very experienced programmers. As weâll see later, most of the time there will be an error message, which will give you a clue to the solution for the problem.
What youâll need¶
You can follow this course using (almost) any copy of Python and something that can edit Python code. That said, there is a Python environment called Thonny which is specially designed for beginners, and thatâs what I recommend for this course:
Once youâve installed this start it up. You should see something like this:
If that doesnât work for you, please first try logging on to Noteable . If that doesnât work, please try a text editor and Python install .
Throughout the rest of the course, where something differs between Thonny and Noteable, Iâll make this clear with colour-coded boxes. You only need to follow the instructions for the one youâre using!
The instructions for Noteable will sometimes be hiddenâif this is the case just click âShowâ to see them.
What is Python, exactly?¶
If youâre doing preparation for the class you can stop reading now! Weâll go through the rest in the class. (Feel free to keep reading if youâre interested, of course âș).
Python is a programming language. Youâve probably heard this term before, but what does it mean? It means that Python takes text that youâve written (called code), turns it into instructions for your computer, and follows those instructions. Weâll be learning how to write code to do cool and useful stuff. No longer will you be restricted to using other peopleâs programs to do things with your computerâyou can make your own!
Using Python¶
To start with weâll use Python as a command-line interpreter (you might also see this referred to as a REPL). This takes text commands and runs them as you enter them, which is very handy for trying things out.
In Thonny just type your commands into the panel titled âShellâ at the bottom of the window.
Click âShowâ to see how to do this in Noteable.
Go back to your Noteable home page, click New â Python 3. A new tab will appear looking like this:
Click on the box after In: [ ]
. Youâll be able to type your code in there. Unlike
in Thonny, you will have to press Shift-Enter to run each line of code.
Interacting With Python¶
You can now enter some code for python to run. Try:
print("Hello world")
Press Enter and see what happens. After showing the results, Python will bring you back to the interactive prompt, where you could enter another command:
>>> print("Hello world")
Hello world
>>> (1 + 4) * 2
10
An extremely useful command is help()
, which enters a help functionality
to explore all the stuff python lets you do, right from the interpreter.
Press q to close the help window and return to the Python prompt.
Exercise¶
Just above we demonstrated entering a command to figure out some math. Try some maths commands of your own! What operations does python know? Get it to add 239 and 588 together, and then square the result.
Solution¶
Here are some ways you might have got the answer:
>>> 239 + 588
827
>>> 827 * 827
683929
>>> (239 + 588) * (239 + 588)
683929
>>> (239 + 588) ** 2
683929
Running Python files¶
When you have a lot of python code to run, you will want to save it into a file, so for instance, you can modify small parts of it (fix a bug) and re-run the code without having to repeatedly re-type the rest. Instead of typing commands in one-by-one you can save your code to a file and run it all at once.
In Thonny: Type your code into the panel at the top of the window:
You can then use File â Save As âŠ. to decide where to save your file.
Click on the green circle with an arrow in it to run your program.
In Noteable: You can type multiple lines of code together (using Enter at the end of each line) then press Shift-Enter to run them all together.
Try this for a simple program with more than one line of code, maybe
print("Hello world")
print("something else")
And now we are all set and can get started with turtle!
Warning
When playing around with turtle in the following sections, avoid naming your file turtle.py
â rather use more appropriate names such as square.py
or
rectangle.py
. Otherwise, whenever you refer to turtle
, Python
will pick up your file instead of the standard Python turtle module.