Chapter 3: Control Flow
1. Conditional Statements
Conditional statements execute different code blocks based on conditions.
1.1 if
Statement
if
StatementThe simplest form of control flow. If a condition is True
, the code inside the if
block is executed.
Syntax:
Example:
1.2 if-else
Statement
if-else
StatementProvides an alternative code block if the condition is False
.
Syntax:
Example:
1.3 if-elif-else
Statement
if-elif-else
StatementTests multiple conditions in sequence.
Syntax:
Example:
1.4 Nested if
Statements
if
Statementsif
statements can be nested for more complex conditions.
Example:
1.5 Ternary Conditional Expression
A one-liner for if-else
statements.
Syntax:
Example:
1.6 Precautions with Conditionals
Proper Indentation: Python relies on indentation to define blocks. A mismatch will cause errors.
Avoid Overcomplicating: Use
elif
instead of nesting multipleif
statements.Boolean Values: Avoid redundancy. For example, instead of:
Simply use:
2. Loops
Loops allow you to execute code repeatedly.
2.1 while
Loop
while
LoopRepeats as long as a condition is True
.
Syntax:
Example:
Precautions:
Infinite Loops: Ensure the condition eventually becomes
False
.Use a
break
statement to exit loops if necessary.
2.2 for
Loop
for
LoopIterates over a sequence (e.g., list, string, range).
Syntax:
Example:
2.3 range()
in for
Loops
range()
in for
LoopsThe range()
function generates a sequence of numbers, commonly used in for
loops.
Examples:
2.4 break
and continue
break
and continue
break
: Exit the loop entirely.continue
: Skip to the next iteration.
Examples:
2.5 Nested Loops
Loops inside other loops.
Example:
3. Control Flow Tools
3.1 The pass
Statement
pass
StatementDoes nothing; acts as a placeholder.
Example:
3.2 The else
Clause in Loops
else
Clause in LoopsA for
or while
loop can have an else
clause. The else
block runs if the loop completes normally (i.e., no break
).
Example:
4. Comprehensions
4.1 List Comprehensions
A concise way to create lists.
Example:
4.2 Dictionary Comprehensions
Create dictionaries in one line.
Example:
5. Precautions and Tricks
Avoid Infinite Loops:
Always ensure the loop condition will eventually be
False
.
Keep Logic Simple:
Use comprehensions where possible for clarity and efficiency.
Don’t Modify a Collection While Iterating:
If you need to modify a list while looping, iterate over a copy:
else
in Loops:Remember that the
else
block executes only if the loop wasn't terminated bybreak
.
Summary Table: Control Flow Tools
if
Executes block if condition is True
.
if x > 5: print(x)
if-else
Adds an alternative block.
if x > 5: ... else: ...
if-elif-else
Tests multiple conditions.
if x > 5: ... elif x == 5: ...
while
Repeats as long as condition is True
.
while x < 5: ...
for
Iterates over a sequence.
for item in list: ...
break
Exits the loop entirely.
if x == 3: break
continue
Skips to the next iteration.
if x == 3: continue
pass
Does nothing (placeholder).
if x > 5: pass
else
in loops
Executes if loop completes normally.
for x in range(5): ... else: ...
Comprehensions
One-line list or dict creation.
[x**2 for x in range(5)]
Last updated