Variables#
In the first chapter we just have seen how we could execute a simple code like a = 5. What happens there is the definition of a variable, a symbol containing a value. This is the same principle as when we define a variable in mathematics: it’s a representation of a number that can take different values and that we can re-use across equations. Just like in mathematics we can define variables of different nature. For example we can define that a variable is an integer, or a matrix or even a function.
As you have seen above, Python has a very natural way of defining variables, just like in mathematics:
a = 3
Note that this is different from many other languages where often variables have to be defined before giving them a value.
If a variable has been defined and we just add it to a cell end execute, we get:
a
3
We see that now we get an output with the value 3. Most variables display an output when they are not involved in an operation. For example the line a = 3 didn’t have an output.
Now we can define other variables in a new cell. Note that we can put as many lines of commands as we want in a single cell. Each command just need to be on a new line.
b = 5
c = 2
As variables are defined for the entire notebook we can combine information that comes from multiple cells. Here we do some basic mathematics:
a + b
8
Here we only see the output. We can’t re-use that ouput for further calculations as we didn’t define a new variable to contain it. Here we do it:
d = a + b
d
8
d is now a new variable. It is purely numerical and not a mathematical formula as the above cell could make you believe. For example if we change the value of a:
a = 100
and check the value of d:
d
8
it has not changed. We would have to rerun the operation and assign it again to d for it to update:
d = a + b
d
105
Type inference#
When we write a = 5 we don’t explictly say what type of variable we want. By type we mean: do we want an integer number, a floating type number etc. This work is done by Python, which infers what we want. We can use the type function to get the type of a variable:
type(a)
int
Let’s try to define a variable of a different type, for example a floating point number:
e = 2.5
type(e)
float
We see that now, as we have used a variable with a commma (floating point), the type of the variable is not more int but float. When we combine different variables, Python assumes we want to keep the most accurate result. So if we combine an integer and a float we get a float:
f = a + e
type(f)
float
Simple operations#
We can apply all simple mathematical operations to these variables, like addition, subtraction, multiplication and division:
a + b
a - b
a * b
a / b
20.0
In addition, we can also raise a number to a power using the ** operator:
a ** 3
1000000
String variables#
After numbers, another very common type of variable is a string. A string is a sequence of characters, which can be letters, numbers or symbols. In Python we can define a string by using single quotes or double quotes:
my_text = 'This is my text'
my_text
'This is my text'
type(my_text)
str
Strings can be be combined with each other by using the + symbol:
my_text + ' and this is more text'
'This is my text and this is more text'
We can also combine strings with numbers, but we need to convert the number to a string first:
a
100
my_text + str(a)
'This is my text100'
f-strings#
Combining text and variables as done above can often becomes complicated depending on the type of numbers. f-strigns offere us a very simple way of combininig text and variables together. We prepend our string with f and then we can enclose any variable into curly parenthesis {}:
f'the value of a is {a}'
'the value of a is 100'
Booleans#
The last type of variable introduced here is the boolean, a variable indicating just a binary values Yes/No, very useful for example to define conditions. In this case its value can be either True or False. We can define it directly:
mybool = True
mybool2 = False
or it can can be the value returned when doing a comparison, for example:
a > 3
True
We can save this output into a new boolan variable:
myresult = a > 3
myresult
True
Exercises#
Create a variable
xwith the value 10, and another variableywith the value 20.5. Create a new variablezthat is the sum ofxandy.Print the value of
zand its type.Create a variable
namewith your name as a string and a variableagewith your age. Print a message that says “Hello your-name>, you are your-age years old!” using an f-string.Create a boolean variable by checking if you are older than 25.