1 Before you start
This is not graded, and it does not count towards anything. Answer every question even if you are only guessing — having a go at something before you have been taught it is one of the best ways to remember it afterwards. The lesson ends with another short check, and there you will be shown the correct answer for anything you missed. Choose an option for every question and confidence rating, then click Submit answers.
What does type(10 / 4) return in Python 3?
What does int("banana") do in Python?
In Python 3, what do 10 / 4 and 10 // 4 evaluate to?
What does 17 % 5 evaluate to in Python?
What does 2 ** 3 + 1 evaluate to in Python?
Which line correctly checks whether patient_age equals 18?
If age is 20 and consent is False, what does age >= 18 and consent evaluate to?
Which line follows Python's standard naming convention for variables?
I can explain to a peer the difference between an int and a float in Python.
I can explain to a peer why = and == do different things in Python, and when to use each.
I can choose between `and`, `or`, and `not` to combine two true-or-false conditions in an if statement.
2 Introduction
Welcome to your first Python programming lesson. A Python program stores values and combines them to make new values. This lesson covers how Python stores a value, the four basic types, and the operators that combine values.

By the end of this lesson, you will be able to read a short Python expression, predict what it prints, and explain why. Try every example in the Python Scratchpad on the right.
3 Variables and Data Types
A variable is a name that points to a value. You make one with a single equals sign (=). The name goes on the left. The value goes on the right.
patient_id = "P-00142"
heart_rate = 88
print(heart_rate) # 88
heart_rate = 92
print(heart_rate) # 92The fourth line makes the name heart_rate point to a new value, 92.
Every value has a type. Python decides the type for you.
This course uses four basic types:
- Integers (
int) are whole numbers, such as42or-7. - Floating-point numbers (
float) are decimal numbers, such as3.14or5e-8, which is 0.00000005. - Strings (
str) are text inside single or double quotes, such as"rs429358". Both kinds of quote work the same, so use one kind consistently. - Booleans (
bool) have exactly two values,TrueandFalse. Each one starts with a capital letter.
is_enrolled = True
print(is_enrolled) # True
is_enrolled = true
# NameError: name 'true' is not defined. Did you mean: 'True'?You can check the type of a value with the built-in type() function.
print(type(67.8)) # <class 'float'>
print(type("Hello world!")) # <class 'str'>You can convert a value to another type with int(), float(), str() and bool(). A conversion that makes no sense raises an error.
dose_text = "25"
print(int(dose_text) * 2) # 50
print(int("normal"))
# ValueError: invalid literal for int() with base 10: 'normal'Type the code below into the Python Scratchpad and run it.
Try this snippet in the Python Scratchpad on the right.
glucose_reading = 7.8
print(int(glucose_reading))
print(round(glucose_reading))
print(int(-2.9))

Now type the code below into the Python Scratchpad and run it.
Try this snippet in the Python Scratchpad on the right.
rsid = "rs429358"
p_value = 4.2e-8
is_significant = p_value < 5e-8
samples = 1247
print(type(rsid))
print(type(p_value))
print(type(is_significant))
print(type(samples))
Python enforces three rules for names:
- A name can contain letters, digits and underscores only.
- A name must start with a letter or an underscore.
- A name cannot be a reserved word, such as
class,deforreturn.
class = "beta blocker"
# SyntaxError: invalid syntaxPython programmers also follow a naming convention called snake_case: lowercase words joined by underscores, as in systolic_bp. A name should also describe the value, not its type.
4 Basic Operators and Expressions
An expression is anything Python can work out to a single value.
print(2 + 2) # 4
print(0.03 < 0.05) # TrueAn operator is a symbol or word that combines values into an expression. Operators fall into three families: arithmetic, comparison and logical.

4.1 Arithmetic operators
Python uses +, - and * for addition, subtraction and multiplication. It has four more arithmetic operators:
/is division. It always gives a float.//is floor division. It divides and rounds down. The result is anintonly if both numbers areintvalues.%is modulo. It gives the remainder after floor division.**raises a number to a power.
print(7 / 2) # 3.5
print(6 / 3) # 2.0
print(7 // 2) # 3
print(7.0 // 2) # 3.0
print(23 % 4) # 3
print(2 ** 5) # 32The remainder 23 % 4 is 3. This is because 4 goes into 23 five times and leaves 3.
Can you predict the output below?
Read the code carefully and type what you think it will print. Click Submit prediction for AI tutor feedback comparing your prediction against the real output, then click Reveal actual output to run the snippet yourself and see what happens.
print(9 / 2)
print(9 // 2)
print(9 % 2)
print(3 ** 2)
Type the code below into the Python Scratchpad and run it.
Try this snippet in the Python Scratchpad on the right.
print(7 + 3)
print(7 - 3)
print(7 * 3)
print(7 / 3)
print(7 // 3)
print(7 % 3)
print(2 ** 10)
Here is each combination in code.
print(2 + 3) # 5
print(2.5 + 3) # 5.5
print("heart" + "rate") # heartrate
print("heart" + 3)
# TypeError: can only concatenate str (not "int") to str4.2 Comparison and logical operators
Comparison operators compare two values and give back a bool. The operators <, >, <= and >= compare sizes. The operator == asks whether two values are equal. The operator != asks whether they are different.
dose_mg = 50
print(dose_mg == 50) # True
print(dose_mg != 50) # False
print(dose_mg < 40) # FalseA single = stores a value. Suppose you type == where you meant =.
dose_mg = 40
dose_mg == 50
print(dose_mg) # 40The second line compares 40 with 50 and throws the answer away. Python reports no error. The dose is still 40, not 50.
Logical operators combine bool values, so they work well with comparisons:
andgivesTrueonly if both sides areTrue.orgivesTrueif at least one side isTrue.notturnsTrueintoFalseandFalseintoTrue.
In the code below, egfr is a patient's estimated glomerular filtration rate (mL/min/1.73 m²), a measure of kidney function.
egfr = 72
is_enrolled = True
print(egfr >= 60 and is_enrolled) # True
print(egfr < 60 or is_enrolled) # True
print(not is_enrolled) # FalseEach line reads almost like English. If a side is not a bool, and and or give back one of the sides. For example, 3 > 1 and 8 gives 8.
Now arrange the lines below into a working program.
All the lines you need are in the Line bank on the left — some may be distractors you should leave behind. Drag the lines you need into the Your solution column on the right, in the correct order, then click Check.
Task: Given a patient's age and consent status, print a message only if they are NOT eligible to enrol. The patient is only eligible if their age is 18 or above and has given consent.
if is_eligible = False:is_eligible = age = 18 and consent_givenprint("cannot enrol")age = 17consent_given = Trueis_eligible = age >= 18 and consent_givenif not is_eligible:is_eligible = age >= 18 or consent_given
- Drop lines here, in order.
Now type the code below into the Python Scratchpad and run it.
Try this snippet in the Python Scratchpad on the right.
p_value = 0.03
effect_size = 0.42
is_publishable = (p_value < 0.05) and (abs(effect_size) > 0.2)
print(is_publishable)
4.3 Operator precedence
When an expression mixes operators, Python applies them in a fixed order: ** first, then *, /, // and %, then + and -, then comparisons, then not, then and, then or.
print(5 + 2 * 3) # 11
print((5 + 2) * 3) # 21
weight_kg = 72
height_m = 1.75
print(weight_kg / height_m ** 2) # 23.510204081632654
print((weight_kg / height_m) ** 2) # 1692.7346938775513Parentheses change the order. A misplaced pair can turn a BMI of 23.5 into 1692.7.
You do not need to memorise this order. When you are not sure, use parentheses ( ) to show the order you mean.

Predict what the code below prints.
Read the code carefully and type what you think it will print. Click Submit prediction for AI tutor feedback comparing your prediction against the real output, then click Reveal actual output to run the snippet yourself and see what happens.
print(2 + 3 * 4)
print(2 ** 3 * 2)
print(2 + 3 > 4 and 5)
5 Check your understanding
You have reached the end of the module. Try the same questions again — your answers here, paired with your pre-test answers, are how we measure what the module taught you. Answer every question and confidence rating, then click Submit and see results to view your score.
What does type(10 / 4) return in Python 3?
What does int("banana") do in Python?
In Python 3, what do 10 / 4 and 10 // 4 evaluate to?
What does 17 % 5 evaluate to in Python?
What does 2 ** 3 + 1 evaluate to in Python?
Which line correctly checks whether patient_age equals 18?
If age is 20 and consent is False, what does age >= 18 and consent evaluate to?
Which line follows Python's standard naming convention for variables?
I can explain to a peer the difference between an int and a float in Python.
I can explain to a peer why = and == do different things in Python, and when to use each.
I can choose between `and`, `or`, and `not` to combine two true-or-false conditions in an if statement.
6 Your results
Here is how your post-test answers compare with your pre-test answers. The pre/post pairing is the most reliable way to see what this module actually taught you.
Submit the post-test to see your results.
What is the one thing from this module that is still unclear to you?