Logic and Decision Making
Control flow is the order in which individual statements, instructions, or function calls are executed. Python uses standard conditional statements and loops, but with a focus on clean, readable syntax driven by indentation.
Conditional Statements (if, elif, else)
Python uses if, elif (short for else if), and else to perform different actions based on different conditions. Unlike Dart or JavaScript, parentheses around conditions are optional, and there are no curly braces.
Structural Pattern Matching (match)
Introduced in Python 3.10, the match statement is similar to switch in other languages but much more powerful. It can match values, types, and even the internal structure of objects.
Loops: for and while
Python's for loop is primarily an 'iterator'—it loops over a sequence (like a list, string, or range). The while loop continues as long as a condition remains true.
Loop Control: break, continue, and pass
- break: Terminates the loop immediately.
- continue: Skips the rest of the current iteration and moves to the next one.
- pass: A null operation; used as a placeholder when a statement is syntactically required but you want no action taken.
Comparison Operators
| Operator | Description | Example |
|---|---|---|
| == | Equal to | 5 == 5 (True) |
| != | Not equal to | 5 != 3 (True) |
| and / or | Logical gates | True and False (False) |
| in | Membership | 'a' in 'apple' (True) |
| is | Identity | x is y (True if same object) |