if-elif-else statement on one line in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. If-Elif-Else statement on one line in Python
  2. Shorthand if-else statement in Python

# If-Elif-Else statement on one line in Python

Use a nested ternary operator to implement an if-elif-else statement on one line.

The first ternary should check for a condition and if the condition is not met, it should return another ternary that does the job of an elif/else statement.

main.py
my_str = 'bobby hadz' result = ("Anonymous" if not my_str else my_str.upper() if len(my_str) > 2 else my_str.capitalize()) print(result) # ๐Ÿ‘‰๏ธ 'BOBBY HADZ'

if elif else statement on one line

The code for this article is available on GitHub

The ternary operator is very similar to an if/else statement.

main.py
name = 'Bobby Hadz' result = 'James Doe' if not name else name print(result) # ๐Ÿ‘‰๏ธ 'Bob'

The example checks if the name variable is falsy and if it is, the string "James Doe" is returned, otherwise, the name variable is returned.

# Using nested ternaries in Python

To have an inline if-elif-else statement, we have to use a nested ternary.

main.py
my_num = 50 result = 10 if my_num > 100 else 20 if my_num < 100 else 0 print(result) # ๐Ÿ‘‰๏ธ 20

using nested ternaries

The code for this article is available on GitHub

You can wrap the statement in parentheses to make it more readable.

main.py
my_num = 50 result = (10 if my_num > 100 else 20 if my_num < 100 else 0) print(result) # ๐Ÿ‘‰๏ธ 20

The first ternary in the example checks if the variable stores a value greater than 100.

If the condition is met, the number 10 gets returned.

If the condition isn't met, the else statement runs and the nested ternary operator gets returned.

The nested ternary operator checks if the variable is less than 100.

If the condition is met, the number 20 is returned, otherwise, 0 is returned.

Here is another example.

main.py
my_num = 50 result = ('a' if my_num > 100 else ('b' if my_num < 100 else 'c')) print(result) # ๐Ÿ‘‰๏ธ 'b'
The code for this article is available on GitHub

If the condition isn't met, the else statement runs and the nested ternary checks for another condition.

The role of the nested ternary is very similar to the role of an elif/else statement.

The nested ternary checks if the variable stores a value of less than 100 and if the condition is met, the string b gets returned. This is the elif statement.

If the condition isn't met, the else statement runs and the string c gets returned.

# The equivalent of the nested ternary in an if-elif-else statement

Here is how we would implement the ternary operator of the example using if/elif/else statements.

main.py
my_num = 50 if my_num > 100: result = 'a' elif my_num < 100: result = 'b' else: result = 'c' print(result) # ๐Ÿ‘‰๏ธ 'b'

the equivalent of the nested ternary in if elif else

The code for this article is available on GitHub

Using if-elif-else statements is a bit more readable, but it is also a bit more verbose.

Whether using a nested ternary operator makes your code more readable depends on the complexity of the conditions you are checking for.

Using the shorthand syntax isn't always recommended.

# Shorthand if-else statement in Python

The ternary operator can also be used if you need a shorthand if-else statement.

The ternary operator will return the value to the left if the condition is met, otherwise, the value in the else statement is returned.

main.py
variable1 = 50 variable2 = 100 result = 5 if variable1 > variable2 else 10 print(result) # ๐Ÿ‘‰๏ธ 10
The code for this article is available on GitHub

The ternary operator is very similar to an if/else statement.

The operator in the example checks if variable1 is greater than variable2.

If the condition is met, 5 is returned, otherwise, the else statement returns 10.

Here is the same code sample but using the longer form syntax.

main.py
variable1 = 50 variable2 = 100 if variable1 > variable2: result = 5 else: result = 10 print(result) # ๐Ÿ‘‰๏ธ 10

# a if condition else b

The syntax of the ternary operator is a if condition else b.

main.py
# ๐Ÿ‘‡๏ธ 'hello' print('hello' if len('hi') == 2 else 'bye')

You can also store the result in a variable.

main.py
result = 'hello' if len('hi') == 2 else 'bye' print(result) # ๐Ÿ‘‰๏ธ result

Here is another example.

main.py
name = None result = 'bobby hadz' if not name else name print(result) # ๐Ÿ‘‰๏ธ bobby hadz
The code for this article is available on GitHub

The example checks if the name variable is falsy and if it is the string "bobby hadz" is returned, otherwise the name variable is returned.

I've also written an article on how to check for multiple conditions in an if statement.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev