If-Else Statements in Python
Master Python conditional logic with practical examples
This tutorial assumes basic Python knowledge including variables and print statements. You'll learn about Boolean data types, comparison operators, and conditional logic through hands-on examples.
Python Boolean Fundamentals
Boolean Data Type
Python's built-in Boolean type has only two values: True or False (with capital letters). Booleans are returned when comparing values or evaluating expressions.
Comparison Operators
Double equal signs (==) compare values for equality, while single equal signs (=) assign values to variables. This distinction is crucial for conditional logic.
Expression Evaluation
Python evaluates expressions like '2 * 2 == 4' and returns True or False. These Boolean results drive the logic in if-else statements.
Building Your First If Statement
Create the Condition
Write an expression that evaluates to True or False, such as '2 * 2 == 4' or 'number > 10'
Add If Keyword and Colon
Start with 'if' followed by your condition and end with a colon to indicate the beginning of the code block
Indent the Action Code
Use exactly four spaces to indent the code that runs when the condition is True. Proper indentation is required in Python
Add Else Block (Optional)
Include 'else:' with no condition to handle cases when the if condition is False, followed by indented alternative code
Assignment vs Comparison Operators
| Feature | Assignment (=) | Comparison (==) |
|---|---|---|
| Purpose | Assigns value to variable | Compares two values |
| Example | number = 4 | 2 * 2 == 4 |
| Result | Variable gets new value | Returns True or False |
| Use Case | Storing data | Creating conditions |
If evaluates this expression and if it returns true, then it does whatever we have here within the scope of the if statement
Common If-Else Patterns
Equality Check
Compare if a value equals an expected result, like checking if user input matches a target number. Returns True for exact matches only.
Range Comparison
Use greater than, less than operators to check if values fall within ranges. Example: 'number > 10' checks if input exceeds threshold.
User Input Validation
Combine input() function with int() conversion and conditional logic to validate and respond to user entries appropriately.
Python requires exactly four spaces for indentation after if and else statements. Incorrect indentation will cause IndentationError and prevent your code from running.
If-Else vs Multiple If Statements
Interactive Number Comparison Example
Get User Input
Use int(input('give me a number')) to convert string input to integer for numerical comparison
Set Up Comparison
Create condition like '2 * 2 == number' to check if user input matches mathematical result
Handle True Case
If condition is True (user enters 4), print 'yes' to confirm the match
Handle False Case
Else block executes for any other number, providing feedback for non-matching input
If-Else Statement Checklist
Single equals assigns values, double equals compares them
Python syntax requires colon to start the code block
Consistent indentation defines which code belongs to if/else
Verify both True and False conditions work as expected
Else has no condition - it catches everything the if missed
After mastering basic if-else statements, explore elif for multiple conditions, list comprehensions for compact conditional logic, and loops for repeated conditional operations.
Key Takeaways