Lesson 4

This is Python Lesson 4

So far we have learnt how to print words and variables. Now we are moving on to loops. What is a loop? Well a loop is something that is repeated as many times as you want it to be. Learning loops after variables is good because it means we can combine the two. So a example of a simple loop might look like this:

Now the code should print out the numbers  1 to 10 but it misses out 10 because the last number is always ignored. So what does the code mean? Well the variable ‘x’ every time is assigned a new value each time the loop runs, so x will equal 1 then 2 and so on. After the word ‘for’ their must be a variable which in this case is ‘x’. After the word ‘in’ there must be something like a list which in this case is 1 , 10. Now you might have noticed the indentation, after a colon in Python, the second line changes position because the code hasn’t finished yet. The indentation and colon show what is in the loop and what is outside.  The numbers in the list are the numbers between 1 and 10 including the first number. Now you have learnt about a simple loop, experiment with different numbers and variables, and build your understanding.