Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn if/elif/else Expressions | Conditional Statements in Python
Introduction to Python (dev copy)

bookif/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:

if condition1:
    do this if condition1 is true
elif condition2:
    do this if condition1 isn't true, but condition2 is
else:
    do this if neither condition is true

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.

123456789101112131415161718192021
# Assign some medium string test = "medium string" # Conditional statements if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small") # One more checking test = "small" # Conditional statement if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small")
copy

Note

You can stack multiple elif blocks as required. However, it's good to note that overusing elif blocks isn't the most efficient way to structure your code.

question mark

Below, you're presented with an if/elif/else block. Given the values, what output would you expect if b = 150 and if b = 25?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 9

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 1.64

bookif/elif/else Expressions

Swipe to show menu

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:

if condition1:
    do this if condition1 is true
elif condition2:
    do this if condition1 isn't true, but condition2 is
else:
    do this if neither condition is true

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.

123456789101112131415161718192021
# Assign some medium string test = "medium string" # Conditional statements if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small") # One more checking test = "small" # Conditional statement if len(test) > 20: print("String: '", test, "' is large") elif len(test) > 10: print("String: '", test, "' is medium") else: print("String: '", test, "' is small")
copy

Note

You can stack multiple elif blocks as required. However, it's good to note that overusing elif blocks isn't the most efficient way to structure your code.

question mark

Below, you're presented with an if/elif/else block. Given the values, what output would you expect if b = 150 and if b = 25?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 9
some-alt