That's more specifically a ternary operator expression than an if-then, here's the python syntax

value_when_true if condition else value_when_false

Better Example: (thanks Mr. Burns)

'Yes' if fruit == 'Apple' else 'No'

Now with assignment and contrast with if syntax

fruit = 'Apple'
isApple = True if fruit == 'Apple' else False

vs

fruit = 'Apple'
isApple = False
if fruit == 'Apple' : isApple = True
Answer from cmsjr on Stack Overflow
Discussions

single line if statement
Yes you can do single line ternaries in python. I'm not sure how "pythonic" it is to other developers, but I appreciate its brevity. the syntax is: expr if cond else expr in your case rowsTot = len(content) if not rows else rows or if you're explicitly checking None: rowsTot = len(content) if rows is None else rows to really rewrite it best i'd do something like rowsTot = rows if rows else len(content) More on reddit.com
🌐 r/learnpython
11
March 8, 2017
return if else else if in one line
Why are you wanting to have it as one line. Reduces readability and makes it more complicated to fix. What you should be able to do is to have a nested statement. Run the first expression, else if the second expression passes use statement2 return statement1 if expression1 else (statement2 if expression2 else statement3) You could continue nesting if you wanted More on reddit.com
🌐 r/learnpython
6
6
August 8, 2021
If-else condition in one liner
for x in lst: _prev = x if x else prev What could why do you need x to be overwritten before it’s next loop…can now just use prev for the rest of the code block. x, _prev = (x,x) if x else (_prev, _prev) This should illuminate though why it’s seem pretty ridiculous to need. And give you a direct answer yes we can do this if we really wanted. But x won’t change the list, to do that we should….just make a new list. new_replace = [] _prev = None for x in my_list: _prev = x if x else _prev #do_something(_prev) new_replace.append(_prev) We can one line this…well two line technically, maybe if the first value is never empty we can omit, not sure. _prev = None _replace = [(_prev := x if x else _prev) for x in my_list] Pretty straightforward use of the walrus operation. (All we are doing is assigning and returning at the same time.) At this point you can just throw walrus into whatever function itself. And get right to it. And forget about it lists and stuff. _prev = None for x in my_list: no_empty_function(_prev := x if x else _prev) This is actually a fairly fast progression of a concept. And IMHO significantly “one lined”. (Though we can go further.) Or some enumerate() stuff but it honestly screams walrus function. I don’t need to make indexes and reference them, I already used the last value I just use it again. All that referencing get troublesome with huge datasets. Now my_list can be my_generator. More on reddit.com
🌐 r/learnpython
29
1
April 18, 2023
how about one-line try-except statement ?
It fits a little awkwardly with "there should be one, and ideally only one, obvious way to to it". I know this was part of the reason they held off adding ternary operators on the early days (although of course they have them now). Speaking for myself, I can't think of many times when I've worked with code that this would help with. And I know they don't like to add language syntax unless it solves quite a lot of people's problems well. For something like your use case, where you're dealing with a common and expected case, it would be common to have an alternate method that returns a sentinel value. And indeed there is such a method, str.find. More on reddit.com
🌐 r/Python
6
1
June 14, 2024
🌐
W3Schools
w3schools.com › python › python_if_shorthand.asp
Python Shorthand If
Python Examples Python Compiler ... Python Certificate Python Training ... If you have only one statement to execute, you can put it on the same line as the if statement....
🌐
GeeksforGeeks
geeksforgeeks.org › python › one-liner-for-python-if-elif-else-statements
Python If Else in One Line - GeeksforGeeks
July 23, 2025 - else "Zero": If both conditions are False, it returns "Zero" as the default result. Python does not directly support a true one-liner for if-elif-else statements like it does for a simple if-else.
🌐
Sololearn
sololearn.com › en › Discuss › 3252109 › solved-python-oneliner-ifelse-statement
[Solved] Python one-liner if-else statement | Sololearn: Learn to code for FREE!
You can use the walrus operator to make an assignment in the same line where you use it. c = 9 if a > b else (c := 20) - 1 But I find it utterly pointless because the value 20 is never actually used or remembered any longer, anyway. Other oneliner options: d = 9 if a > b else 19 e = [19, 9][a > b]
🌐
LearnPython.com
learnpython.com › blog › python-if-in-one-line
How to Write the Python if Statement in one Line | LearnPython.com
This is the exact use case for a conditional expression! Here’s how we rewrite this if/else block in a single line: age = 8 is_baby = True if age < 5 else False print(is_baby) # output: # False · Much simpler! We hope you now know many ways to write a Python if in one line.
Find elsewhere
🌐
Sentry
sentry.io › sentry answers › python › python equivalent of a ternary operator: if-then-else in one line
Python equivalent of a ternary operator: if-then-else in one line | Sentry
It usually looks something like this: int x = b == true ? 1 : 0 // if b == true, x = 1; else x = 0 ... Yes, Python supports inline conditional assignments using a form of the ternary operator, usually called a conditional expression.
🌐
Python
wiki.python.org › moin › Powerful(20)Python(20)One(2d)Liners.html
Powerful Python One-Liners
However, if you use well-established one-liner tricks such as list comprehension or the ternary operator, they tend to be Pythonic.
🌐
Leapcell
leapcell.io › blog › how-to-use-python-if-else-in-one-line
How to Use Python `if...else` in One Line | Leapcell
July 25, 2025 - Use Python's one-line `if...else` for concise, readable conditional logic.
🌐
W3Schools
w3schools.com › python › python_conditions.asp
Python If Statement
The if statement evaluates a condition (an expression that results in True or False). If the condition is true, the code block inside the if statement is executed. If the condition is false, the code block is skipped.
🌐
Medium
medium.com › data-science › python-if-else-statement-in-one-line-ternary-operator-explained-eca2be64b7cc
Python If-Else Statement in One Line — Ternary Operator Explained | by Dario Radečić | TDS Archive | Medium
January 11, 2022 - Python If-Else Statement in One Line — Ternary Operator Explained Single-line conditionals in Python? Here’s when to and when NOT to use them. Python isn’t the fastest programming language out …
🌐
GeeksforGeeks
geeksforgeeks.org › python › different-ways-of-using-inline-if-in-python
Different Ways of Using Inline if (ternary operator) in Python - GeeksforGeeks
July 23, 2025 - Let's explore the different ways of using Inline if: In this example, we are comparing and finding the minimum number by using the ternary operator. ... Explanation: This is not a ternary expression, but rather a one-line if statement.
🌐
DEV Community
dev.to › kiani0x01 › python-one-line-if-else-statement-5817
Python One-Line If Else Statement - DEV Community
August 6, 2025 - Learn how to use Python's one-line if-else (ternary) expression for concise conditionals with examples, best practices, and tips.
🌐
Codingem
codingem.com › home › python if-else on one line
Python If-Else on One Line - codingem.com
May 17, 2023 - In Python, you can create an if else on one line of code. For example: num = 0 if value
🌐
ONEXT DIGITAL
onextdigital.com › home › python if else in one line: a concise guide to use it and practical use cases
Python if else in one line: A concise guide to use it and Practical use cases
November 7, 2023 - This is achieved using a feature called a “conditional expression” or “ternary operator” in Python. The syntax of the one-class ternary operator is as follows: some_expression if condition else other_expression · Consider an if-else statement that determines if a person is an adult depending on their age: age = 20 if age < 18: age_group = "Minor" else: age_group = "Adult" print(age_group) This functions fairly well. However, putting an if-else statement as a tidy one-line expression would perform the job much better.
🌐
Quora
quora.com › Should-I-condense-my-if-else-statements-in-Python-into-one-line-even-if-it-does-not-make-the-code-any-more-readable
Should I condense my if-else statements in Python into one line even if it does not make the code any more readable? - Quora
Answer (1 of 6): This is the sort of question that leads to quasi-religious dogmatism in answers. On the one hand there is PEP-8 which says you should not condense – except that it also has the clause “unless it is better to do so” (paraphrased). All the code formatting checkers that ...
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
As well as the while statement just introduced, Python uses a few more that we will encounter in this chapter. if Statements: Perhaps the most well-known statement type is the if statement. For exa...
🌐
YouTube
youtube.com › watch
Python If-Else One Line and Match-Case (Visually Explained) | #Python Course 16 - YouTube
Visually explained how to write if-else statements in one line and use the match-case structure in Python to simplify your conditional logic.Want More? 👇- ...
Published   July 22, 2025
🌐
Finxter
blog.finxter.com › home › learn python blog › if-then-else in one line python
If-Then-Else in One Line Python - Be on the Right Side of Change
September 23, 2022 - Yes, you can write most if statements in a single line of Python using any of the following methods: Write the if statement without else branch as a Python one-liner: if 42 in range(100): print("42").
🌐
Python documentation
docs.python.org › 3 › reference › compound_stmts.html
8. Compound statements — Python 3.14.3 documentation
Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way. In general, compound statements span multiple lines, although in simple incarnations a whole compound statement may be contained in one line. The if, while and for statements implement traditional control flow constructs.