Computer ScienceChapter 3: Programming Fundamentals (Python)
3.8.6 Conditional Statements
Let's consider an example where we take a number input from the user and determine whether the entered number is even or odd.
Explanation level
Let's consider an example where we take a number input from the user and determine whether the entered number is even or odd.
Example — Multiple if Statements: ```python num = eval(input("Enter a Number: ")) if num % 2 == 0: print("Entered Number Is Even.") if num % 2 == 1: print("Entered Number Is Odd.") ```
Run 1: ``` Enter a Number: 7 Entered Number is Odd. ```
Run 2: ``` Enter a Number: 4 Entered Number is Even. ```
IF-ELSE
Instead, a good choice to write the same code is as follows, where we check for a condition in `if` statement and the alternate condition is handled by an `else` statement, which handles any other possible outcomes.
Example: ```python num = eval(input("Enter a Number: ")) if num % 2 == 0: print("Entered Number Is Even.") else: print("Entered Number Is Odd.") ```
IF-ELIF-ELSE
Some people treat Zero neither as even nor odd. It means now we have 3 possibilities. The last program will treat zero as even integer. So, let's change it by incorporating `elif` which is short for `else-if statement`.
We changed the above program and tried to incorporate all possible outcomes of the program by using `if-elif-else`.
Example (Version 1 — Not Correct): ```python num = eval(input("Enter a Number: ")) if num % 2 == 0: print("Entered Number Is Zero.") elif num % 2 == 1: print("Entered Number is Odd.") else: print("Entered Number is Even.") ```
This does not provide the desired result because `num % 2 == 0` is true for zero AND for even numbers.
Example (Version 2 — Correct): ```python num = eval(input("Enter a Number: ")) if num == 0: print("Entered Number is Zero.") elif num % 2 == 1: print("Entered Number is Odd.") else: print("Entered Number is Even.") ```
Output: ``` Enter a Number: 0 Entered Number is Zero. Enter a Number: 6 Entered Number is Even. Enter a Number: 7 Entered Number is Odd. ```
Alternate method: Use logical operators `and` and `or` and `not` to combine multiple conditions in an `if-else` statement.
Intermediate Level
Arithmetic Operators — Detailed
| Operator | Meaning | Example | Result |
|---|---|---|---|
| `+` | Addition | `10 + 3` | `13` |
| `-` | Subtraction | `10 - 3` | `7` |
| `*` | Multiplication | `10 * 3` | `30` |
| `/` | Division (float) | `103` | `3.333...` |
| `%` | Modulus (remainder) | `10 % 3` | `1` |
| `//` | Floor division | `10 // 3` | `3` |
| `**` | Exponentiation | `10 ** 3` | `1000` |
Operator Precedence Table
| Precedence | Operators | Description |
|---|---|---|
| 1 (highest) | `**` | Exponentiation |
| 2 | `*`, `/`, `//`, `%` | Multiplication, Division, Floor Division, Modulus |
| 3 | `+`, `-` | Addition, Subtraction |
| 4 | `==`, `!=`, `>`, `<`, `>=`, `<=` | Comparison |
| 5 | `not` | Logical NOT |
| 6 | `and` | Logical AND |
| 7 (lowest) | `or` | Logical OR |
Parentheses `()` have the highest precedence of all and can be used to override the default order.
Comparison Operators — Detailed
| Operator | Meaning | Example | Result |
|---|---|---|---|
| `==` | Equal to | `5 == 5` | `True` |
| `!=` | Not equal to | `5 != 3` | `True` |
| `>` | Greater than | `5 > 3` | `True` |
| `<` | Less than | `5 < 3` | `False` |
| `>=` | Greater than or equal to | `5 >= 5` | `True` |
| `<=` | Less than or equal to | `5 <= 3` | `False` |
Logical Operators — Truth Table
| A | B | A and B | A or B | not A |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
IF-ELIF-ELSE Structure
```python if condition1: # execute if condition1 is True elif condition2: # execute if condition2 is True elif condition3: # execute if condition3 is True else: # execute if none of the above conditions are True ```
Important: Only one branch will execute — the first one whose condition is True.
Formula / Mathematical Form
Arithmetic Operators: ``` Addition: a + b Subtraction: a - b Multiplication: a * b Division: ab Modulus: a % b Floor Division: a // b Exponentiation: a ** b ```
Comparison Operators: ``` Equal: a == b Not Equal: a != b Greater Than: a > b Less Than: a < b Greater or Equal: a >= b Less or Equal: a <= b ```
Logical Operators: ``` AND: a and b OR: a or b NOT: not a ```
Solved Example
Example — Check if a Number is Positive, Negative, or Zero
Given: - A number entered by the user
Required: - Determine if the number is positive, negative, or zero
Code: ```python num = eval(input("Enter a number: "))
if num > 0: print("The number is positive.") elif num < 0: print("The number is negative.") else: print("The number is zero.") ```
Output (Run 1): ``` Enter a number: 5 The number is positive. ```
Output (Run 2): ``` Enter a number: -3 The number is negative. ```
Output (Run 3): ``` Enter a number: 0 The number is zero. ```
- `num > 0` — If true, number is positive
- `num < 0` — If true, number is negative
- `else` — If neither is true, number is zero
Important Points
- - Arithmetic operators perform mathematical calculations.
- - Modulo (`%`) returns the remainder of a division.
- - Integer division (`//`) returns only the integral part.
- - Exponentiation (`**`) raises a number to a power.
- - Operator precedence: `**` > `*`, `/`, `//`, `%` > `+`, `-`
- - Parentheses have the highest precedence.
- - Bitwise operators work on binary representations.
- - Membership operators (`in`, `not in`) check if a value exists in an object.
- - Comparison operators return `True` or `False`.
- - Logical operators (`and`, `or`, `not`) combine conditions.
- - `if-elif-else` allows multiple conditions to be checked in sequence.
- - Only one branch of `if-elif-else` executes.
Remember
- - Arithmetic Operators: `+`, `-`, `*`, `/`, `%`, `//`, `**`
- - Comparison Operators: `==`, `!=`, `>`, `<`, `>=`, `<=`
- - Logical Operators: `and`, `or`, `not`
- - Membership Operators: `in`, `not in`
- - Bitwise Operators: `&`, `|`, `^`, `~`, `<<`, `>>`
- - Precedence: `**` > `*`, `/`, `//`, `%` > `+`, `-`
- - Parentheses have the highest precedence.
- - `=` is assignment; `==` is comparison.
- - `if-elif-else` checks conditions in sequence.
- - Only one branch of `if-elif-else` executes.
Common Mistakes
- - Confusing `=` with `==`: `=` is assignment; `==` is comparison.
- - Forgetting operator precedence: `2 + 3 * 4` is `14`, not `20`. Use parentheses to change order.
- - Using `%` instead of `//` or vice versa: `%` gives remainder; `//` gives integer quotient.
- - Misunderstanding `and` vs `or`: `and` requires both conditions to be true; `or` requires at least one.
- - Using `not` incorrectly: `not` flips the result — `not True` is `False`.
- - Forgetting that only one branch executes in `if-elif-else`: Even if multiple conditions are true, only the first true branch runs.
- - Using bitwise operators when logical operators are needed: `&` is not the same as `and`.
- - Forgetting that comparison operators return booleans: `x = (5 > 3)` stores `True` in `x`.
Quick Revision
Other than the assignment operator (`=`) and normal arithmetic operators i.e. addition (`+`), subtraction (`-`), multiplication (`*`) and division (`/`), Python provides the option of:
- Modulo operator (`%`) — returns remainder of a division
- Integer division (`//`) — returns only integral value
- Exponentiation (`**`) — raises a number to a power
Python considers operator precedence in the sequence: `**`, `*`, `/`, `//`, `%`, `+`, `-`. Operators having same precedence are evaluated from left to right. Parentheses have the highest precedence.
Python provides operators for bitwise operations, which are used for comparison purposes of binary numbers.
Membership operators in Python are used to check whether a specified value either exists in an Object or not. The operators are `in` and `not in`.
Comparison operators include `==`, `!=`, `>`, `<`, `>=`, `<=`.
Logical operators include `and`, `or`, `not`.
Conditional statements include `if`, `if-else`, and `if-elif-else`.
60-Second Revision
- Arithmetic: `+`, `-`, `*`, `/`, `%`, `//`, `**`
- Comparison: `==`, `!=`, `>`, `<`, `>=`, `<=`
- Logical: `and`, `or`, `not`
- Membership: `in`, `not in`
- Precedence: `**` > `*`, `/`, `//`, `%` > `+`, `-`
- `=` assignment; `==` comparison.
- `and` = all true; `or` = at least one true; `not` = opposite.
- `if-elif-else` — only one branch executes.
- Warning: Always use `==` for comparison, not `=`.
Did you understand this topic?
Your answer is saved with your learning record. It does not mark the topic as mastered.
Quick revision
Revision Zone
Review the key facts, formulas and checks before you move on.3.8.6 Conditional Statements
Sticky Notes · Quick Revision
Key Point 1
- Arithmetic: `+`, `-`, `*`, `/`, `%`, `//`, `**`
Key Point 2
- Comparison: `==`, `!=`, `>`, `<`, `>=`, `<=`
Key Point 3
- Logical: `and`, `or`, `not`
Key Point 4
- Membership: `in`, `not in`
Key Point 5
- Bitwise: `&`, `|`, `^`, `~`, `<<`, `>>`
Key Point 6
- Precedence: `**` > `*`, `/`, `//`, `%` > `+`, `-`
Key Point 7
- `=` assignment; `==` comparison.
Key Point 8
- `if-elif-else` — only one branch executes.
Key Point 9
- `and` = all must be true; `or` = at least one true.
Key Point 10
- **0** = opposite.
Same supplied content as this Notes page (shared canonical source).