Skip to content
Python for Quantitative Finance / Control Flow and Simple Numerical Programs

Branching Programs and Boolean Logic

"If the option is in the money, exercise it; otherwise let it expire" is a conditional, and every payoff function this vault ever writes — a call's max⁡(ST−K,0)\max(S_T - K, 0), a knock-out barrier's zero-if-touched rule — is, at the code level, exactly this construct. This note introduces Python's conditional statement, the mechanics of nested and chained conditionals, and the semantic role of indentation, which is the single biggest surprise for anyone coming from a brace-delimited language. It closes on why conditionals alone are still not enough to write interesting programs — the missing piece, iteration, is the subject of the next lesson but one.

Learning objectives. By the end of this note you should be able to:
  • write a correct if/else and if/elif/else conditional, including nesting;
  • explain why Python uses indentation to delimit blocks and what changes if a statement is indented one level differently;
  • build and read a compound Boolean test using and, or, and comparison operators;
  • explain what "runs in constant time" means for a branching program and why that is a genuine limitation.

In this lesson

  1. The conditional statement
  2. Chained and nested conditionals
  3. Compound Boolean tests
  4. Why conditionals alone are still not enough

Before you start

The full lesson, with worked examples and exercise solutions, is for members.

Read the full lesson

New to SelfMadeQuant.com? See membership options or browse the Python for Quantitative Finance syllabus.