Python Language Overview

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!")

x = 2 + 2
print(x)
HELLO WORLD!
4

Let’s debrief each of these concepts one at a time.

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:

# this is 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:

print("HELLO") # this is still a comment
HELLO

In terms of practical usage of comments, in the classroom, we encourage students to use comments to cite or attribute sources of help:

# adapted this code from url: ___________
print("SOME CODE FROM ONLINE...")

# adapted this code from session with ChatGPT: ___________
print("SOME CODE FROM CHAT GPT...")

# student ________ helped me with this code:
print("SOME CODE FROM ANOTHER STUDENT...")
SOME CODE FROM ONLINE...
SOME CODE FROM CHAT GPT...
SOME CODE FROM ANOTHER STUDENT...

Sometimes we will use comment blocks to organize sections of our code:

#
# THIS IS A SECTION OF MY CODE
#

print("HELLO")

#
# THIS IS ANOTHER SECTION OF MY CODE
#

x = 2 + 2
print(x)
HELLO
4
Pro Tip

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:

  1. Click the cursor on the first line where we want to start the selection (where we would type our first comment character)
  2. Then press and hold “shift + option” on Mac, or “shift + alt” on Windows.
  3. Then click and drag straight down, and release the keys you were holding.
  4. Finally type a comment character (or whatever text you’d like), and notice it will be typed onto all the selected lines.

Feel free to use comments librally in your coding. There is no penalty for using comments!

Variables

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:

x = 5
print(x)
5
message = "HELLO WORLD!"
print(message)
HELLO WORLD!

The name “variable” means that its value can change over time:

x = 5
print(x)

x = 10
print(x)
5
10

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.

Variable Naming Conventions

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)

speed_mph = 90 # BEST
print(speed_mph)
90

Constants

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:

MY_CONSTANT = 3.14
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.

Built-in Functions

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.

The print Function

For 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

Functions as Variables

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 capabilities of the print function in a new function called my_print
my_print = print

# now the alias my_print has the same capabilities of the original print function:
my_print("HELLO WORLD")
HELLO WORLD

Printing vs Mentioning Last

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.

x = 10
x # we see this because it has been mentioned last
10
x = 10
print(x)

y = 20
y # we see this because it has been mentioned last
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:

x = 10
x # we don't see this unless we print it

y = 20
print(y)
20

To make sure we see all desired outputs, it never hurts to print each value you would like to be displayed.

x = 10
print(x)

y = 20
print(y)
10
20

Comma Separated Print Statements

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.

Function Parameters

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.

We will return to use many of the other built-in functions provided by Python.