Almost all programs will have some requirement to process a bunch of items or do something repeatedly. This is where loops come in to picture. In Python, we have for loops and while loops. In this post we will look at both of them.

For loop

Basic syntax A for loop in Python looks like

users = ["a", "b", "c"]
for user in users:
	# do something with user
	# here we only print
	# notice the indentation
	print("this will be printed once per user")
# outside of for loop
print("outside of loop. this will be printed only once")

for keyword is used to indicate that we want to “write” a for-loop. A for-loop is used when you want to process a each item in a collection. Here, users is some kind of collection which is mostly represented as a list in Python. We also need to give some name to the current item that is being processed- in above example, we’ve named the current item as user. Code that will process one single user should be indented as shown above. This tells Python that any code that is indented after for .. in .. : line should be executed for all items in collection. Any code that you want to do after processing all items in the collection should be “dedented”.

Let’s say you have a list of numbers like: [1, 2, 10, 8] and you want to calculate mean or average. When we do it manually, we could write it as 1 + 2 + 10 + 8 and then:

  1. add 1 + 2 which produces 3 and we keep that result in our mind
  2. we now take the next number, which is 10, and then add the previous result which is 3 with 10 giving us 3+10=13
  3. now take the next number, which is 8, and then add the previous result which is 13, giving us 13+8=21
  4. since there are no numbers remaining, we say the mean is 21 / 4 i.e. total_sum/n, where n is the number of items in the list

Notice that steps 2 and 3 have exactly the same pattern: add previous result with the current one. But step 1 is a bit different so let’s rewrite step 1 as

  1. imagine previous total is 0, so take the next number, which is 1, and then add previous result which is 0 with 1 giving us 0+1=1
  2. now take the next number, which is 2, and then add the previous result, which is 1, with 2 giving us 1+2=3

We basically rephrased the computation steps so that they all have same pattern. This makes transferring the logic to code straightforward. Let’s implement this is Python.

numbers = [1, 2, 10, 8]
total = 0
for number in numbers:
	# notice the indentation
	total = total + number
average = total / len(numbers)
print("Average is ", average)

The code will print Average is 5.25. In line 1, we’ve defined the same numbers in a list and assigned it to a variable called numbers. In line 2, we create a variable called total with value 0. This line directly corresponds to our “imaginary total” explained above. We will use this variable to hold the sum of all numbers. But in the beginning its value is 0 since we haven’t added any numbers yet.

Now comes the for loop. For every number in numbers, we add that number with the current value of total. The adding part total = total + number is indented, meaning that this line will run for all numbers. After all numbers have been added, in line 6, we calculate the average and then print it. Let’s look at what happens to total step by step.

step number total
1 1 0+1 = 1
2 2 1+2 = 3
3 10 3+10 = 13
4 8 13+8 = 21