Зміст курсу
Introduction to Python
3. Conditional Statements
Introduction to Python
if/elif/else Expressions
In our last example, you might've picked up on the fact that we didn't account for when the revenue is exactly $2000. In such a scenario, we're neither at a loss nor making a profit. So, we should tweak our code to handle such nuances.
To check a subsequent condition after your initial check, use elif
. The structure goes like this:
The code following the final else
will run only if none of the previous conditions are met.
Recall our previous example about string length. Let's adjust it. Now, we'll label a string as 'large' if it contains more than 20
characters, 'medium' if it has more than 10
(but certainly no more than 20
), and 'small' for anything less.
Note
You can stack multiple
elif
blocks as required. However, it's good to note that overusingelif
blocks isn't the most efficient way to structure your code.
Все було зрозуміло?
Зміст курсу
Introduction to Python
3. Conditional Statements
Introduction to Python
if/elif/else Expressions
In our last example, you might've picked up on the fact that we didn't account for when the revenue is exactly $2000. In such a scenario, we're neither at a loss nor making a profit. So, we should tweak our code to handle such nuances.
To check a subsequent condition after your initial check, use elif
. The structure goes like this:
The code following the final else
will run only if none of the previous conditions are met.
Recall our previous example about string length. Let's adjust it. Now, we'll label a string as 'large' if it contains more than 20
characters, 'medium' if it has more than 10
(but certainly no more than 20
), and 'small' for anything less.
Note
You can stack multiple
elif
blocks as required. However, it's good to note that overusingelif
blocks isn't the most efficient way to structure your code.
Все було зрозуміло?