Conditionals and Booleans
Part of: Programming Lab: Python for CSP
Making Decisions A conditional lets a program choose between paths. It tests a Boolean , a value that is either True or False, and runs different code depending on the result. The test age = 18 produces a Boolean. If it is True, the indented block under if runs. If it is False, the block under else runs. Only one branch runs, never both. Comparison Operators You build Booleans with comparisons: == (equal), != (not equal), <, , <=, =. Notice == with two equals signs tests equality, while a single = assigns. Mixing them up is a classic beginner bug. More Than Two Choices Use elif (short for else if) to check several conditions in order. Python tries each test top to bottom and runs the first block whose test is True. Order matters here. A score of 95 matches the first test and stops, so it never reaches the elif. Key takeaways: - A conditional chooses a path based on a Boolean. - == compares; a single = assigns. - if, elif, and else handle two or more choices. - Only the first matching branch runs.
Challenge: Even or Odd