Data—numbers, variables, and functions
- Be able to apply basic operations to numbers.
- Understand the role of variables and how to use them appropriately to represent data.
- Be able to understand and use functions.
We will start our investigation of Python programming by thinking about a very simple programming environment that you are all likely to be familiar with—a calculator. If you think about the basic functionality of a calculator, it allows you to enter representations of numbers, perform operations on them, and display the output.
We can approach Python in the same way. For example, if we wanted to add two numbers together and display the result, we could write the following Python code:
print 2 + 2
If we then save the file containing this code and run it, we can see that it produces the following output, as expected:
This example shows that Python is aware of a concept of a number and has the potential to apply mathematical operators to them (addition, in this case). Let’s have a look at a few more operations:
# multiplication print 2 * 3 # division print 2 / 2 # exponentiation print 2 ** 4 # subtraction print 2 - 4
6 1 16 -2
You can see in the above several lines that begin with
Starting a line with the
# character tells Python that the rest of the line is a comment, and it will not attempt to interpret it.
We can put whatever we like in comments, and they are a very useful way of explaining our code using natural language.
In the above, we represented the numbers as integers. We can also represent them as decimals, such as:
print 2.0 ** 3.0
The difference between representing a number as an integer and as a decimal can be potentially important in some cases. Unless what you’re representing can only be an integer, it is best to use a decimal representation.
Programming becomes much more powerful once we introduce variables. By using variables, we can store and refer to data during the operation of our program.
For example, say we have 3 apples and 2 oranges. We can write something like:
apples = 3
Here, we’ve created a variable called
We’ve given it an informative name, then the equals sign (to indicate assignment), and then the value that it will refer to (the integer 3, in this case).
We can call our variables close to whatever we want; they can’t start with a number, contain any spaces, or come from a set of words that are special to Python, but in general we are free to choose our names as we please.
Now we can proceed and create another variable for our oranges:
apples = 3 oranges = 2
Now that we have specified our two variables, we can use them to refer to their values. For example:
apples = 3 oranges = 2 print apples + oranges
By using functions, we can expand the range of operations that we can apply to our data.
Before introducing functions, we will first consider another concept in Python programming—how we can make additional functionality available to our scripts.
The Python language has a lot of built-in functionality available, but an immense amount of functionality is optional and we need to tell Python that we want to make it available.
To do this, we use the
For example, a functionality that is required surprisingly often in programming is the capacity to generate random numbers. Luckily, Python has such functionality—but it is an optional extra so we first need to tell Python that we want to use it:
Now we have the functionality of the
random package available to us.
To generate a random number, we’ll use a function called
import random rand_val = random.random() print rand_val
What functions are available in a package? In Spyder, you can type the package name followed by a dot (e.g.
random.) and then press the
TAB key to get a list of the package functionality, which includes any available functions.
Functions can take values called arguments, which affect how the function operates.
Such arguments are specified by including them in parentheses; in this example, we do not need to provide any arguments so we simply open and close our parentheses.
random returns a random number between 0 and 1, which we assign to the variable
rand_val, which we then print to the screen.
How do you know what arguments a function is expecting? The best way is to write the function name in Spyder and then press
CTRL-i while the cursor is nearby to the function name. The help window will then show the information about the function, including its arguments. You can also look at its help website.
Let’s look at another example of a function, this time where we do want to specify some arguments. Say if we want to generate a random number from a Gaussian (Normal) distribution, rather than from a uniform distribution between 0 and 1. As we know, a Gaussian distribution requires two pieces of information—its mean and its standard deviation. So if we want to generate a random number from a Gaussian distribution with a mean of 0 and a standard deviation of 1, we can write:
import random gauss_val = random.gauss(0, 1) print gauss_val
Instead of simply specifying the value of a particular argument, we can also explicitly tell the function what the value of a particular argument is. This is often good practice, as it makes the function arguments very clear. For example:
import random gauss_val = random.gauss(mu=0, sigma=1) print gauss_val
We can also use variables to specify the function arguments:
import random gauss_mean = 0.0 gauss_stdev = 1.0 gauss_val = random.gauss(mu=gauss_mean, sigma=gauss_stdev) print gauss_val
As well as using the functions that are available to us as part of the Python language, and that we have used the
import statement to bring in from external packages, we can create our own functions.
This is a very powerful strategy for programming, as it allows us to re-use algorithms without having to duplicate their instructions.
To begin with a contrived example, imagine that we want to be able to add together the contents of two variables. As we have seen before, we can do something like:
apples = 3 oranges = 2 print apples + oranges
We can recreate this behaviour using our own custom function. This function will receive two numbers and give us back their sum.
In Python, we indicate that we want to define our own custom function by beginning with the special word
This is then followed by the name of the function, which is just like a variable name in that it can be pretty much whatever we want.
Then, we specify what we want the inputs to be called, inside parentheses, followed by a colon.
For our example, this might look something like:
def sum_fruit(fruit_a, fruit_b):
So we started with the special word
def, then indicated that we want our function to be called
We then specify that it will receive two inputs, which we will assign to the variable names
Now, we need to write the instructions that are part of the function. We do this by indenting our lines of Python code such that they are ‘under’ the function definition.
def sum_fruit(fruit_a, fruit_b): total_fruit = fruit_a + fruit_b
‘Indenting’ means to include some blank characters at the start of a line.
We can do this by pressing the space bar 4 times, or we can use the
TAB key—we have set up spyder so that pressing
TAB automatically inserts 4 space characters.
Indenting is a very important concept in Python, as it is often the sole way in which the structure of code is indicated.
We will return to the concept of indentation in future lessons.
For the function to give back its result, we can use the special Python word
return followed by the data that we wish to send back.
For the purposes of this example, we need to return the sum of the two input variables:
def sum_fruit(fruit_a, fruit_b): total_fruit = fruit_a + fruit_b return total_fruit
Because we have now defined the function, we can use it:
def sum_fruit(fruit_a, fruit_b): total_fruit = fruit_a + fruit_b return total_fruit apples = 3 oranges = 2 print sum_fruit(fruit_a=apples, fruit_b=oranges)