Crash Course: Python I

Setup, environments, and basics

Brendan Chapuis

March 8, 2027

Course Home Page

Python Installs

Installing Python

Python comes with many operating systems, but to get the latest version…

You want to always have the latest version, but this leads to many installations of Python on the same computer.

Each of these installations are referred to as “interpreters”.

Choosing a Python Interpreter

VS Code allows you to choose a Python Interpreter when editing a “.py” file.

Python2 vs Python3

Python2 is an older version of Python, no longer supported (since Jan. 1, 2020).

  • Current version is Python3

Some systems still have Python2 installed as the default “python” command.

  • You can check which version you have with python --version or python3 --version
  • Always use python3 to run Python code from the terminal, to avoid accidentally using Python2

Running Python in VS Code

You can run a whole file of Python code by clicking the play arrow.

Or you can run a single line or selection…

  • hitting Shift+Enter
  • right clicking the line, then selecting “Run Python”

Both of these will execute the Python code in a terminal.

Hello World Example

x = 'Hello Class!'
print(x)

a = 0.1
b = 0.1
c = 0.1

print(a + b + c == 0.3)
Hello Class!
False

Python Environments

Default Python Global Environment

graph TD
    subgraph ProjectC["Global Python Environment"]
    GP["🐍 System Python<br/>Installation"]
    PA["📁 Project A"]
    PB["📁 Project B"]
    PC["📁 Project C"]
    end
    
    PA -.- GP
    PB -.- GP
    PC -.- GP
    
    style GP fill:#ff9999
    style PA fill:#e1f5ff
    style PB fill:#e1f5ff
    style PC fill:#e1f5ff

Environment Visualization

graph TD

    subgraph ProjectB["Global Python Environment"]
    GP["🐍 System Python<br/>Installation"]
    PB["📁 Project B"]
    end
    
    subgraph ProjectA[".venv"]
        PA["📁 Project A"]
    end

    
    subgraph ProjectC["my-venv"]
        PC["📁 Project C"]
    end
    
    PA -.- GP
    PB -.- GP
    PC -.- GP
    
    style GP fill:#ff9999
    style PA fill:#e1f5ff
    style PB fill:#e1f5ff
    style PC fill:#e1f5ff

Environments

There are two main options for setting up Python environments:

  1. venv

Comes with the latest Python installations, creates a folder with symlinks to the packages.

  1. conda

Part of the Anaconda/miniconda world.

Can be used both as a package manager and for environments.

Aside on Conda

Conda manages both Python installations, packages, and environments from outside Python.

Venv manages Python environments from within Python.

Anaconda is a distribution of (1) a Python installation, (2) Conda environments, (3) a bunch of default packages.

Miniconda is a distribution of (1) a Python installation and (2) Conda environments.

Use Python Environments

You should be using environments for any language, but especially for Python.

  • You will have multiple versions of Python installed at once
  • Python package managers historically handled dependencies poorly
    • Updating one package would break another package
  • The latest versions have started giving warning messages if you try to install packages system-wide

Creating an Environment

For venv the command to create a virtual environment is…

terminal
python3 -m venv /path/to/new/virtual/environment

And for conda

terminal
conda create --name <my-env>

Creating an Environment in VS Code

Luckily VS Code’s Python Extension makes handling these environments easy.

  • Open the Command Palette Shift+Cmd+P

  • Search for “Python: Create Environment…”

  • Select either “venv” or “conda”

  • Select the Python interpreter (version) to use

Now whenever you launch a terminal for this workspace, it will use the environment you created.

Live Coding Example

  • Create a new VS Code workspace
  • Create a Python environment with venv
  • Install the numpy package in that environment

Python Packages

Python Packages

Packages are how you can import functions.

You will sometimes see “Modules”. A package could have one or many modules within it.

Packages can be installed using

  • pip built in to Python
  • conda

Pip Install Packages

pip stands for “pip installs packages”.

  • recursion joke!

It installs Python packages hosted on the Python Package Index (PyPI).

terminal
python3 -m pip install numpy

python3 -m pip install numpy is executed in a terminal, not in Python code itself, unlike R.

Conda Install Packages

Installs packages hosted on the Anaconda repository.

Can also install other software, like R.

terminal
conda install numpy

You can also use pip install to install packages in a conda environment, but this can cause conflicts, so you should use conda install by default in this situation.

Importing a Package

Once a package is installed (to your environment), you can…

  1. Import the package and shorten the name
import numpy as np

numpy is a package for numerical computation (ex. better arrays and linear algebra).

If you want to use the function arange() from numpy, you would write

np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Importing a Package, Other options

  1. Import the package without shortening the name
import numpy
numpy.arange(10)
  1. Directly import one function of the package
from numpy import arange
arange(10)
  1. Directly import all functions of the package
from numpy import *
arange(10)

The first option is what is recommended and used most often.

Python Basics

Variable Assignment

Python uses a single = for assignment

x = 42

Just like R, functions can be assigned to new variables:

a = np.arange
a(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Basic Math

Math is not that different:

3 + 3
6
3 - 3
0
3 * 3
9
3 / 3
1.0
3 ** 3 # Power
27
3 % 3  # Modulo
0

Logic

Logic operators are written out instead of symbols.

1 > 2
False
1 < 2
True
1 > 2 and 1 < 2
False
1 > 2 or 1 < 2
True
not 1 > 2
True

Python Data Types

  • Booleans
  • Numbers: Integer, Floating
  • Strings
  • Collections:
    • List, Tuple, Dictionary, Set

You can check the type of any object with type()

type(42)
int
type("hello")
str
type([1, 2, 3])
list

Lists

Lists are constructed as comma-separated elements in square brackets

my_list = [1, 5, 2, 8]
print(my_list)
[1, 5, 2, 8]

Lists don’t enforce a single type.

my_list = [1, 'hi', 2, False]
print(my_list)
[1, 'hi', 2, False]

Tuples

Tuples are constructed as comma-separated elements in parentheses.

my_tuple = (1, 'hi', 2, False)
print(my_tuple)
(1, 'hi', 2, False)

Tuples Are Immutable

Tuples are immutable; lists are mutable.

  • i.e. you cannot change values of tuples once created, or add additional elements
my_list[2] = 4
print(my_list)
[1, 'hi', 4, False]
my_tuple[2] = 4
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[24], line 1
----> 1 my_tuple[2] = 4

TypeError: 'tuple' object does not support item assignment

Sets

Sets are constructed as elements in curly braces.

my_set = {"RI", "MA", "VT"}
my_set
{'MA', 'RI', 'VT'}

Sets are only unique values.

my_set  = {"RI", "RI", "MA", "VT", "MA"}
my_set
{'MA', 'RI', 'VT'}

Dictionaries

Dictionaries are constructed as key:value pairs in curly braces.

my_dict = {"RI":"Rhode Island", "MA":"Massachusetts", "VT":"Vermont"}
print(my_dict)
{'RI': 'Rhode Island', 'MA': 'Massachusetts', 'VT': 'Vermont'}

Dictionaries can be subsetted by their keys.

my_dict["RI"]
'Rhode Island'

Python Indexing

Python starts indexing from 0.

x = ['a', 'b', 'c']
x[0]
'a'

For some people, this is the mark of a true programming language.

One of the most common errors when switching between R and Python.

Slicing

Python can slice sequences using start:stop (stop is exclusive).

x = ['a', 'b', 'c', 'd', 'e']
x[0:2]
['a', 'b']

Leave out start or stop to go from the beginning or to the end.

x[:3]
['a', 'b', 'c']
x[2:]
['c', 'd', 'e']

Negative indices count from the end.

x[-2:]
['d', 'e']

For Loops

Python is space sensitive.

For example, a for loop requires the looped lines to be offset by at least one space (customary to use a tab—4 spaces).

for i in np.arange(3):
    print(i)
0
1
2

Leaving out the space will throw an error.

for i in np.arange(3):
print(i)
  Cell In[35], line 2
    print(i)
    ^
IndentationError: expected an indented block after 'for' statement on line 1

Ending For Loops

Python does not have an “end” statement to end a for loop.

Instead, the for loop ends when the spacing ends.

for i in np.arange(3):
    print(i)
print("Done!")
0
1
2
Done!

If Statements

Python uses if, elif, and else — with spacing, just like loops.

x = 15

if x > 10:
    print("Big")
elif x > 5:
    print("Medium")
else:
    print("Small")
Big

Compare to R’s if (...) { } else if (...) { }.

Python uses elif (shortened) and spacing instead of braces.

Objects

Python is an object-oriented programming language.

Example: a list is an object.

Objects have

  1. Properties
  2. Methods

Classes define objects; objects are the actual instance of the class.

Methods

Methods are a key feature of Python.

Methods are functions attached to an object that operate on the object.

For instance, we said a list is an object. Lists have the method: sort().

a_list = [2, 1, 9, 4, 6]
a_list.sort()
print(a_list)
[1, 2, 4, 6, 9]

Methods are Functions

Remember: methods are a type of function.

You can think of methods as functions that always take as an input the object they are defined for.

They may take other inputs as well.

Every object has their own methods. The sort() method is not defined for tuples.

a_tuple = (4, 1, 3, 2)
a_tuple.sort()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[39], line 2
      1 a_tuple = (4, 1, 3, 2)
----> 2 a_tuple.sort()

AttributeError: 'tuple' object has no attribute 'sort'

Append Method

If you wanted to append an element to a list…

a = [2, 4, 1]
a.append(3)
a
[2, 4, 1, 3]

If you appended a whole list…

a = [2, 4, 1]
a.append([3, 5, 6])
a
[2, 4, 1, [3, 5, 6]]

What Methods Does an Object Have?

You can print all the methods of an object with the dir() function.

  • dunder methods: have double underscores, like __init__(), are special methods that are used by other Python functions.
dir(x)
['__abs__',
 '__add__',
 '__and__',
 '__bool__',
 '__ceil__',
 '__class__',
 '__delattr__',
 '__dir__',
 '__divmod__',
 '__doc__',
 '__eq__',
 '__float__',
 '__floor__',
 '__floordiv__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getnewargs__',
 '__getstate__',
 '__gt__',
 '__hash__',
 '__index__',
 '__init__',
 '__init_subclass__',
 '__int__',
 '__invert__',
 '__le__',
 '__lshift__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__neg__',
 '__new__',
 '__or__',
 '__pos__',
 '__pow__',
 '__radd__',
 '__rand__',
 '__rdivmod__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rfloordiv__',
 '__rlshift__',
 '__rmod__',
 '__rmul__',
 '__ror__',
 '__round__',
 '__rpow__',
 '__rrshift__',
 '__rshift__',
 '__rsub__',
 '__rtruediv__',
 '__rxor__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 '__truediv__',
 '__trunc__',
 '__xor__',
 'as_integer_ratio',
 'bit_count',
 'bit_length',
 'conjugate',
 'denominator',
 'from_bytes',
 'imag',
 'is_integer',
 'numerator',
 'real',
 'to_bytes']

Not Everything is a Method

Some functions that you would expect to be methods are not.

Example, len() returns the length of an object.

x = [1, 2, 3]
x.len()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[43], line 2
      1 x = [1, 2, 3]
----> 2 x.len()

AttributeError: 'list' object has no attribute 'len'

But len() is not a method of a list, even though you might expect it.

x = [1, 2, 3]
len(x)
3

Define Your Own Functions

Python functions are defined with the keyword def and spacing:

def my_function():
    print("Hello!")

my_function()
Hello!

You can define arguments and return values.

def my_function(a, b):
    c = a ** 2 + b ** 2
    return c

my_function(3, 4)
25

F-Strings

Python’s f-strings let you insert variables directly into strings.

Prefix the string with f and use {} for expressions.

name = "World"
print(f"Hello, {name}!")
Hello, World!

You can put any expression inside the braces.

a, b = 3, 4
print(f"{a}^2 + {b}^2 = {a**2 + b**2}")
3^2 + 4^2 = 25

Compare to R’s paste0("Hello ", name).

Define Your Own Classes (Objects)

You can define your own classes with the class keyword.

class MyClass:
    def __init__(self, a):
        self.a = a
    def my_method(self):
        print("This is a method of MyClass")

my_object = MyClass(5)
print(my_object.a)
my_object.my_method()
5
This is a method of MyClass

Next Class

Next class: pandas

Today: setup, environments, and the core language.

Next class (Python II): pandas DataFrames, reading and writing CSVs, and how they compare to data.table and the tidyverse.

Other Python packages you will run into:

  • numpy arrays and numerical analysis
  • matplotlib, seaborn, plotly plotting
  • scikit-learn, PyTorch machine learning

Class Activity

Class Activity (~ 5-10 minutes)

Starting from this list:

temps = [45, 32, 28, 55, 40, 19, 60]

Tasks:

  1. Use .append() to add 38 to the list
  2. Use .sort() to sort the list
  3. Use slicing to get the first three temps
  4. Write a for loop that prints "Cold!" for temps below 30 and "Warm!" otherwise

Class Activity, Part 2

  1. Make a dictionary mapping "RI", "MA", "VT" to their lowest temperature in temps (any values are fine)
  2. Loop over the dictionary with .items() and print each pair
  3. Define a function to_celsius(f) that returns (f - 32) * 5 / 9
  4. Use an f-string to print "45F is 7.2C" using your function (hint: round(x, 1))

Jupyter

What is Jupyter?

Jupyter Notebooks mix code, output, and Markdown text in one document.

  • Name comes from Julia, Python, and R; today it supports many languages
  • We will use it with Python
  • Edit notebooks in the browser, or in VS Code (what we will use)

Requires the Python package jupyter and, in VS Code, the “Jupyter” extension.

terminal
python3 -m pip install jupyter

Install it in your project’s .venv, as with any other package.

Notebooks and Cells

Notebook files have the extension .ipynb: interactive python notebook.

In VS Code, create an empty file with that extension and it opens as a notebook.

Two types of cells:

  • Markdown cells: formatted text (headers, links, bullets)
  • Code cells: run individually, with output, errors, and plots shown below the cell

Kernels

Code in a notebook is sent to a “kernel” that runs it.

  • For Python, the kernel is a Python interpreter
  • Use your project’s .venv as the kernel, where you installed jupyter
  • VS Code asks you to choose a kernel when you open a notebook; you can change it later

The .ipynb File Stores Output

.ipynb files are JSON text files: you can see each cell, but it is messy.

The output of code cells is saved in the file. This means

  • you can send the file to someone and they see your results without running it
  • notebook files can get very large
  • git diffs are a big mess

Interactive Window in VS Code

The Jupyter extension also gives you an “Interactive Window”.

This is like a better Python terminal.

  • Send code to it from a .py file
  • Displays output, errors, and plots more readably than the terminal

Interactive Notebooks

Notebook Pros and Cons

Pros

  • Documentation next to code, easy to share output
  • Documents are never out of sync with the code

Cons

  • Harder to maintain environments; others may struggle to run your code
  • Encourages one long linear file instead of scripts and functions
  • Harder to test and debug

When to Use Notebooks?

Good for:

  • trying out new ideas
  • reports, websites, and presentations (we will use Quarto later in the semester)

But your research project should not be in a notebook.

  • You want to run it end-to-end with only the necessary dependencies
  • You want others to run it easily, and to test/debug/optimize the code

Live Coding Example

Live Coding Example

  • Launch a Python REPL in VS Code
  • Launch an “Interactive Window” for Python in VS Code
  • Launch a Jupyter Notebook in VS Code