Tuesday, August 23, 2016

Lather Rinse and Repeat

When we have to repeat something boring we hate it. Computers have no problem with that. They have no emotions. And man are they fast!

Let's start with Python range() function.

Eeeh, function?!

Consider this as a name for bunch of instructions that do something useful but we do not know (or care) how it does what it does. All we care about is how to use it and what it produces. More on functions in the future.

produces numbers form 0 to the last number you typed - 1.

list(range(start_number, end_number))
produces list of numbers from start_number to end_number - 1

Let's open our python interpreter in our terminal to see those in action 
pi@tron ~ $ python3
>>> numbers = list(range(1,10))
>>> print(numbers)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Wow! What just happened?!

We created a list of numbers (more on lists in the near future) and saved it under name numbers

Notice, that the range starts at 1, but ends with ... 9, not 10. Remember that.

Let's try to build another list this way:  
>>> butterflies = list(range(11))
>>> print(butterflies)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

See how it works? This time in function range() I did not specify the beginning number this time. It started automatically with 0 and ended with the number: 11 - 1. Eleven subsequent numbers: from 0 to 10. 

Let's try another one:

>>> even = list(range(2,11,2))
>>> print(even)
[2, 4, 6, 8, 10]

Now, Python created range of numbers that start at 2, end with 11 - 1, with increments of 2!

Why this range() function is important?

Let's make our computer repeat something.
How about creating a short program that produces square of the first 20 numbers.

Open Leafpad text editor and type in the program as shown below, and save it as loop.py

Watch out for the 4 spaces following the line that ends with semicolon (:). We talked about this in my previous post. What follows semicolon is a block of statements that must always be indented with the same number of spaces (4 recommended). Here is our short program:

print("*** Program Calculating Square ***")
print("***   of the first 20 numbers  ***")

for n in range(1, 21):
    print(n, "squared is", n * n)

And when run, here is what it does. Analyze this program and its result (don't worry if you find it difficult to understand as we are going to dissect it in our next chat).
pi@tron ~ $ python3 loop.py
*** Program Calculating Square ***
***   of the first 20 numbers  ***
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
6 squared is 36
7 squared is 49
8 squared is 64
9 squared is 81
10 squared is 100
11 squared is 121
12 squared is 144
13 squared is 169
14 squared is 196
15 squared is 225
16 squared is 256
17 squared is 289
18 squared is 324
19 squared is 361
20 squared is 400
pi@tron ~ $

Did you see how quickly it did the job? And your Raspberry PI is never going to complain that it is boring!

Isn't that sweet?

Problem Solving

Write a code that calculates cube of first 20 numbers (hint: n * n * n).

Write a code that prints your name 5 times.

Write a code that does the following:
  • Ask you your name and store it in variable name
  • Then it should ask your for the number and store it in variable number.  
  • Finally, it should use the for-loop to write your name on the screen the specified number of times. 
You can add something to this line besides your name if you like it. It should work like this program below:
pi@tron ~ $ python3 loop-name.py 
What is your name?
Enter a number (not to big please!):
Hello Neila I am Raspberry PI and I love working with you!
Hello Neila I am Raspberry PI and I love working with you!
Hello Neila I am Raspberry PI and I love working with you!
Hello Neila I am Raspberry PI and I love working with you!
pi@tron ~ $ 

In our next lesson, we will analyze the loop.py program step by step in order to fully understand how it works.