Section 1

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.

Pre-test

What does type(10 / 4) return in Python 3?

Pre-test

What does int("banana") do in Python?

Pre-test

In Python 3, what do 10 / 4 and 10 // 4 evaluate to?

Pre-test

What does 17 % 5 evaluate to in Python?

Pre-test

What does 2 ** 3 + 1 evaluate to in Python?

Pre-test

Which line correctly checks whether patient_age equals 18?

Pre-test

If age is 20 and consent is False, what does age >= 18 and consent evaluate to?

Pre-test

Which line follows Python's standard naming convention for variables?

Pre-confidence

I can explain to a peer the difference between an int and a float in Python.

Not at all confident
Fully confident
Pre-confidence

I can explain to a peer why = and == do different things in Python, and when to use each.

Not at all confident
Fully confident
Pre-confidence

I can choose between `and`, `or`, and `not` to combine two true-or-false conditions in an if statement.

Not at all confident
Fully confident
Section 2

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.

Variables, data types and operators in Python.
Variables, data types and operators in Python.

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.

Section 3

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)  # 92

The 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 as 42 or -7.
  • Floating-point numbers (float) are decimal numbers, such as 3.14 or 5e-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, True and False. 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 it out

Try this snippet in the Python Scratchpad on the right.

Try this snippet
glucose_reading = 7.8
print(int(glucose_reading))
print(round(glucose_reading))
print(int(-2.9))
Variables and data types in Python.
Variables and data types in Python.

Now type the code below into the Python Scratchpad and run it.

Try it out

Try this snippet in the Python Scratchpad on the right.

Try this snippet
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, def or return.
class = "beta blocker"
# SyntaxError: invalid syntax

Python 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.

Section 4

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)  # True

An operator is a symbol or word that combines values into an expression. Operators fall into three families: arithmetic, comparison and logical.

Different operators in Python.
Different operators in Python.
Section 4.1

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 an int only if both numbers are int values.
  • % 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)  # 32

The remainder 23 % 4 is 3. This is because 4 goes into 23 five times and leaves 3.

Can you predict the output below?

Predict the output

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.

Code
print(9 / 2)
print(9 // 2)
print(9 % 2)
print(3 ** 2)

Type the code below into the Python Scratchpad and run it.

Try it out

Try this snippet in the Python Scratchpad on the right.

Try this snippet
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 str
Section 4.2

4.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)  # False

A single = stores a value. Suppose you type == where you meant =.

dose_mg = 40
dose_mg == 50
print(dose_mg)  # 40

The 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:

  • and gives True only if both sides are True.
  • or gives True if at least one side is True.
  • not turns True into False and False into True.

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)  # False

Each 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.

Parsons problem · Check enrolment eligibility with comparison and logical operators

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.

Line bank
  • if is_eligible = False:
  • is_eligible = age = 18 and consent_given
  • print("cannot enrol")
  • age = 17
  • consent_given = True
  • is_eligible = age >= 18 and consent_given
  • if not is_eligible:
  • is_eligible = age >= 18 or consent_given
Your solution
  • Drop lines here, in order.

Now type the code below into the Python Scratchpad and run it.

Try it out

Try this snippet in the Python Scratchpad on the right.

Try this snippet
p_value = 0.03
effect_size = 0.42
is_publishable = (p_value < 0.05) and (abs(effect_size) > 0.2)
print(is_publishable)
Section 4.3

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.7346938775513

Parentheses 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.

Operator precedence in Python.
Operator precedence in Python.
How Python decides which operator goes first, and why parentheses make your intent obvious.
How Python decides which operator goes first, and why parentheses make your intent obvious.

Predict what the code below prints.

Predict the output

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.

Code
print(2 + 3 * 4)
print(2 ** 3 * 2)
print(2 + 3 > 4 and 5)
Section 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.

Post-test

What does type(10 / 4) return in Python 3?

Post-test

What does int("banana") do in Python?

Post-test

In Python 3, what do 10 / 4 and 10 // 4 evaluate to?

Post-test

What does 17 % 5 evaluate to in Python?

Post-test

What does 2 ** 3 + 1 evaluate to in Python?

Post-test

Which line correctly checks whether patient_age equals 18?

Post-test

If age is 20 and consent is False, what does age >= 18 and consent evaluate to?

Post-test

Which line follows Python's standard naming convention for variables?

Post-confidence

I can explain to a peer the difference between an int and a float in Python.

Not at all confident
Fully confident
Post-confidence

I can explain to a peer why = and == do different things in Python, and when to use each.

Not at all confident
Fully confident
Post-confidence

I can choose between `and`, `or`, and `not` to combine two true-or-false conditions in an if statement.

Not at all confident
Fully confident
Section 6

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.

Your score

Submit the post-test to see your results.

Muddiest point

What is the one thing from this module that is still unclear to you?

Rate this module

Overall, how would you rate this module?

How likely are you to recommend this module to a peer? (0 = not at all, 10 = extremely likely)