• Stephen Gruppetta

Python tutorial 2: Don't Repeat Yourself

Updated: Oct 25, 2019


Image Little London Magazine


If you missed part 1 catch up via the link below:

Python tutorial 1


In our first tutorial we introduced you to the basics of programming using Python: how we can use commands that exist in the programming language as building blocks so that, step by step, we achieve what we want our program to do.


Let us go back to a simplified version of the code from the last tutorial. Press the play button to show our Turtle called fred draw a square:



You didn't have to type this code in yourself since it's already there, but what do you think you would have found a bit annoying if you had to type it in yourself?


You've probably guessed it: you have to repeat the same two lines over and over again. In this case we are repeating the forward and left commands fours times each to make a square. But 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.


One of the very important principles in coding is DRY. This stands for Don't Repeat Yourself. 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.




Line 11 above is the line that 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 line 11. 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 lines 12 and 13. 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. Lines 15 and 16 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:


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.




If you enjoyed our tutorial why not find out more about our coding courses, just following the link below.






Try a Free Coding Lesson

© Codetoday Limited

CodeToday Limited is a company registered in England (company number 9789836). Registered office: 13 Hawley Crescent, London, NW1 8NP, United Kingdom