01 Variables
In this module we will learn about variables and the basic types included with Python. We will write a script that will do some simple math and spend a bit of time looking at some functionality related to variables representing text.
It is a humble beginning but this is our first proper step towards hacking the planet.
Variables
A variable is a name that refers to some value.
x = 7
In the above line we are creating a variable called x and assigning it the value 7. Once we’ve
defined a variable, we can retrive its value using the handy name x instead of having to type
7. For example, we could use the built in print function that we used last time to look at the
value of x.
Let’s make a new Python script called variables.py:
# variables.py
x = 7
print(x)
$ uv run variables.py
7
Even if we’ve never written a line of code, we at least kind of know what variables are. In math we
often name our variables with a letter. Usually in code we don’t want to name our variables with
just a letter, rather its better to use some kind of meaningful word or words. We have a special
word for raw values, we call them literals and they are often on the right hand side of the =
in our assignment statements. To Python, after it sees the line x = 7, x and 7 are equavalent
but to us they obviously look different. x is a variable while 7 is a literal.
Before we move on its worth taking just a moment to discuss something that might be very obvious. Python reads our scripts line by line and executes each one in turn. This means that if we were to swap the two lines in our script by placing the
print(x)line before thex = 7line, it would fail. Without definingxbefore we try tox.
OK le’ts get back to variables. we’ve all seen a line expressed in math as $y = mx + b$, all of the letters in there are variables and they work in a very similar way to variables in Python. We know from geometry class that $m$ is the slope of a line, while $b$ is the intercept. In our script, we’ll use those as our variable names.
Let’s make some changes to variables.py to calculate our little line equation.
# variables.py
slope = 2
x = 7.8
intercept = 4.0
y = slope * x + intercept
print(y)
$ uv run variables.py
19.6
Let’s unpack this a little bit. In the first line, we are creating a variable named slope and storing
the value 2 in that variable. We can then use slope later in our program and it will be
equivalent to 2 until we change it. In Python, as in most languages, we use = to perform
assignment.
The second and third lines—x = 7.8 and intercept = 4.0—are functionally identical. We’re
storing 7.8 in x and 4.0 in intercept. Note that these numbers have decimal places but the
value we stored in slope does not. We’ll get to that in a second.
Where it starts to get interesting is in the final line y = slope * x + intercept. Here, we are
creating the variable y to store the value slope * x + intercept. We can use variables to
define another variable!
Python understands our mathematical operations (* and +) and reads the values of slope, x, and
intercept when assigning a value to y. We call the slope * x + intercept part an expression.
Strings
We can also create variables with types that are not numbers. In the hello_world.py script
we’ve already seen such a value. Let’s modify our hello world script to store a string (str in
Python-speak) in a variable before printing it.
# hello_world.py
my_string = "hello world"
print(my_string)
$ uv run hello_world.py
hello world
We define strings using quotation marks. We can either use the double quote " as illustrated
above or the single quote '. We could have used my_string = 'hello world' and it would have
worked just the same.
Python has a really handy way to modify strings using other variables—often other
strings—called f-strings. Let’s return to our hello-world.py script one more time to see what
f-strings (the f is for format) can do.
# hello-world.py
title = "comrade"
name = "DeepMay
my_f_string = f"hello {title} {name}"
print(my_f_string)
$ uv run hello-world.py
hello comrade DeepMay
Our little hello world script is doing something pretty fancy now! Lets focus on the line where we
define the my_f_string variable. An f-string is a way to insert other variables into a string in
a really straightforward way. We denote an f-string with a lowercase f before the leading quotes
and it let’s us insert values by wrapping them in {}. We’ll use f-strings a lot throughout these
modules.
Numeric Types
Let’s return to numbers for a second. Earlier in our script we had decimal points in some numbers
(x = 7.8 and intercept = 4.0) and none in another (slope = 2). Intuitively, we might think
that, say, 2 is the same as 2.0 and indeed Python mostly treats them as the same.
Strictly speaking, however, 2 is an integer or int while 2.0 is a floating-point number
or float. Occasionally, a mismatch between an int and a float might cause problems so its
worth being mindful of which type of number we’re dealing with. Luckily we can always check what a
variable’s type is with the type() function.
# variables.py
slope = 2
x = 7.8
intercept = 4.0
y = m * x + b
print(type(slope))
print(type(x))
print(type(intercept))
$ uv run variables.py
<class 'int'>
<class 'float'>
<class 'float'>
There’s one caveat we should point out with numbers and strings before we move on to our final
basic type. Sometimes we might want to represent a number as text rather than as a number per se.
Think about what might happen if we did something like my_var = "2". In this case despite being a
number, the value stored in my_var is actually a string. Let’s see what happens if we wrap
the value assigned to x in quotes in our script.
# variables.py
slope = 2
x = "7.8"
intercept = 4.0
y = slope * x + intercept
print(y)
$ uv run variables.py
Traceback (most recent call last):
File "<.../variables.py>", line 6, in <module>
y = slope * x + intercept
~~~~~~~~~~^~~~~~~~~~~
TypeError: can only concatenate str (not "float") to str
Luckily, Python caught it for us and we didn’t just end up with something nonsensical. Sometimes
these kinds of numeric/string mismatches can sneak in and cause confusion results. When in doubt,
use type.
Booleans
There’s one more basic type that we won’t discuss much just yet called a boolean or bool. A
boolean variable is either True or False—truly binary. Rarely will we want to define a
boolean variable ourselves we’ll use them implicitly a ton when we start talking about
conditionals in a couple of modules.
Summary
We started this module with the core building block or programming, the variable. We used variables to do some simple math and format a string. We also looked at different kinds of numbers and looked at how they interact with each other and with strings. Throughout all of our time at camp and beyond we will be using these things constantly, so its crucial that we build an understanding of them as early as possible.
Next time, we will build on all of this to make reusable units of code and move one step closer to programs that actually do something.