What does x = 1
mean to a computer? This is assignment, it tags the object on the right with the variable on the left. A variable is just a name for an object.
The function id()
gives the memory address of its argument, print()
prints its argument.
x = 1
print(id(x))
4297514912
Writing y=x
tags the object named x
with the variable y
. x
and y
are now different names for the same object, so the memory addresses are the same.
y = x
print(id(x))
print(id(y))
4297514944 4297514944
Returning to x=x+1
, we can make now sense of it. It calculates x+1
using the original object named x
, then names this new object x
. Importantly, the memory address of x
has changed.
x = 1
print(id(x))
x = x+1
print(id(x))
print(y)
4297514912 4297514944 2
A number can be stored as an integer (int
) or a decimal (float
). Python guesses the type of your variable, so writing x=1
will give you an integer tagged by x
, whereas x=1.0
gives a float. There's also a type for text: string
.
We can operate on variables using the following operations:
+
e.g. a = 5.0 + 1.0
results in a = 6.0
-
e.g. b = a - 1.0
results in b = 5.0
*
e.g. c = 5.0 \ 3.0
results in c = 15.0
/
e.g. d = 10.0 / 5.0
results in d = 2.0
**
e.g. e = 2.0 ** 3.0
results in e = 8.0
Other operations do exist, but for now these should be enough.
An example is shown below, where we calculate the fractional difference in the gravitational force on the Earth depending on whether the moon is on the same side as the sun or on the other side.
sun_mass = 1.981 * (10 ** 31)
moon_mass = 7.348 * (10 ** 22)
earth_mass = 5.972 * (10 ** 24)
G = 6.674 * (10 ** (-11))
r1 = 149597870700 # Sun earth distance
r2 = 384402000 # Earth moon distance
sun_force = (G * earth_mass * sun_mass)/(r1**2)
moon_force = (G * earth_mass * moon_mass)/(r2**2)
max_force = sun_force + moon_force
min_force = sun_force - moon_force
print(min_force/max_force)
0.9988770761490094
In addition to the ones above, we have the following more advanced operators:
%
This gives the remainder if you divide one by another
a = 6.7 % 2.2 results in a = 0.1+=
If we have a += b
, this is equivalent to a = a + b
a += 1 results in a = 1.1-=
If we have a -= b
, this is equivalent to a = a - b
a -= 1 results in a = 0.1\=
If we have a \= b
, this is equivalent to a = a \*b
a *= 10 results in a = 1.0/=
If we have a /= b
, this is equivalent to a = a / b
a /= 10 results in a = 0.1**=
If we have a = b, this is equivalent to `a = a b`
a **= 2 results in a = 0.01a = a % b
a %= 0.01 results in a = 0.0A simple example of some of these is shown below:
starting_balance = 1000
current_balance = starting_balance
current_balance -= 100 # Expensive night out
current_balance += 2000 # Student finance
print("current balance: £" + str(current_balance))
# We changed current balance into a string and added it to another string, and the total was printed out.
current balance: £2900
The way we are using Python currently, it is nothing more than a calculator with memory.
We can do much better. One way to do more advanced things is by harnessing the power of control statements, the simplest of which is the if/else if statement. An example is shown in a cheerful example below:
tripos_score = 31.0
if tripos_score >= 70:
tripos_class = 1.0
elif tripos_score >= 60:
tripos_class = 2.1
elif tripos_score >= 50:
tripos_class = 2.2
elif tripos_score >= 40:
tripos_class = 3.0
else:
tripos_class = None
if tripos_class:
print("You got a " + str(tripos_class))
else:
print(":(")
:(
In addition to int
, float
, and string
, python also has a type called bool
. A boolean can take the value True
or False
. In addition, any variable in python can also be None
, which means it doesn't hold a value.
A boolean expression is a statement which evaluates to either True
or False
. Examples are
a == b
True if a
is equal to b
a != b
True if a
is not equal to b
a > b
True if a
is greater than b
a < b
True if a
is less than b
a <= b
True if a
is less than or equal to b
a >= b
True if a
is greater than or equal to b
We can also create a compound statement consisting of more than one boolean expression using the following operators:
and
or
not
Which operate as you expect, e.g.
not is_poisonous and is_tasty
Would give True
if is_posinous
is False
and is_tasty
is True
.
An if
statement performs an action if the boolean expression in the if
statement is True
. elif
is short for else if, and the action is performed if the first if statement was false but the boolean expression in the elif statement is true. Finally, if all the ifs and the elifs are False, the action in the else is executed. else
and elif
statements are optional.
The basic structure is shown below:
if boolean expression:
statement_1
elif boolean expression:
statement_2
else:
statement_3
Indentation is used to show where the statements from one if statement ends and the next starts.