3.1. Conditionals#
The main topic of conditionals is the if statement, which executes different code depending on the program’s state. Some topics are essential to understand before you learn conditional statements:
boolean expressions for
True/Falsedecision, andlogical operators for combining Boolean expressions.
There are five main kinds of conditional statements:
Conditional Execution
Alternative Execution
Chained Conditional
Nested Conditional
The Match Statement (not covered in this section)
We will start by looking at a use case of integer division and modulus operators to clarify the concept of if/then conditions.
3.1.1. Integer division and modulus#
Recall that the integer division operator, //, divides two numbers and rounds down to an integer.
Integer division and modulus are often used together with conditional statements. Recall that for division we have:
floating-point division operator
/calculates the precise mathematical quotient, including any fractional part, and always returns a float data type.integer division (also called floor division) operator,
//, performs division and then discards the fractional part, effectively rounding down to the nearest whole number (the “floor”);
Operator |
What it does |
Common conditional use |
|---|---|---|
|
Gets remainder |
Check if a number is even, multiple, cycle, etc. |
|
Gets integer quotient |
Check if a number is in a “group” or “range” |
For example, suppose the run time of a movie is 105 minutes. You might want to know how long that is in hours. Conventional division returns a floating-point number:
minutes = 105
minutes / 60
1.75
But we don’t normally write hours with decimal points. Integer division returns the integer number of hours, rounding down:
minutes = 105
hours = minutes // 60
hours
1
To get the remainder, you could subtract off one hour in minutes:
remainder = minutes - hours * 60
remainder
45
Or you could use the modulus operator, %, which divides two numbers and returns the remainder.
remainder = minutes % 60
remainder
45
The modulus operator is more useful than it might seem.
For example, it can check whether one number is divisible by another – if x % y is zero, then x is divisible by y.
Also, it can extract the right-most digit or digits from a number.
For example, x % 10 yields the right-most digit of x (in base 10).
Similarly, x % 100 yields the last two digits.
x = 123
x % 10
3
x % 100
23
Finally, the modulus operator can do “clock arithmetic”. For example, if an event starts at 11 AM and lasts three hours, we can use the modulus operator to figure out what time it ends.
start = 11
duration = 3
end = (start + duration) % 12
end
2
The event would end at 2 PM.
### Exercise: modulus & floor division
### How many hours, minutes, and seconds are there in 12345 seconds?
### (Convert an integer, n in seconds, into hours, minutes, and seconds.)
### Use f-string to print
### Your output should be the same as the cell below
### Your code starts here
### Your code ends here
3 hours, 25 minutes, and 45 seconds.
3.1.2. if statements#
if is a Python keyword.
if statements have the same structure as function definitions: a
header followed by an indented statement or sequence of statements called a block.
The boolean expression after if is called the condition.
If it is true, the statements in the indented block run. If not, they don’t.
There is no limit to the number of statements that can appear in the block, but there has to be at least one.
Occasionally, it is useful to have a block that does nothing – usually as a place keeper for code you haven’t written yet.
In that case, you can use the pass statement, which does nothing.
if x < 0:
pass # TODO: need to handle negative values!
The word TODO in a comment is a conventional reminder that there’s something you need to do later. Modern IDE’s would recognize it and act accordingly.
To write useful programs, we almost always need the ability to check conditions and adjust the program’s behavior accordingly. Python uses if statements to handle conditional logic, offering several structures to control program flow:
conditional execution (if),
alternative execution (if/else),
chained execution (if/elif/else),
nested conditions, and
match statement.
3.1.2.1. Conditional execution (if)#
Conditional gives us the ability to check the conditions and is the simplest form of the if statement:
if x > 0:
print('x is positive')
x is positive
if x < 0:
pass # TODO: need to handle negative values!
The word TODO in a comment is a conventional reminder that there’s something you need to do later. Modern IDEs would recognize it and act accordingly.
The pass statement in Python is a placeholder that does nothing when executed. It is used to keep code blocks valid where a statement is required, but no logic is needed yet. It is for creating empty functions, classes, loops, or conditional blocks.
3.1.2.2. Alternative Execution (else)#
An if statement can have a second part, called an else clause. If the condition is true, the first indented statement runs; otherwise, the second indented statement runs. For example:
num = int(input("Please enter an integer: "))
if num % 2 == 0: ### % modulus operator
print("num is even")
else:
print("num is odd")
Output:
Please enter an integer: 5
num is odd
In this example, if num is even, the remainder when num is divided by 2 is 0, so the condition is True and the program displays num is even. If num is odd, the remainder is 1, so the condition is false, and the program displays num is odd.
Since the condition must be true or false, exactly one of the alternatives will run. The alternatives are called branches.
3.1.2.3. Chained Conditional (elif)#
Sometimes there are more than two possibilities, and we need more than two branches. One way to express a computation like that is a chained conditional, which includes an elif clause.
elif is an abbreviation of “else if” and is used to add multiple conditions inside an if block. There is no limit on the number of elif clauses.
If there is an else clause, it has to be at the end, but there doesn’t have to be one.
Note that:
Each condition is checked in order.
If the first is false, the next is checked, and so on.
If one of them is true, the corresponding branch runs, and the
ifstatement ends.Even if more than one condition is true, only the first true branch runs.
A good example of a chained conditional statement is:
num = int(input("Please enter an integer: "))
if num > 0:
print("positive")
elif num == 0:
print("neither positive or negative")
elif num < 0:
print("negative")
else:
print("I don't know what you are talking about.")
### Exercise:
### Write a Python script that asks the user to input a numerical
### score (0-100). Based on that score, the program should print a specific message.
### Requirements:
### If the score is 90 or above, print: "Excellent! You got an A."
### If the score is between 70 and 89, print: "Good job! You passed."
### If the score is below 70, print: "Keep practicing! Try again."
### Bonus: Add a check at the very beginning. If the user enters a
### number greater than 100 or less than 0, print: "Invalid score. Please enter a value between 0 and 100."
### Your code starts here.
### Your code ends here.
Excellent! You got an A.
3.1.2.4. Nested Conditionals#
One conditional can also be nested within another. We could have written a conditional to compare two numbers:
x = 5
y = 10
print(f"x: {x}; y: {y}")
if x == y:
print('x and y are equal')
else:
if x < y:
print('x is less than y')
else:
print('x is greater than y')
x: 5; y: 10
x is less than y
In this example, the outer if statement contains two branches:
The first branch contains a simple statement.
The second branch contains another
ifstatement with two branches of its own.
Those two branches are both simple print statements, although they could have been conditional statements as well.
Although the indentation of the statements makes the structure apparent, nested conditionals can be difficult to read. I suggest you avoid them whenever possible.
Logical operators often simplify nested conditional statements. Here’s an example with a nested conditional.
if 0 < x:
if x < 10:
print('x is a positive single-digit number.')
x is a positive single-digit number.
The print statement runs only if we make it past both conditionals, so we get the same effect with the and operator.
if 0 < x and x < 10:
print('x is a positive single-digit number.')
x is a positive single-digit number.
For this kind of condition, Python provides a more concise option:
if 0 < x < 10:
print('x is a positive single-digit number.')
x is a positive single-digit number.
### Exercise: Nested Conditional
### Create a simple login system that verifies a username and then a password.
### For this exercise, set the "correct" credentials to:
### Username: admin
### Password: password123
### Requirements:
### Ask the user to input a username.
### First Level (Username Check):
### If the username is correct ("admin"), ask for a password.
### If the username is incorrect, print: "Access Denied: User not found."
### Second Level (Password Check - Nested):
### If the password is correct ("password123"), print: "Welcome, Admin! Login successful."
### If the password is incorrect, print: "Access Denied: Incorrect password."
### Your code starts here
### Your code ends here
Welcome, Admin! Login successful.