Python tutorial 1: The basics
Coding is a way of communicating with a computer to get it to do things for us. Computers need to be told what to do in a very clear and logical way and we have special languages to talk to them. Python is one of these languages. Python is the language we have chosen to teach at codetoday because of its accessibility.
Just like human languages, programming languages have words, grammar and punctuation.
Have a look at the first few lines of the code shown below, which when run, draws a simple house:
The first line is asking the program to access the library where Python keeps all of its commands and bring in a set of commands known as `turtle`. This is a graphics package which gives us several command that allow us to draw anything we wish.
But first we must create something to draw with:
is the rather strange command that creates the graphical sprite, known as a Turtle, that allows us to draw on the screen (imagine a turtle moving and leaving a trail behind it). Notice the lowercase and uppercase letters and the punctuation marks (including the empty brackets); these details are very important in programming.
Rather than just writing turtle.Turtle() however, line 3 of the program is:
fred = turtle.Turtle()
This means we have given the Turtle a name, making it a unique object in the program. This is very important as without a name we are not able to call the Turtle or ask it to do things for us.
Now have a look at the code up to line 17. Note that any line that starts with an # (whose official name is octothorpe) is ignored by the computer program. This is a line written for us humans and is used to explain what sections of code are doing. You can see that this line is written in normal English and not in Python.
The section of code up to line 17 contains four different types of commands. All of the lines start with fred, followed by a dot; this means we are asking fred to do something for us. These commands ask fred to change its colour (although note that the command color is written in the American spelling in Python, with a missing 'u'), change the thickness of the drawing pen, move forward and turn left.
Run the program (by pressing the play button above the code) and see whether you can identify which part of the drawing these lines refer to.
You can now have a look at the rest of the code from line 19 onwards and you will notice the same commands being used, as well as a couple of new ones. These lines, as the comment (the line starting with #) says, draws the door of the house.
Can you finish off this house by drawing two windows each a different colour, and perhaps a chimney too?
At codetoday we teach coding in Python. We are running a series of online coding camps, with the next course starting April 6th 2020.. For more information and to book click below.