It should be clear now that we can print the

It should be clear now that we can print the squares by printing “n*n”. In fact we can make the output more helpful by printing phrases like “The square of 3 is 9”. The following code shows this change to the print instruction repeated inside the loop. Note how the variables are not inside quotes and are therefore evaluated.

for n in range(10):

print("The square of", n, "is", n*n)

pass

print("done")

The result is shown as follows.

This is already quite powerful! We can get the computer to potentially do a lot of work very quickly with just a very short set of instructions. We could easily make the number of loop iterations much larger by using range(50) or even range(1000) if we wanted. Try it!

Comments

Before we discover more wild and wonderful Python commands, have a look at the following simple code.

# the following prints out the cube of 2

print(2**3)

The first line begins with a hash symbol #. Python ignores any lines beginning with a hash. Rather than being useless, we can use such lines to place helpful comments into the code to make it clearer for other readers, or even ourselves if we came back to the code at a later time.

Trust me, you’ll be thankful you commented your code, especially the more complex or less obvious bits of code. The number of times I’ve tried to decode my own code, asking myself “what was I thinking …”

Functions

We spent a lot of time earlier in part 1 of the guide working with mathematical functions. We thought of these as machines which take input, do some work, and pop out the result. And those functions stood in their own right, and could be used again and again.

Many computer languages, Python included, make it easy to create reusable computer instructions. Like mathematical functions, these reusable snippets of code stand on their own if you define them sufficiently well, and allow you to write shorter more elegant code. Why shorter code? Because invoking a function by its name many times is better than writing out all the function code many times.

And what do we mean by “sufficiently well defined”? It means being clear about what kinds of input a function expects, and what kind of output it produces. Some functions will only take numbers as input, so you can’t supply them with a word made up of letters.

Again, the best way to understand this simple idea of a function is to see a simple one and play with it. Enter the following code and run it.

# function that takes 2 numbers as input

# and outputs their average

def avg(x,y):

print("first input is", x)

print("second input is", y)

a = (x + y) / 2.0

print("average is", a)

return a

Let’s talk about what we’ve done here. The first two lines starting with # are ignored by Python but for us can be used as comments for future readers. The next bit “def avg( x,y )” tells Python we are about define a new reusable function. That’s the “def” keyword. The “avg” bit is the name we’ve given it. It could have been called “banana” or “pluto” but it makes sense to use names that remind us what the function actually does. The bits in brackets ( x,y ) tells Python that this function takes two inputs, to be called x and y inside the forthcoming definition of the function. Some computer languages make you say what kind of objects these are, but Python doesn’t do this, it just complains politely later when you try to abuse a variable, like trying to use a word as if it was a number, or other such insanity.

Now that we’ve signalled to Python that we’re about to define a function, we need to actually tell it what the function is to do. This definition of the function is indented, as shown in the code above. Some languages use lots of brackets to make it clear which instructions belong to which parts of a program, but the Python designers felt that lots of brackets weren’t easy on the eye, and that indentation made understanding the structure of a program instantly visual and easier. Opinions are divided because people get caught out by such indentation, but I love it! It’s one of the best human-friendly ideas to come out of the sometimes geeky world of computer programming!