In this lecture, we'll introduce basic numerical operations in Python. We'll discuss:
Because Python is an interactive language, it's a very practical substitute for a calculator. Let's try a few simple computations.
The simplest approach is to type in the formula for a computation you would like to do. Here are a few examples using integers:
27+6
33
3*(2+3)
15
36/12 # (division converts integers into floating point numbers, i.e. decimals)
3.0
35/12
2.9166666666666665
37 % 12 # remainder after division
1
2**3 # python uses x**y to denote "x to the power y"
8
Most of the same operations work with floating point numbers. However, surprises are possible.
27.3 + 5.4
32.7
3.2**2.3
14.515932837559118
37.4 % 12.2
0.8000000000000007
Wait. Uh, what?
Recall that %
refers to the remainder after division. In this case, 12.2 "goes into" 37.4 three times. Mathematically, the remainder is 37.4 - 3*(12.2) = 37.4 - 36.6 = 0.8
.
Just like in C++, Python's implementation of floating point numbers (i.e. decimals) has finite precision. That means that most numbers cannot be represented exactly. the result above reflects this fact: Python cannot represent 0.8
exactly, so it uses a close approximation in floating point representation.
As you might expect, operations like taking the square root of -1
yield complex numbers.
(-1)**(0.5)
(6.123233995736766e-17+1j)
When writing code, it's almost always best to give names to important numbers. You can then use those names in subsequent computations. These names are called variables.
x = 27.3
y = 5.4
x + y
# same as writing 27.3 + 5.4
32.7
This code block creates variables x
and y
with values 27.3 and 5.4 respectively, and then prints their sum.
You can also assign names to the outputs of computations:
z = x + y
z
32.7
When are two variables "the same"? It's easy to check the id
of a variable in Python -- this indicates the location in memory to which the name of the variable points. We can also use the is
binary operator to check whether two variables point to the same memory location. The is
operator should not be confused with ==
. The ==
operator checks whether two variables have the same value, regardless of whether they refer to the same object.
To clarify the difference, consider the following scenarios:
your_stack
and my_stack
are both variables with value 4. Thus, your_stack == my_stack
(they have the same value). Because each variable refers to the same stack of cookies, it also holds that your_stack is my_stack
.your_stack
and my_stack
are again variables of value 4, and your_stack == my_stack
. However, these two variables now refer to different stacks of cookies, and so it is not the that case your_stack is my_stack
.Variables whose values are immutable (cannot be changed) often share the same memory. Integers, floats, and strings are examples of immutable types in Python.
x = 4
y = 4
x == y
True
id(x), id(y)
(4504444688, 4504444688)
x is y
True
On the other hand, mutable variables like lists will generally not share memory (we haven't formally introduced lists yet -- all you need to know for now is that they are data structures can be altered).
a = [1 , 2]
b = [1 , 2]
c = a
id(a), id(b)
(4537059912, 4538546184)
a is b, a == b
(False, True)
id(c)
4537059912
a is c
True