Course Content
Conditional Statements in Python
Conditional Statements in Python
Logical Operators
Previously, we explored situations involving a single condition in the if statement. Now, let's delve into scenarios where we need to evaluate multiple conditions.
One approach is to use nested if
statements, as demonstrated in the example:
Example 1:
is_adult = True has_license = True if is_adult: if has_license: print("You can drive car")
However, relying heavily on nested if
conditions is not considered best practice. A seasoned developer would prefer to use logical operators instead of nesting conditions.
Example 2:
is_adult = True has_license = True if is_adult and has_license: print("You can drive car")
Python language has 3 logical operators:
Note
In Python syntax, each "empty" value is equivalent to
False
, and any "non-empty" value is equivalent toTrue
. For example, 0 is False, 1 is True, [ ] is False, [1,] is True, " " is False, "a" is True, etc.
Example 3:
list = [] if list: # same as `if True:` print("List is filled") if not list: # same as `if not False:` print("List is empty")
Let's continue by examining conditional statements with multiple conditions. Imagine you've taken exams in three subjects and received the following results: math_exam = 95
, english_exam = 90
, programming_exam = 100
. You've decided to apply to three different universities, each with its own admission requirements. Let's explore these requirements.
To enter the first university, you must have a score greater than or equal to 90 in all three subjects simultaneously. Let's see if you meet this university's criteria.
# Your scores math_exam = 95 english_exam = 90 programming_exam = 100 # Checking whether you will pass to the first university if math_exam >= 90 and english_exam >= 90 and programming_exam >= 90: print('Congratulations! You are enrolled in our university')
As we can see, your scores from all exams are greater than or equal to 90, so our if statement worked.
Then move on to the next university. Here the condition is different, since this is the best university in your city, your scores must be greater than or equal to 95.
# Your scores math_exam = 95 english_exam = 90 programming_exam = 100 # Checking whether you will pass to the second university if math_exam >= 95 and english_exam >= 95 and programming_exam >= 95: print('Congratulations! You are enrolled in our university')
As we see that our condition is not fulfilled, since we have two objects that satisfy the condition, but the third object, namely english_exam = 90
, it is less than 95. Therefore, we do not get anything as a result, and our if statement is not executed.
Moving on to the next university. Here the condition is quite simple. In order to pass here, you need to have at least one subject that has passed 100 points.
It is obvious that for this case we need to use the or operator.
# Your scores math_exam = 95 english_exam = 90 programming_exam = 100 # Checking whether you will pass to the third university if math_exam == 100 or english_exam == 100 or programming_exam == 100: print('Congratulations! You are enrolled in our university')
As we can see, we still have one subject with a score of 100. It's important to note that for the or
operator, it's enough for just one condition to be True
.
It's worth remembering that if none of the conditions are True
, the if
statement won't be executed, and you won't meet the criteria.
Now, it's time to practice!
Thanks for your feedback!