Conditionals
Code sample link: https://replit.com/@jjoco/python-conditionals
Conditionals are used to decide whether to run different codeblocks given some condition(s). In Python, this can be accomplished using if-else
statements. For those familiar with other languages, Python does not support switch-case
, unfortunately.
If statement
If-statements in Python are slightly different from other language syntax. Take the following example:
1 2 |
|
condition
is satisifed (ie is True
), then its code block is executed.
There are no parentheses surrounding the condition statement, and there are no curly braces wrapping the conditional code block.
Code statements that belong to the if
code block must be indented once more with respect to the if
statement.
The following is a valid if-statement:
1 2 3 4 5 6 7 |
|
count
is greater than 4
, the program goes into the if
code block to execute the print statement. However, in the following example:
1 2 3 4 5 6 7 |
|
if
statement since the if
condition was not satisfied.
Elif
Python does not use else if
; it uses elif
, instead.
1 2 3 4 5 |
|
An elif
block must be after an if
block, or else there will be runtime errors. Here's an example if-elif
block:
1 2 3 4 5 6 7 8 9 10 |
|
Here, count does not satisfy the initial if
condition; fortunately, the elif
condition is satisfied, and its respective code block is executed. Of course, if neither the if
nor elif
conditions are satisfied, any code within those blocks will be skipped.
Else
The else
block the block executed in a bigger if-else
block in case neither the initial if
condition nor the subsequent elif
conditions are met.
The syntax for else
is the following:
1 2 3 4 5 |
|
elif
, an else
block must be after an if
block, or else there will be runtime errors. Here's an example if-else
block:
1 2 3 4 5 6 7 8 9 |
|
if
condition was not satisfied, the else
block is executed.
Combined if-else
if
, elif
and else
can be combined like so:
1 2 3 4 5 6 7 8 9 10 |
|
Here's an example of a combined if-elif-else
block:
1 2 3 4 5 6 7 |
|
if
nor the elif
conditions are satisfied, the else block is executed.
Note that elif
and else
blocks cannot exist on their own without belonging to a bigger if
block.
Keep in mind that you can chain several elif
blocks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
elif
's choosing what letter grade to print out given a test grade:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Multiple condition checking
In Python, you can check multiple conditions in an if
or elif
before deciding to execute its respective code block. Important keywords are logical operators and
, or
, and not
.
and
If you want two conditions to be met before executing a block, use the and
keyword (&&
in other languages).
Syntax:
1 2 |
|
1 2 3 4 5 6 7 8 9 |
|
if
block is executed. Because I'm too low energy and don't have enough stuff in my fridge, I decide to order takeout.
or
If you want either of two conditions to meet before executing a code block, use or
(||
in other languages).
Syntax:
1 2 |
|
1 2 3 4 5 6 7 8 |
|
if
block is executed. Even though it's the afternoon, my energy levels are low enough, such that I decide to rest.
not
If you don't want a condition to be met before executing a code block, use not
(!
in other languages).
Syntax:
1 2 |
|
1 2 3 4 5 6 7 |
|
weather = "raining"
, then that code block would not execute.
Mixing multiple logical operators
You can have multiple logical operators in the same conditional line; use parentheses to denote which logical comparisons should take priority over others; an abstract example can look like:
1 2 |
|
1 2 |
|
1 2 |
|