Variables¶

Variables are like boxes: you can store anything in them and then put a label on it. The label doesn't need to match the contents, but it's really helpful if it does.

We store a value into a variable with the = (single equals) operator. This creates a statement that has an effect (to store a value in the "box" with the specified label).

In [2]:
x = 2

Once a variable exists, we can use it as an expression (thing that evaluates to a value):

In [3]:
x
Out[3]:
2

But the variable must exist first! Referencing a label for a box that hasn't been created yet generates an error:

In [4]:
y
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 y

NameError: name 'y' is not defined

Variables will always hold the same value until they are re-assigned. Moreover, the expression on the right-hand side of the = is fully evaluated at assignment time.

In [5]:
x = 2 + 2  # stores 4, i.e. the result of the `2 + 2` expression

We can do a number of things in the meantime, including using x...

In [6]:
y = x
z = 65
n = z + 5.4 / x
m = 11.2 * x

... but x will remain unchanged.

In [27]:
x
Out[27]:
22
In [8]:
x * 2 + 32 / 9
Out[8]:
11.555555555555555
In [10]:
22 = x
22 = 4
  Cell In[10], line 1
    22 = x
    ^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
In [12]:
x = 22
x
Out[12]:
22
In [13]:
pi = 3.14
In [14]:
avogadro  = 6 
avogadro
Out[14]:
6
In [28]:
y = 25
x_and_y = x + y
x_and_y
Out[28]:
47
In [19]:
x123 = 123
x123
Out[19]:
123
In [25]:
x/y
Out[25]:
0.88

Go to this code in PythonTutor. What's the output?

https://dub.sh/DacUWD3

In [ ]:
 

Aside: print¶

In the Python Interpreter (or the last line of code in a notebook cell), the value of expressions is automatically shown. Often, we need to be more explicit about when values are shown in our terminal or on screen. This is where print comes in.

In [ ]:
print("Hello World!")
In [ ]:
"Hello World!"

This is especially helpful when seeing intermediate values in a short program:

In [29]:
x = 2 + 4
print(x)
y = 3 + x
print(y)
x = y
print(x)
6
9
9
In [31]:
x = 2 + 4
print(x)
6
In [33]:
print("Hello")
Hello
In [34]:
x = print(42) 
42
In [40]:
print(x)
None

Boolean Expressions¶

So if we have variables that can have arbitrary values, then we might at some point need a way to determine a variable's stored value without changing it. This is where boolean expressions come in.

In [41]:
x = 2
x < 5
Out[41]:
True
In [42]:
x > 5
Out[42]:
False
In [43]:
22 >= 22
Out[43]:
True
In [45]:
not 22 == 22
Out[45]:
False
In [48]:
not x < 5 or x == 2
Out[48]:
True
In [50]:
False or False
Out[50]:
False
In [52]:
False and True
Out[52]:
False
In [53]:
# What's printed out here?
temp = 72
is_liquid = temp > 32 and temp < 212
print(is_liquid)
temp = 300
print(is_liquid)
True
True

Strings¶

Strings are a type of value that can hold arbitrary letters, symbols, numbers, etc. They are denoted by paired single (') or double (") quotes.

In [ ]:
name = "Alessia"
name
In [ ]:
thing_one = ""
thing_two = 0
thing_three = print("Hello")
In [ ]:
thing_one == thing_two == thing_three