Loading Python Environment...

This may take a few seconds on first load

1. Introduction to Python

Welcome to this comprehensive Python tutorial! This course is designed for complete beginners with no prior programming experience. By the end, you'll be able to write Python programs to analyse pregnancy and health data.

What is Python?

Python is a programming language—a way for humans to give instructions to computers. Think of it like learning a new language, except instead of communicating with people, you're communicating with a computer.

Python is special because:

  • It reads like English: Python code is designed to be readable. Even without training, you can often guess what Python code does just by reading it.
  • It's "interpreted": You can run your code line by line and see immediate results, which makes learning and fixing mistakes (called "debugging") much faster.
  • It's beginner-friendly: Python allows you to write powerful programs with very few lines of code, so you can focus on solving problems rather than fighting the language.
  • It's widely used: Python is the #1 language for data science, machine learning, and scientific research. Skills you learn here are directly applicable to real jobs.

Key Terminology

Code: The instructions you write for the computer.
Program: A complete set of code that performs a task.
Run/Execute: Tell the computer to follow your instructions.
Output: What the computer shows you after running your code.

Why Use Python for Pregnancy & Health Data?

While pregnancy is a personal journey, understanding it on a larger scale involves analysing data. Public health officials, researchers, and clinicians analyse data to find patterns, understand risks, and improve care for mothers and babies.

Python is the leading language for this because:

  • Powerful Data Tools: Libraries (pre-written code packages) like Pandas and Matplotlib make it simple to clean, organise, analyse, and visualise large datasets of patient information.
  • Reproducibility: A Python script provides a clear, repeatable set of steps for an analysis. This is crucial for scientific validity—other researchers can run your exact analysis and verify your results.
  • Automation: You can write scripts to automate repetitive tasks, like processing patient records or generating weekly reports, saving time and reducing human error.
  • Free and Open Source: Python is completely free to use. There are no licensing fees, making it accessible to everyone.

Throughout This Tutorial

We'll work with real-world examples including: maternal age tracking, gestational age calculations, trimester classification, birth weight classification, blood pressure monitoring, APGAR score analysis, and due date calculations.

How This Tutorial Works

This is an interactive tutorial. Each lesson contains:

  1. Explanations: Clear descriptions of each concept in plain language.
  2. Code Examples: Real Python code you can see, edit, and run right in your browser.
  3. Try It Yourself: Opportunities to experiment and practice what you've learned.

The code boxes below run real Python code. Click the green "Run" button to execute the code and see the results. You can also edit the code and run it again—don't worry, you can't break anything!

Your First Python Program

Let's write your first program! The print() function is one of the most basic and useful tools in Python. It displays text (or numbers) on the screen. Whatever you put inside the parentheses will be shown as output.

Python - Your First Program
Output
Click "Run" to execute the code

Understanding the Code Above

Lines starting with # are called "comments". Python ignores them completely—they're just notes to help humans understand the code.

print("...") is a "function call". Functions are like actions—print is the action of displaying something. The text inside the quotes is what gets displayed.

Try It Yourself!

Now it's your turn! In the code box below, try these exercises:

  1. Change the message to say your name
  2. Add another print() line with a different message
  3. Try removing the quotation marks and see what error you get (this teaches error messages!)
Python - Try Editing This!
Output
Click "Run" to execute the code

Python Can Do Math!

Python is excellent at performing calculations. You can use it like a very powerful calculator. Here are the basic math operations:

  • + Addition (e.g., 5 + 3 gives 8)
  • - Subtraction (e.g., 10 - 4 gives 6)
  • * Multiplication (e.g., 6 * 7 gives 42)
  • / Division (e.g., 15 / 3 gives 5.0)
Python - Basic Calculations
Output
Click "Run" to execute the code

Notice Something?

In the code above, we used words like total_days and weeks to store numbers. These are called variables—and they're the topic of our next lesson!

Getting Started Offline (Optional)

This tutorial runs Python directly in your browser, so you don't need to install anything. However, if you want to write Python on your own computer later, here are your options:

Option 1: Google Colab (Easiest - No Install)

Go to Google Colab. It's free and runs in your browser. You just need a Google account.

Option 2: VS Code + Python (Recommended for Later)

1. Download Python
2. Download VS Code
3. Install the Python extension in VS Code

Option 3: Download This Tutorial

Download the Jupyter notebook version to practice offline:
Download Python_Pregnancy_Tutorial.zip

Next: Variables & Data Types

2. Variables and Data Types

In the last lesson, we used words like total_days and weeks to store numbers. These are called variables, and they're one of the most fundamental concepts in programming.

What is a Variable?

Think of a variable as a labeled box where you can store a piece of information. The label (variable name) helps you remember what's inside, and you can look inside the box whenever you need that information.

In health data, variables might store things like:

  • A patient's age (e.g., maternal_age = 34)
  • A measurement (e.g., birth_weight_kg = 3.45)
  • A patient ID (e.g., patient_id = "PAT-00123")
  • A yes/no answer (e.g., is_first_pregnancy = True)

Creating Variables

To create a variable in Python, you use the equals sign (=). This is called "assignment"—you're assigning a value to a name.

Important: = Means "Assign", Not "Equals"

In Python, = means "store this value in this variable". It's NOT a mathematical equation. When you write age = 34, you're saying "put the value 34 into the box labeled 'age'".

Python - Creating Variables
Output
Click "Run" to execute the code

Variable Naming Rules

Python has some rules about what you can name your variables:

Variable Naming Rules

✓ Must start with a letter or underscore: age, _temp
✓ Can contain letters, numbers, underscores: patient_id_2
✗ Cannot start with a number: 2nd_patient is NOT valid
✗ Cannot contain spaces: Use underscores instead: birth_weight
✗ Cannot use Python keywords: Words like if, for, print

Best Practice: Use Descriptive Names!

Use names that describe what the variable contains. maternal_age is much clearer than x or ma. Your future self (and colleagues) will thank you!

Data Types: What Kind of Data?

Every variable in Python has a type that tells Python what kind of data it holds. This is important because different types can do different things. Here are the four main types:

1. Integers (int) — Whole Numbers

Numbers without decimal points. Used for things like age, counts, or gestational weeks.

Python - Integers
Output
Click "Run" to execute the code

2. Floats (float) — Decimal Numbers

Numbers with decimal points. Used for measurements like weight, temperature, or percentages.

Python - Floats
Output
Click "Run" to execute the code

3. Strings (str) — Text

Text data enclosed in quotation marks. Used for names, IDs, notes, or any text.

Python - Strings
Output
Click "Run" to execute the code

4. Booleans (bool) — True/False

Values that can only be True or False. Perfect for yes/no questions.

Python - Booleans
Output
Click "Run" to execute the code

Operators: Doing Things with Variables

Now that you know how to store data, let's learn how to work with it using operators.

Arithmetic Operators (Math)

Python - Math Operators
Output
Click "Run" to execute the code

Comparison Operators

Comparison operators compare two values and return True or False:

Python - Comparison Operators
Output
Click "Run" to execute the code

= vs ==

= means "assign a value" (put something in a variable)
== means "is equal to?" (compare two values)

This is a common source of confusion for beginners!

F-Strings: Easy Text Formatting

You've seen f"..." in some examples. These are called f-strings (formatted strings), and they're the easiest way to combine text with variables:

Python - F-Strings
Output
Click "Run" to execute the code
Next: Data Structures

3. Data Structures: Lists and Dictionaries

So far we've stored single values in variables. But what if you need to track multiple blood pressure readings over time? Or store all the information about a patient in one place? That's where data structures come in.

Lists: Storing Multiple Items in Order

A list is like a numbered container that holds multiple items in a specific order. Think of it like a patient's chart where measurements are recorded chronologically.

Lists are created using square brackets [], with items separated by commas:

Python - Creating Lists
Output
Click "Run" to execute the code

Accessing Items by Index

Each item in a list has a position number called an index. Important: Python counts from 0!

Lists Start at Index 0, Not 1!

The first item is at index [0], the second at index [1], and so on. This trips up many beginners!
You can also use negative indices: [-1] is the last item, [-2] is second to last.

Python - Accessing List Items
Output
Click "Run" to execute the code

Modifying Lists

Lists are mutable, meaning you can change them after creating them:

Python - Modifying Lists
Output
Click "Run" to execute the code

Common List Methods

list.append(item) - Add item to end
list.pop() - Remove and return last item
list.insert(index, item) - Insert item at position
len(list) - Get number of items
sum(list) - Add up all numbers in list
min(list) / max(list) - Get smallest/largest

Dictionaries: Labeled Data

A dictionary stores data in key-value pairs. Think of it like a patient record where each piece of information has a label (key) and a value.

Dictionaries are created using curly braces {} with key: value pairs:

Python - Creating Dictionaries
Output
Click "Run" to execute the code

Why Dictionaries Are Powerful

Unlike lists where you access items by number (index 0, 1, 2...), dictionaries let you access data by meaningful names. This makes your code much more readable!

Python - Working with Dictionaries
Output
Click "Run" to execute the code

Lists vs Dictionaries: When to Use Which?

Use Lists when: You have a collection of similar items in order (e.g., weekly measurements, list of patients)
Use Dictionaries when: You have different pieces of information about one thing (e.g., a patient record with name, age, ID, etc.)

Lists of Dictionaries: Real-World Data

In real health data, you often have lists of dictionaries—multiple patient records, each containing multiple fields:

Python - Lists of Dictionaries
Output
Click "Run" to execute the code
Next: Conditionals

4. Making Decisions: Conditional Logic

In healthcare, programs often need to make decisions: Is this blood pressure high? What trimester is this patient in? Is the birth weight concerning? Conditionals allow your program to execute different code based on whether conditions are true or false.

The if Statement

The if statement is the simplest form of conditional logic. It runs a block of code only if a condition is true:

Python - Basic if Statement
Output
Click "Run" to execute the code

Indentation is Crucial!

In Python, indentation (4 spaces or 1 tab) determines what code belongs to the if statement. The indented lines only run if the condition is true. The non-indented line at the end always runs.

This is different from many other languages—Python uses indentation instead of curly braces!

if-else: Two Paths

Use else to specify what happens when the condition is false:

Python - if-else Statement
Output
Click "Run" to execute the code

if-elif-else: Multiple Paths

Use elif (short for "else if") when you have more than two possibilities:

Python - Trimester Classification
Output
Click "Run" to execute the code

How if-elif-else Works

Python checks each condition from top to bottom:
1. If the if condition is True → run that block, skip the rest
2. If False → check the first elif
3. Keep checking until one is True or you reach else
4. else runs only if ALL previous conditions were False

Birth Weight Classification

Here's a practical example classifying newborn birth weight:

Python - Birth Weight Classification
Output
Click "Run" to execute the code

Combining Conditions with and/or

Sometimes you need to check multiple conditions together:

  • and - Both conditions must be True
  • or - At least one condition must be True
  • not - Reverses True/False
Python - Combining Conditions
Output
Click "Run" to execute the code

Practice Makes Perfect!

Try modifying the values in the examples above and running them again. Change gestational_age to different values (8, 15, 30), or change the boolean values (True/False) to see how the output changes.

Next: Loops

5. Loops: Repeating Actions

Imagine you need to analyse 100 patient records. You wouldn't want to write the same code 100 times! Loops let you repeat code automatically, making them essential for processing health data at scale.

The for Loop

The most common loop in Python is the for loop. It goes through each item in a list (or other collection) one at a time:

Python - Basic for Loop
Output
Click "Run" to execute the code

How the for Loop Works

for week in gestational_weeks:

1. Take the first item from the list → store it in week → run the indented code
2. Take the next item → store it in week → run the code again
3. Continue until all items have been processed

The variable name (week) can be anything you choose—but use descriptive names!

Using range() for Counting

Sometimes you want to repeat something a specific number of times. Use range():

Python - Using range()
Output
Click "Run" to execute the code

range() Tips

range(5) → 0, 1, 2, 3, 4 (5 numbers, starting at 0)
range(1, 6) → 1, 2, 3, 4, 5 (from 1 up to, but not including, 6)
range(0, 10, 2) → 0, 2, 4, 6, 8 (count by 2s)

Loops with Conditionals

Combining loops with if statements is very powerful—you can check each item and respond accordingly:

Python - Loop + Conditional
Output
Click "Run" to execute the code

Accumulating Values in a Loop

A common pattern is to build up a result as you loop through data:

Python - Accumulating Values
Output
Click "Run" to execute the code

Looping Through Dictionaries

You can also loop through patient records stored as dictionaries:

Python - Looping Through Patient Records
Output
Click "Run" to execute the code
Next: Functions

6. Functions: Reusable Code

Imagine you need to calculate the BMI for 50 patients. Writing the formula weight / (height * height) 50 times would be tedious and prone to errors. Functions let you write code once and reuse it whenever you need it.

Parts of a Function

A function is like a recipe: it takes ingredients (inputs), does something with them (processing), and gives you a result (output).

Function Anatomy

def function_name(parameter):
    """Docstring explaining what it does"""
    # ... code that does something ...
    return result

def: Keyword that defines a function
parameter: Variable that receives input (the ingredients)
return: Keyword that sends back the result

Creating Your First Function

Let's create a simple function to calculate BMI:

Python - BMI Function
Output
Click "Run" to execute the code

Functions with Logic

Functions can contain any code, including if statements. Let's make a function that checks for fever:

Python - Clinical Logic Function
Output
Click "Run" to execute the code

A Real-World Example: Due Date Calculator

Let's build a function to estimate a due date. We'll use Python's datetime module to handle dates.

Python - Due Date Calculator
Output
Click "Run" to execute the code

Functions Returning Multiple Values

Functions can return more than one piece of information. This is useful for returning a result AND a status message.

Python - Multiple Return Values
Output
Click "Run" to execute the code
Next: Error Handling

7. Error Handling: Expecting the Unexpected

In the real world, health data is messy. You'll encounter missing values, typos (like "O positive" instead of "O+"), or text where numbers should be. If you don't handle these errors, your program will crash.

The try-except Block

Python uses try and except to handle errors gracefully:

try:
    # Attempt to run this code...
except ErrorName:
    # If that specific error happens, run this code instead!

Handling User Input Errors

Let's try to convert a string to a number. If the string contains letters, Python raises a ValueError.

Python - Handling ValueError
Output
Click "Run" to execute the code

Cleaning Data with Error Handling

Error handling is essential for "cleaning" data so you can analyse it. Let's filter out invalid APGAR scores so we can calculate an accurate average.

Python - Data Cleaning
Output
Click "Run" to execute the code

Catching Multiple Errors (Advanced)

Sometimes different things can go wrong. You can have multiple except blocks:

Python - Multiple Exceptions
Output
Click "Run" to execute the code
Next: Data Analysis

8. Putting It All Together: Capstone Project

Congratulations! You've learned the building blocks of Python. Now, let's combine them into a real data analysis script. We'll analyse a dataset of patient outcomes to identify preterm births and weight abnormalities.

Comprehensive Patient Analysis

The Plan

1. Data Structure: A list of dictionaries (Patient Database)
2. Logic: Functions to classify weight and check term status
3. Analysis: A loop to process each patient and update statistics
4. Output: A summary report

Python - Final Project
Output
Click "Run" to execute the code

Congratulations!

You've completed the Python for Health Data tutorial! You now know how to work with variables, data structures, conditionals, loops, functions, and error handling—all skills you can apply to real health data analysis.

Continue Learning

Download the Jupyter notebook version of this tutorial to practice on your own computer with larger datasets:

Download Tutorial Notebook