- Python: if-else in one line – ( A Ternary operator )
- if..else in a single line in python like a ternary operator
- Frequently Asked:
- Syntax of if…else in one line or ternary operator
- Example of if…else in one line
- Use if…else statement in one line carefully
- Related posts:
- Share your love
- Leave a Comment Cancel Reply
- Terms of Use
- Disclaimer
- Python return if else in one string
- # Table of Contents
- # If-Elif-Else statement on one line in Python
- # Using nested ternaries in Python
- # The equivalent of the nested ternary in an if-elif-else statement
- # Shorthand if-else statement in Python
- Python return if else in one string
- # Table of Contents
- # If-Elif-Else statement on one line in Python
- # Using nested ternaries in Python
- # The equivalent of the nested ternary in an if-elif-else statement
- # Shorthand if-else statement in Python
Python: if-else in one line – ( A Ternary operator )
In this article, we will learn how to use if-else in one line in python.
Other programming languages like C++ and Java have ternary operators, which are useful to make decision making in a single line. Python does not have a ternary operator. But in python, we can use the if-else in a single line, and it will give the same effect as the ternary operator.
So, let’s see how to use if-else in one line,
if..else in a single line in python like a ternary operator
In python, we can convert the if…else statement to a one-line conditional expression. For this, we need to write it in a specific format, checkout its syntax,
Frequently Asked:
Syntax of if…else in one line or ternary operator
value_1 if condition else value_2
When the condition evaluates to True, then the result of this one-liner if..else expression will be value_1. Whereas, if the condition evaluates to False, then the result of this one-liner expression will be value_2. Let’s see some examples of it.
Example of if…else in one line
In this one-liner expression, we are using an if…else statement in a single line.
- If the value of x is greater than 10, then the expression will return ‘High’.
- If the value of x is less than 10, then the expression will return ‘Low’.
We can assign the value returned by the expression to another variable.
Now let’s see how to use this one liner if else expression,
x = 18 result = 'High' if x > 10 else 'Low' print(result)
Here the value of x was 18, which is greater than 10. So one-liner if-else expression evaluated to ‘High’ and got assigned to variable result.
x = 5 result = 'High' if x > 10 else 'Low' print(result)
Here the value of x was 5, which is less than 10. So one-liner if-else expression evaluated to ‘Low’ and got assigned to variable result.
Use if…else statement in one line carefully
While using the if-else statement in one line, we should prefer using parenthesis to avoid confusion. Checkout following example,
x = 20 result = 10 + 10 if x > 100 else 0 print(result)
In this example, the conditional expression x>100 evaluated to False, and the value of the else portion, i.e., 0 was returned.
But what is the value of if portion in the above example? Is it 10 or 10 + 10 ?
The value of if-portion is 10+10, i.e., 20.
But it is a little confusing here because some might think that the if..else expression is 10 if x > 100 else 0 and then 10 to the value returned by expression. By this logic if x is less than 100, then the value of this expression should be 10. But that is wrong in this case.
So, to avoid such kind of confusion, we should proper brackets/parenthesis while using if…else in one line, like this,
x = 20 result = 10 + (10 if x > 100 else 0) print(result)
Now in this one-liner if-else statement, the value of the if-portion is 10, and the value in else-portion is 0.
Although python does not have a ternary operator, but still using if-else in one line, we can have an effect of a ternary operator in python.
Related posts:
Share your love
Leave a Comment Cancel Reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Terms of Use
Disclaimer
Copyright © 2023 thisPointer
To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Python return if else in one string
Last updated: Feb 21, 2023
Reading time · 3 min
# Table of Contents
# 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.
Copied!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'
The ternary operator is very similar to an if/else statement.
Copied!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.
Copied!my_num = 50 result = 10 if my_num > 100 else 20 if my_num 100 else 0 print(result) # 👉️ 20
You can wrap the statement in parentheses to make it more readable.
Copied!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.
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.
Copied!my_num = 50 result = ('a' if my_num > 100 else ('b' if my_num 100 else 'c')) print(result) # 👉️ 'b'
If the condition isn’t met, the else statement runs and the nested ternary checks for another condition.
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.
Copied!my_num = 50 if my_num > 100: result = 'a' elif my_num 100: result = 'b' else: result = 'c' print(result) # 👉️ 'b'
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.
Copied!variable1 = 50 variable2 = 100 result = 5 if variable1 > variable2 else 10 print(result) # 👉️ 10
The ternary operator is very similar to an if/else statement.
The operator in the example checks if variable1 is greater than variable2 .
Python return if else in one string
Last updated: Feb 21, 2023
Reading time · 3 min
# Table of Contents
# 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.
Copied!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'
The ternary operator is very similar to an if/else statement.
Copied!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.
Copied!my_num = 50 result = 10 if my_num > 100 else 20 if my_num 100 else 0 print(result) # 👉️ 20
You can wrap the statement in parentheses to make it more readable.
Copied!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.
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.
Copied!my_num = 50 result = ('a' if my_num > 100 else ('b' if my_num 100 else 'c')) print(result) # 👉️ 'b'
If the condition isn’t met, the else statement runs and the nested ternary checks for another condition.
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.
Copied!my_num = 50 if my_num > 100: result = 'a' elif my_num 100: result = 'b' else: result = 'c' print(result) # 👉️ 'b'
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.
Copied!variable1 = 50 variable2 = 100 result = 5 if variable1 > variable2 else 10 print(result) # 👉️ 10
The ternary operator is very similar to an if/else statement.
The operator in the example checks if variable1 is greater than variable2 .