#controlStructures #U3-AOS1
### Jupyter notebook
![[IF_statements_and_control_flow(1).ipynb]]
# Python Conditions and If Statements
In programming, we use **control structures** to decide which parts of our code should run. The most fundamental of these is the `if` statement, which relies on logical conditions.
## 1. Logical Conditions
Python supports standard logical conditions from mathematics. These result in a **Boolean** value (either `True` or `False`).
|Operator|Meaning|Example|
|---|---|---|
|`==`|Equals|`a == b`|
|`!=`|Not Equals|`a != b`|
|`<`|Less than|`a < b`|
|`<=`|Less than or equal to|`a <= b`|
|`>`|Greater than|`a > b`|
|`>=`|Greater than or equal to|`a >= b`|
Export to Sheets
These conditions are most commonly used in **if statements** and **loops**.
---
## 2. The Basic "If" Statement
An `if` statement evaluates a condition.
- **If the condition is `True`:** The indented code block is executed.
- **If the condition is `False`:** The code block is skipped.
- ![[Pasted image 20260129124644.png]]
- ### Example
Here is a basic comparison between two variables, `a` and `b`.
Python
```python
a = 33
b = 200
if b > a:
print("b is greater than a")
```
**What is happening here?**
1. Python checks the condition: _Is 200 greater than 33?_
2. The result is **True**.
3. Therefore, the program runs the print statement and displays `"b is greater than a"`.
---
## 3. The Importance of Indentation
Unlike languages like C# or Java which use curly brackets `{ }` to define scope, **Python uses indentation** (whitespace at the beginning of a line).
> **Crucial Rule:** All statements within the same block of code must have the **same amount** of indentation. You can use tabs or spaces, but do not mix them.
### Correct Indentation (Multiple Statements)
You can have as many lines of code inside the `if` block as you like, provided they are indented equally.
Python
```python
age = 20
if age >= 18:
print("You are an adult") # Inside the if block
print("You can vote") # Inside the if block
print("You have full legal rights") # Inside the if block
print("This runs regardless") # Outside the if block
```
### Incorrect Indentation (Syntax Error)
If you do not indent the code immediately following the `if` statement, Python will raise an `IndentationError`.
Python
```python
a = 33
b = 200
if b > a:
print("b is greater than a") # This will cause an ERROR
```
---
## 4. Using Variables and "Truthiness"
You don't always need to compare two numbers. Python allows you to check Boolean variables directly, or check if a value exists.
### Using Boolean Variables directly
This is cleaner and considered "Pythonic" (best practice).
Python
```python
is_logged_in = True
# Good practice
if is_logged_in:
print("Welcome back!")
# Avoid doing this: if is_logged_in == True:
```
### Implicit Truth (Truthiness)
Python evaluates the "truthiness" of values even if they aren't Booleans.
- **Falsy Values (Treated as False):** Zero (`0`), empty strings (`""`), `None`, and empty lists/collections.
- **Truthy Values (Treated as True):** Any non-zero number (e.g., `5`, `-3`), and any non-empty string.
```python
number = 15
# Checks if number is not zero/empty
if number:
print("The number exists and is not 0")
```