top of page

Python tutorial 3: Storing things inside our program



If you missed part 1 and 2 you can catch up via these links:




In our tutorials, that teach children to code, we have looked at how we get started with writing a computer program and we introduced one of the most important tools in coding: repeating code using a loop.


In this tutorial we will look in a bit more detail at how things are stored in a computer program. We have already come across this when we created a Turtle and gave it a name:


fred = turtle.Turtle()


The code after the equals sign tells the program to create a Turtle. The equals sign asks the program to store this Turtle using the name fred. Think of this as a box in which we store our Turtle (the coding turtle not a real one!), and on the outside of the box be stick a label with fred written on it. Whenever we type fred, the computer program will know to look inside the box and find the Turtle in there, ready to draw something.


Storing things in our program so that we can use them later is a very important aspect of coding. Let's see some more examples of this.


In the last tutorial we asked you to draw a shape with 15 sides using a loop. Here is one way of doing this:




On line 11 we have used range(15) in the 'for' loop since we want to repeat 15 times. However we also had to use the same number 15 on line 13 to work out the angle we want to use to turn the Turtle (we are dividing the whole circle in 15 parts. Note: the / symbol is the division sign in coding).


Each time we want to change the number of sides of our shape, we need to change the number in two places. This is not too bad, but what if we were using that same number in 20 different places. That is a lot of work and we risk forgetting to change one of them.


The solution is to create a "box" and store the number of sides we wish to have in that box. For example we can write:


number_of_sides = 15


We cannot use spaces when giving a name to things, so we put the underscore ( _ ) symbol instead. We also prefer to always use small case letters, even for the first letter. Now we can use the name number_of_sides whenever we want to use the number 15:



Look at lines 3, 12 and 14 in the code above. This seems like more work than before, but now, if we want to change how many sides we wish our shape to have, all we have to do is change the number once on line 3. Try out a few other number of sides.


When we create a "box" to store something in it using the equals sign, we call this a variable. We have two variables in the code above, one called fred that stores the Turtle, and one called number_of_sides that stores a whole number.


Can you create another variable ("box") to store how long each side of the shape is?


If you are asking yourself: "What is the point of these variables?" then don't worry, you are in good company as many students do ask this question at this stage. As the code you write becomes longer and more complicated, you will start to appreciate variables even more.


To finish off, here is another task for you: Create another Turtle in the same program (you cannot use the same name for the Turtle - let us call the second Turtle mary). Make mary draw a shape of a different colour and with a different number of sides to that which fred drew.


If you enjoy our tutorials why not take your coding to the next level and join us on a course.



23 views

Commentaires


Python Coding for Young People

CT-Unlimited-FinalRAW-transparent.png

Codetoday Unlimited is for the curious teenager or preteen keen to learn proper Python coding. Stephen's courses start from the basics and carry on to intermediate and advanced levels.

Python Coding for Adults

The Python Coding Place is Stephen's platform full of courses and other resources for beginners and intermediate learners. The focus is on clarity and Stephen's unique communication style.

bottom of page