Is Python for kids difficult? This is a how-long-is-a-piece-of-string type of question. Python is a vast and very powerful language. Several aspects of it, when one reaches the intermediate and advanced stages, are difficult, yes.
But that doesn't mean getting started with Python should be. The trick is to start with engaging projects and to learn things in a good order, using engaging project. So if you've never coded using Python before, why not have a go with this gentle tutorial.
First question: where do I write and run my Python code? Before wasting time and effort trying to install Python on your computer, just head to this page to use a web-based coding platform: codetoday.co.uk/code
Type your code in the blank area, and press the Run (Play) button whenever you want to run your code.
Just like human languages, coding 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, we can (and should) write:
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.
Some of the things we can ask fred our Turtle to do is to move, change colour, change the thickness of the line it draws, turn left and right and more.
Have a look at the next few lines of now:
# draw the main outline of the house (red) fred.color('red') fred.pensize(5) fred.forward(100) fred.left(90) fred.forward(100) fred.left(90) fred.forward(100) fred.left(90) fred.forward(100) fred.left(90)
The first line starts with # (whose official name is octothorpe). This means that the computer program will ignore this line. 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 next lines are all Python lines though. Look at the patterns. What do you notice about all of these lines?
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 putting it in the coding platform and pressing the play button above the code) and see whether you can identify which part of the drawing these lines refer to.
Let's look at the next block of code now. Notice how the comments (the lines that start with #) give us a clue to what this bit of code does:
# draw the door (orange) fred.forward(40) fred.color('orange') fred.begin_fill() fred.forward(20) fred.left(90) fred.forward(40) fred.left(90) fred.forward(20) fred.left(90) fred.forward(40) fred.end_fill()
Now coding is not about reading code written by someone else and copying it. Coding is about exploring, experimenting, and making mistakes. Yes, mistakes are the most important part of learning coding (and most things in life!)
Here is the full code so far:
import turtle fred = turtle.Turtle() # draw the main outline of the house (red) fred.color('red') fred.pensize(5) fred.forward(100) fred.left(90) fred.forward(100) fred.left(90) fred.forward(100) fred.left(90) fred.forward(100) fred.left(90) # draw the door (orange) fred.forward(40) fred.color('orange') fred.begin_fill() fred.forward(20) fred.left(90) fred.forward(40) fred.left(90) fred.forward(20) fred.left(90) fred.forward(40) fred.end_fill()
Can you finish off this house by drawing two windows each a different colour, and perhaps a chimney too?
Let's add a new tool
Just go back to drawing a simple square:
import turtle fred = turtle.Turtle() # draw the main outline of the house (red) fred.color('turqouise') fred.pensize(5) # draw a square fred.forward(100) fred.left(90) fred.forward(100) fred.left(90) fred.forward(100) fred.left(90) fred.forward(100) fred.left(90)
You may have copied and pasted this code, or maybe you typed it in. Either way this is not good coding. We are repeating ourselves and one of the most important rules in coding is DRY. DRY stands for Don't Repeat Yourself.
What if instead of a square we were drawing a shape with 20 sides, or 500? That's a lot of repetition and we don't really want to be copying and pasting chunks of code.
Also, what if you want to make the square larger? How would you do it? Again you would have to change all the forward(100) to forward(200), say. That's a lot of repetition.
Instead of repeating ourselves, we want the computer program to do the repetition for us. Computers don't mind repeating things over and over again as they are very fast and they don't get bored!
Here's how we can change the code above to avoid repetition. (As the pros would say, we are making the code more DRY). Run the program below to see how it draws the same square as the code above (with an extra dot at the end). Compare the two programs and see whether you can spot the difference.
import turtle fred = turtle.Turtle() # choose the colour and the thickness of the line fred.color('turquoise') fred.pensize(5) # draw a sqaure for repeat in range(4): fred.forward(100) fred.left(90) fred.color('purple') fred.dot(20)
There is a very strange line in this code:
for repeat in range(4):
This line asks the computer to repeat something over and over again. The word `repeat` is not important here. Try changing it to any other word you wish and you will see that the code will still work.
The important keywords are the other words in that line, especially the word `for` at the beginning. In fact we call this a `for loop`; a loop is something that goes on and on.
`range(4)` is how we tell the program how many times to repeat something. You will also notice a colon at the end of this line. In English a list of things usually follows a colon. Here, we are telling the program that the list of commands we want it to repeat is coming next.
Finally, you will notice something else that is different about the two lines that come after the for repeat in range(4): line. Have a good look. The space at the beginning of the line is called an indent, and it's not there to make the code look pretty. This is an important part of the grammar (or syntax) in Python. The lines that have an indent are the ones that the program will repeat four times. Without this indent, the program has no way of knowing how many lines need repeating.
The last two lines are not repeated because they don't have an indent so they only happen once the `for loop` finishes repeating the lines with an indent.
Now, it's your turn to code a bit more:
1. Can you place a dot in each of the four corners of the square, not just the last one? [Hint #1: all lines with an indent in a `for loop` need to be together in one block and cannot be separated by lines that do not have an indent. Hint #2: Use the tab key to make an indent, it's easier than using the space bar]
2. Can you make sure that the square is turquoise but the dots are purple?
3. Now can you change the square into a pentagon? And now into a 15-sided shape.
You have now learnt about one of the most important tools in programming: the loop. A loop allows you to repeat lines of code as many times as you need.
Now you are ready to make your own drawing…