# 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:
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:
The name “variable” means that its value can change over time:
Notice we can reference the variable’s previous value when assigning a new value:
x = x + 10
print(x)
x += 10 # FYI: += is a shorthand operator equivalent to this = this + that
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):
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:
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:
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:
We generally don’t want to do this, unless we are aliasing the function for some reason (which you may never need to do):
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.
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:
To make sure we see all desired outputs, it never hurts to print each value you would like to be displayed.
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):
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):
It is crucially important to consult the documentation for any function you are using, to understand what parameters it accepts.
We will return to use many of the other built-in functions provided by Python.
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 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:
Feel free to use comments librally in your coding. There is no penalty for using comments!