All the objects you learned about in the first part of this tutorial, such as integers, floating point numbers, strings, lists etc. can be declared as a variable and assigned a value.
For example you can assign the value 2 to the variable n
:
n=2
Nothing is displayed by the interpreter after this entry, so it is not clear anything happened. But in fact Python has stored the value 2 into a variable named n
. You can easily check this via just typing in n and pressing enter:
n
Now check for the type of n.
type(n)
Python has automatically realized that the value of 2 is an integer and therefore declared the variable n
to be of type int. Python is relatively smart in figuring out what you mean, so it automatically declares your variables with the correct type:
pi=3.141
type(pi)
word='Hello'
type(word)
many_numbers=[1, 6, 3, 0]
type(many_numbers)
All the above are examples for assignment statements. An assignment statement associates a variable name on the left of the equal sign with the value of an expression on the right of the equal sign. You can be as creative as you like when it comes to giving names to variables. You only need to keep in mind that you cannot separate by an empty space ("many numbers" would not work) and that variables are case-sensitive (so Pi is a different variable than pi).
Try to assign the same list above to the same variable name but separated by an empty space. Also try out what you get asking for the variable pi and for Pi (with the capital letter).
# Exercise 2.1.1
Once a variable is assigned a value, the variable can be used in place of that value:
width = 10
height = 5
area = width * height
area
A word of caution: The =
sign that is used for assigning values is actually a bit misleading. It is not to be confused with a mathematical =
sign. The =
sign that is used for assigning values would much better be represented by an arrow like this $\leftarrow$. It is not a symmetric operation.
Try what happens if you wanted to assign the value of 10 to the variable width the other way around, i.e. 10 = width
.
# Exercise 2.1.2
Also, once you already have assigned a value to a variable, you can assign a new value to it using the same variable:
width = width + 10
width
Clearly, you can see from this example that the =
sign for value assignment does not at all have the properties of a mathematical equal sign.
Note that there is a shorter version of writing the above that often comes in handy, e.g when you want to increase integer values in loops (section 4).
width += 10
width
Does this shortcut also work with minus?
# Exercise 2.1.3
width
In computer programs it is often necessary to compare numbers. Let's check the following statements:
1 < 2
1 > 2
Apparently Python understands the <
and >
signs to compare numbers. It correctly identifies the statement 1 < 2
to be True
and the statement 1 > 2
to be False
.
So Python assigns a value to each of these statements, namely True
or False
. These values are a special Python type called a "Boolean" (bool
). Booleans are named after George Boole, who wrote a book about logic in the 19th century. True
and False
are the only two values a Boolean can have. Booleans are used to keep track of whether something is true or not.
In the first example above the<
operator takes two integers and returns a Boolean value. In general, there are 6 relational operators for comparisons:
>
(greater than)<
(less than)>=
(greater than or equal to)<=
(less than or equal to)!=
(not equal to)==
(equal to)Note that a common mistake is to use the simple =
sign when you want to check whether two numbers are equal to each other. But remember that the =
in Python programming is used to assign a value to a variable. So if you just write x = y
, it means x
is assigned the value of y
. If you want to check if x
is equal to y
, you need to write x == y
.
Let x=2
and y=3
. Try to predict the Boolean value of the following statements (and check what Python says):
x * y == 6
x + y < 5
x + y != 5
x == y
x = y
# Exercise 2.2
For numbers you can do arithmetics with the operators +
, -
, *
, etc. Similarly, you can do arithmetics with Booleans, too! So what operators do Booleans have? (That was what George Boole's book was about...)
and
operator¶Predict the Boolean value of the following statements (and check what Python says) :
1 < 2 and 2 < 3
1 < 2 and 2 > 3
# Exercise 2.3.1
or
operator¶Predict the Boolean value of the following statements (and check what Python says) :
1 < 2 or 2 < 3
1 < 2 or 2 > 3
# Exercise 2.3.2
not
operator¶Predict the Boolean value of the following statements (and check what Python says) :
not 2 < 3
not 2 > 3
# Exercise 2.3.3
Just as you can combine numbers with +
, -
, *
, etc. to form complex expressions, you can do the same with Booleans and the above and
, or
and not
operators. To mark precedence, just as for numbers, you can use parantheses.
In this section, we have covered:
+=
shortcut for incrementing variables>
, <
, >=
, <=
, ==
and !=
and
, or
and not