Summary
Control Statements:
In Python, control statements allow you to guide the flow of your program, depending on conditions and repeating certain actions.
- True, False, and Comparisons: Python uses Boolean values (True and False) for logical comparisons, helping to evaluate conditions.
- if Statements: These are used to make decisions in your code. If a condition is True, a specific block of code will execute.
- Formatting of if Statements: Proper indentation is crucial in Python to indicate which block of code belongs to the if condition.
- if, elif, and else: Multiple conditions can be checked using elif, and an else statement covers all remaining cases if none of the previous conditions are met.
- for Loops: A for loop is used to iterate over a sequence (like a list or range) and execute a block of code for each item.
- while Loops: A while loop continues executing as long as a specified condition is True.
Boolean Operators:
Boolean operators like and, or, and not allow you to combine multiple conditions and refine your logical tests.
break, continue, and pass:
- break: Exits a loop prematurely when a condition is met.
- continue: Skips the remaining code in the loop and moves to the next iteration.
- pass: A placeholder for code you want to implement later.
Infinite Loops: Be cautious of infinite loops, which occur when a loop’s exit condition is never met. Guard against them by ensuring your conditions are correct.
