Module 2
>Operators and Control Flow
Use operators and decisions to make your Python programs calculate, compare, and repeat work.
// Lessons
5
// Quiz questions
50
// Reading time
49 min
// Exam length
15 Q
// Coding exercises
9
- 1Python OperatorsYou will use Python operators to calculate, compare values, and combine conditions. 10 min 10 questions
- 2Input, Output and f-stringsYou will format output clearly and understand how text entered with input becomes a string. 9 min 10 questions
- 3if...elif...else StatementsYou will choose which code runs by writing conditions with if, elif, and else. 10 min 10 questions
- 4for Loops and range()You will repeat work over collections and number ranges with for loops. 10 min 10 questions
- 5while Loops, break and continueYou will repeat code while a condition is true and control a loop with break and continue. 10 min 10 questions
Coding practice: write it yourself
Real Python, real checks: write your answer, press Run checks, and keep going until it passes. (0/4 passed this visit)
You can run every exercise now. Create a free ID to keep passes, code attempts and XP.
Lab 1: Even or odd?
Not passed yetnumber = 8. Print EVEN if it divides by 2 with no remainder, otherwise ODD.
number = 8 # your if/else here
Lab 2: Times table rows
Not passed yetUsing a for loop over range(1, 4), print:
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15
for i in range(1, 4):
# print each row
Lab 3: Pass mark check
Not passed yetBangladesh's pass mark is 33. For marks = 38, print PASS when marks is at least 33, else FAIL.
marks = 38 # PASS or FAIL?
Lab 4: Exam countdown
Not passed yetPrint a countdown from 3 down to 1, one number per line, using a while loop.
days = 3
while days > 0:
# print days, then decrease it
References & Further Reading
Related entries from PyLabs's own reference library.
- bool() (Convert a value to True or False.)
- range() (Make a sequence of evenly spaced integers.)
- enumerate() (Pair each item with a counter.)