Python If Statements
Make decisions in your code using if statements.
Make decisions in code.
Basic If Statement
```python age = 18
if age >= 18: print("You can vote!") ```
If-Else
```python temperature = 75
if temperature > 80: print("It's hot in Phoenix!") else: print("Nice weather in Seattle!") ```
If-Elif-Else
```python score = 85
if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") elif score >= 70: print("Grade: C") else: print("Grade: F") ```
Comparison Operators
```python x = 10
x == 10 # Equal to x != 5 # Not equal to x > 5 # Greater than x < 20 # Less than x >= 10 # Greater than or equal x <= 15 # Less than or equal ```
Logical Operators
```python age = 25 has_license = True
if age >= 18 and has_license: print("You can drive!")
if age < 18 or not has_license: print("Cannot drive") ```
Remember
- Indentation is important! - Use elif, not else if - and, or, not for logic