Reading

Comics this time! A few that I really like. These are more general motivation type than some of the stuff so far but these are important lessons to take home.

Onto work!

Why loops are such a big deal

Now we’re really starting to get into programming.

Loops are one of the main reason we use computers, or even machines at all. Making a human do repeatitive and boring things is hard. Tell the person sitting next to you to multiply 24103512347189547013954802370542354* 4324902384238284289282394 and they’ll just refuse. Now tell them to do it ten times and they’ll think you’re crazy.

Next ask them to do it a million times. Then they’ll walk away. Okay maybe a contrived example but even the logistics of having humans do a million very large multiplications given an entire day is mind boggling.

Computers though. They’ll not complain. They’ll do it faster than you can blink.

for i in range(100000):
    result = 24103512347189547013954802370542354*4324902384238284289282394

Welcome to loops

Python (like pretty much every other language that matters) supports loops quite neatly, and they’re going to be pretty similar in most languages that you touch.

That was just one example. We’ll cover two of the major looping structures today and you’ll find this in most major languages (Java, C, Swift, Ruby, Javascript etc.)

They are

  • while
  • for

While

The simpler of the two, this structure will keep repeating a block of code while a given condition is true.

print("I will only stop when you say Thanks")
response = input()
while(response != "Thanks"):
    response = input("You did not say Thanks! Say it now:")

So the loop keeps going until the condition inside the brackets is false, that is when you enter Thanks it will exit. (So remember to thank the bus driver)

The general syntax

The general form will remind of if statements but now it’s repeating.

while(condition):
    stuff_you_want_to_repeat

Note the empty line.

An exercise

This is common design pattern in many programs I’ve seen out there. But write a program that will keep taking user input until I give an empty line. After which I want you to print DONE!

The output looks like

1
2
3
Fasd
wooords
I am
typing 
words

DONE!

This is similar to the first while example I showed, try it out and experiment.

For

Now a more complex loop structure is called the for loop. This is when you want to

  • do a task some pretermined n number of times

  • go over some list or similar

for i in range(2):
    print("Hip.")

print("Hooray!")

The general syntax

for <variable> in <something that can be looped over>:
    stuff_you_want_to_repeat

In proper programming terms the ‘something that can be looped over’ is called iterable. All this means is that if we can tell python hey here is how to loop through this then we can use the same for loop syntax for this. In time I will show you how to write your own.

With ranges

So ranges are a good way of doing things an n number of times. And the variable basically changes each time so you can make use of that. This is highly useful.

for i in range(1, 10):
    print(i)

This prints the numbers including the start number (1) and excluding the end (10).

1
2
3
4
5
6
7
8
9

(note: pretty much almost all the times in programming it will be inclusive of first and exclusive of the end - this will seem strange now but it makes code neater. You will realize this in time so just trust me on this for now.)

With strings

So this also works with strings, take a look.

for letter in "Harsh Deep":
    print(letter)

which gives

H
a
r
s
h

D
e
e
p

This is useful and you’ll find yourself doing this from time to time.

With arrays

Now I won’t go into a lot of detail on this yet (next lesson) but python has a way of storing lists in one variable like

list = [1, 2, 3, 4 5, 7, 10, 12, 25, 42]
for number in favorite_number:
    print(number)

which prints

1
2
3
4
5
7
10
12
25
42

An exercise

Write a program that gives the sum of the first n numbers.

  • 10 gives 55
  • 1 gives 1
  • 4 gives 10
  • 5 gives 15

Break

You can exit loops early using the break keyword. More on that later but if you see it around you’ll know what it means.

A warning

This concept is hard to get for many, and getting used to thinking with loops is perhaps one of the first key moments of learning to program so I will spend quite long making sure you learn how to think with these before moving on.

That means more practice and drills. Looping and arrays are essesstial concepts you need to know for any amount of practical programming.

What next: arrays, functions and fun!