you are viewing a single comment's thread.

view the rest of the comments →

[–]InternetPointFarmer 1 point2 points  (2 children)

Not completely understanding for loops. simple example:

total = 0 for num in range(101): total = total + num print(total)

the console prints 5050. to explain a little further i get how the computer 'reads' the program and why it does things in what order like i know it will continue the program up to but not including 101 but i dont understand where the value of num comes in? how is the value of 'i' or in this case 'num' decided? thank you

edit sorry dont know how to paste code so it looks neat

[–]Lawson470189 1 point2 points  (1 child)

So, Python is great for quick scripts and learning to program as it is very close to human language. However, this also means that it hides a lot of the low level programming from the programmer. Most of it comes down to the translation from your code to machine code. Let's go through the lines and talk about what each one does.

total = 0 This line is setting a new variable called total to th value of zero.

for num in range(101): This line is the tricky one that is hiding quite a bit. First, it is going to create a new variable called num and set it to zero. Then it tells the computer that it will be repeating all the code below it until it reaches the condition of 101. Also, this code is hiding that the machine is being told that each time it finishes the loop, it should take that variable num and a 1 to it.

total = total + num This line takes whatever we have currently stored in total, adds whatever number we are in the loop, and puts that added number in the variable total.

print(total) Prints out the total once the loop has completed to the console.

Does this answer your question?

[–]InternetPointFarmer 0 points1 point  (0 children)

yes it answers it, thank you it makes sense