# this is a comment
print("HELLO WORLD!")
= 2 + 2
x print(x)
HELLO WORLD!
4
Let’s take a moment to familiarize ourselves with some basic Python language concepts, including comments, variables, and built-in functions.
Each of these concepts are exemplified in the cell below:
# this is a comment
print("HELLO WORLD!")
= 2 + 2
x print(x)
HELLO WORLD!
4
Let’s debrief each of these concepts one at a time.
Let’s move on to discussing variables.
We use a variable to store a value for later. And anytime we reference that variable, we access whatever value it holds at the time we reference it.
When storing variables, we use the equals sign (=
) as the assignment operator. Whatever is on the right of the equals sign is stored in the variable on the left:
= 5
x print(x)
5
= "HELLO WORLD!"
message print(message)
HELLO WORLD!
The name “variable” means that its value can change over time:
= 5
x print(x)
= 10
x print(x)
5
10
Notice we can reference the variable’s previous value when assigning a new value:
= x + 10
x print(x)
# FYI: `this += that` is shorthand for `this = this + that`
+= 10
x print(x)
20
30
Remember, whatever is on the right side of the equals sign will be evaluated first, then stored in the variable on the left.
In spreadsheet software, we may be constrained to referenceing cells using variables like “A2” or “C3”.
However in Python we get to choose our own variable names.
Let’s prefer to use contextual variable names, which describe that the value is about, so when we read the code in English, we can understand what concept the variable is referencing.
Notice, for variables with multiple words, we use snake case, (which is essentially all lowercase with underscores):
# s = 90 # OK
# print(s)
# speed = 90 # BETTER
# print(speed)
# speed mph = 90 # NOT VALID (CAN'T HAVE SPACES)
= 90 # BEST
speed_mph print(speed_mph)
90
Sometimes you may see us use all capital letters for variables that are considered as “constants”. A constant is a type of variable whose value we don’t expect will change. For example:
= 3.14
MY_CONSTANT print(MY_CONSTANT)
3.14
In some languages, we will be prevented from updating the value of a constant, but in Python constants are just a naming convention (they don’t actually prevent the value from being updated).
Often in practice we may use a constant for configuration variables and notebook secrets.
In spreadsheet software, we have access to a number of built-in functions like “SUM” or “VLOOKUP”.
In Python, there are a number of built-in functions we can use as well.
print
FunctionFor example, the print
function we have been using so far is one such built-in function.
To understand how to use any built-in function, we must consult the function’s documentation.
The print
function displays some values into the notebook’s output. Whatever value we pass in will be displayed on its own line:
print("HELLO WORLD")
print(4)
print(4 + 4)
HELLO WORLD
4
8
Notice, when we are invoking or calling a function, we must use trailing parenthesis. Otherwise we are referencing the function as a variable, and not ever calling it:
print # DON'T DO THIS
<function print(*args, sep=' ', end='\n', file=None, flush=False)>
We generally don’t want to do this, unless we are aliasing the function for some reason (which you may never need to do):
# storing the print function in a new variable called `p`:
= print
p
# now the alias `p` has the same capabilities:
"HELLO WORLD") p(
HELLO WORLD
In a Python notebook, we can omit printing if we simply just mention a variable as the very last line executed at the bottom of a cell.
= 10
x # we see this because it has been mentioned last x
10
= 10
x print(x)
= 20
y # we see this because it has been mentioned last y
10
20
Notice, the value has to be the last value in a cell. If you forget to print a value that appears earlier in the cell, it won’t be printed:
= 10
x # we don't see this unless we print it
x
= 20
y print(y)
20
To make sure we see all desired outputs, it never hurts to print each value you would like to be displayed.
= 10
x print(x)
= 20
y print(y)
10
20
Normally a function has a specific number and order of parameters that can be passed to it.
But the print
function is a little special in the sense that we can pass any number of values to it, separated by commas. When we use comma separated print statements, we see all the values displayed on the same line.
Printing multiple items on the same line (each item is separated by a comma):
print(100, "hello", True)
100 hello True
print("$", 3.99)
$ 3.99
You’ll notice this introduces a space character into the output in between each printed value.
The print
function specifically has an optional parameter called sep
which we can use to customize the separator (for example, removing the space character):
print(100, "hello", True, sep=" | ")
100 | hello | True
print("$", 3.99, sep="")
$3.99
It is crucially important to consult the documentation for any function you are using, to understand what parameters it accepts.
input
FunctionThe input
function prompts the user to provide an input:
= input("What's your favorite ice cream flavor? ")
fav_flavor print(fav_flavor)
vanilla bean
We will return to use many of the other built-in functions provided by Python.
4.1 Comments
When programming, sometimes we want to accompany our code with comments, written in normal English.
We use comments to explain the code, and provide context.
In Python, we use the pound sign character (
#
) to introduce a comment:Observe that whatever text appears to the right of the comment is not executed. But any code to the left is still executed:
In terms of practical usage of comments, in the classroom, we encourage students to use comments to cite or attribute sources of help:
Sometimes we will use comment blocks to organize sections of our code:
As a beginner, feel free to use comments liberally. There is no penalty for using comments!
4.1.1 Vertical Column Selection
As a time saving tip for commenting out many lines of code at the same time, we can use a keyboard shortcut to perform a vertical column selection.
To perform a vertical column selection: