Skip to contentSkip to navigation

Module 2 of 6, Python Foundations

Operators and Control Flow

Calculate with operators, talk to the user, and steer a program with decisions and loops.

module checksSign in to track

  • Lessons complete-, not done
  • Lesson quizzes taken-, not done
  • Module Labs passed-, not done
  • Module exam passed at 80%-, not done
lessons
5
quiz questions
60
reading time
40 min
exam length
15 Q
coding exercises
9

Lessons

  1. Operators & ExpressionsCalculate with arithmetic operators, compare values, combine conditions with and, or and not, check membership with in, and know the order Python applies them in. 8 min 12 questions
  2. Interactive Input & OutputRead what a user types with input(), convert it before doing maths, and format results neatly with f-strings, decimal places, thousands separators and aligned columns. 8 min 12 questions
  3. Conditional Branching (if / elif / else)Let a program make decisions with if, elif and else, combine conditions, use truthy and falsy values, and keep branching code readable. 8 min 12 questions
  4. Definite Iteration & for LoopsRepeat work for every item with for loops: walk through strings and lists, count with range(), build running totals, and get positions with enumerate(). 8 min 12 questions
  5. Indefinite Loops & Loop ControlRepeat until something changes with while loops, avoid infinite loops, leave a loop early with break, skip an item with continue, and use the loop else clause. 8 min 12 questions

Lab: write it yourself

Real Python, real checks: write your answer, press Run checks, and keep going until it passes. (4 exercises)

All module exercises

The Lab needs an account. Sign in or create a free ID to run these exercises.

Lab 1: Even or odd?

Sign-in required

number = 8. Print EVEN if it divides by 2 with no remainder, otherwise ODD.

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.

Lab 2: Times table rows

Sign-in required

Using a for loop over range(1, 4), print:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.

Lab 3: Pass mark check

Sign-in required

Bangladesh's pass mark is 33. For marks = 38, print PASS when marks is at least 33, else FAIL.

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.

Lab 4: Exam countdown

Sign-in required

Print a countdown from 3 down to 1, one number per line, using a while loop.

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.

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