Please enable JavaScript.
Coggle requires JavaScript to display documents.
Think Python: Chapter 5 - Conditionals and recursion: - Coggle Diagram
Think Python: Chapter 5 - Conditionals and recursion:
Learning how programs make decisions and perform repeated tasks using recursion
5.4 Conditional Execution
The basic 'if' statement
Only runs a block of code if a condition is true
Purpose: Makes programs "react" to different situations.
5.5 Alternative Execution
if-else statement
: Two-way branching - one block runs if the condition is true; another if it's false.
5.6 Chained Conditionals
Using 'if-elif-else' for multiple tests in order.
Python checks conditions one by one top to bottom, and only the first true block runs.
(Downey, 2015, p.41)
5.7 Nested Conditionals
Placing one 'if' inside another
Can become messy, so it's often better to use 'elif' or combine conditions with and/or.
5.8 Recursion
Definition
: A function that calls itself
Key rule
: Always have a base case to avoid infinite loops.
Why use recursions:
Elegant solutions for problems like searching, sorting, or fractals (Downey, 2015, p.43)
5.9 Stack Diagrams for Recursive Functions
Visualizations: Each function call creates a new frame (stack).
Helpfuk for tracing how recursion progresses and "unfolds" back up.
5.10 Infinite Recursion
Happens if a recursion never reaches a base case.
Causes Python to throw a 'RecursionError' when too many calls are made (Downey, 2015, p.44).
5.1 Floor Division and Modulus
Floor decisions (//):
Divides two numbers and rounds down to the nearest whole number.
Modulus (%)
: Returns the remainder after division.
Use case:
Great for time calculations or checking divisibility
(Downey, 2015, p.39)
5.2 Boolean Expressions
Expressions that evaluate to either
True
or
False
Relationship operator: == (equal), !=(not equal), >, <, >=, <= (greater, lesser, etc.).
important concept: Python uses == for comparison, not = (assignment operator)
(Downey, 2025, p.40)
5.3 Logical Operators
Used to build more complex conditions.
and (both must be true)
or (at least one must be true)
not (negates a boolean value)
5.11 Keyboard Input
Getting user input with the input() function.
Important: Input is always read as a string, so it may need to be converted.
5.12 Debugging
Conditional bugs
: Forgetting to account for all possibilities.
Recursion bugs
: Forgetting the base case or reducing the problem incorrectly.
Downey (2025, p. 46)
emphasizes visualization and testing small cases when debugging recursion.