Overview

Before we code: key concepts

  • Object-oriented programming
  • Data types
  • Functions, methods, attributes
  • Control flow

Python 101

  • Data types
  • Variable/object assignment
  • Working with strings
  • Lists
  • Dictionaries
  • Control flow
  • Functions and methods
  • Libraries

Data management with pandas

  • Loading data
  • Inspecting data
  • Subsetting data
  • Tidy data management

Shifting paradigms: FP ➡️ OOP

  • This morning, we explored functional programming (FP) in R.
  • We’re now shifting to Python, where object-oriented programming (OOP) is the dominant paradigm

FP vs. OOP

Paradigm Language Focus Example
FP R What to do with objects
summary <- mtcars %>%
  filter(mpg > 20) %>%
  group_by(cyl) %>%
  summarize(mean_hp = mean(hp))
OOP Python What the object does
summary = (
    mtcars[mtcars["mpg"] > 20]
    .groupby("cyl")
    .agg(mean_hp=("hp", "mean"))
)

In Python, objects have built-in behavior; in R, behavior is typically external to the object.

Data types

Data type R Python
Integers numeric int
Floating point numbers numeric float
Boolean values logical bool
Strings character str

Functions, Methods Attributes

In Python, objects contain functions, methods, and attributes:

  • A function takes an object as an input (e.g., len(my_list)).
  • A method is called by the object itself (e.g., my_list.append(42)).
  • An attribute is accessed directly from the object without parentheses (e.g.: my_list.size)

We use . to call functions/methods/attributes from objects.

Control Flow

  • R relies heavily on vectorized operations and functional tools for many tasks.

  • Python, by contrast, emphasizes explicit control over how code is executed.

  • In Python, control flow is how we:

    • Make decisions
    • Repeat steps
    • React to inputs
    • Write code that’s more dynamic

Control flow

The core building blocks of control flow in Python are:

  • if statements, which run code only if a condition is True
  • while loops, which repeat code while a condition remains True
  • for loops, which run code for each item in a container (like a list or dictionary)
  • break and continue statements, which allow you to modify the behavior of loops

Each of these relies on Boolean values: data types that can only be True or False.