Your C code doesn't contain two return statements. Neither should your python code... The translation of your ternary expression is n if n<m else m, so just use that expression when you return the value:

def minn(n,m):
    return n if n<m else m
Answer from Karoly Horvath on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › ternary-operator-in-python
Ternary Operator in Python - GeeksforGeeks
The ternary operator in Python ... expression because it evaluates a condition and returns one value if the condition is True and another if it is False....
Published   December 20, 2025
🌐
Dataquest
dataquest.io › blog › python-ternary-operator
Python Ternary: How to Use It and Why It's Useful (with Examples)
March 6, 2023 - If the standard syntax for the Python ternary operator is a if condition else b, here we would re-write it as (b, a)[condition], like this: t = 90 ('Water is not boiling', 'Water is boiling')[t >= 100] ... In the syntax above, the first item of the tuple is the value that will be returned if the condition evaluates to False (since False==0), while the second is the value that will be returned if the condition evaluates to True (since True==1).
🌐
Python Tips
book.pythontips.com › en › latest › ternary_operators.html
6. Ternary Operators — Python Tips 0.1 documentation
In python there is also the shorthand ... Python 2.5 and can be used in python 2.5 or greater. ... The first statement (True or “Some”) will return True and the second statement (False or “Some”) will return Some....
🌐
Mimo
mimo.org › glossary › python › ternary-operator
Python Ternary Operator: Syntax, Usage, and Examples
The ternary operator (also called ... Syntax:value_if_true if condition else value_if_false · The condition is evaluated first. If the condition is True, value_if_true is returned....
Top answer
1 of 16
9351

Yes, it was added in version 2.5. The expression syntax is:

a if condition else b

First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, or else when b is evaluated and returned but a is ignored.

This allows short-circuiting because when condition is true only a is evaluated and b is not evaluated at all, but when condition is false only b is evaluated and a is not evaluated at all.

For example:

>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'

Note that conditionals are an expression, not a statement. This means you can't use statements such as pass, or assignments with = (or "augmented" assignments like +=), within a conditional expression:

>>> pass if False else pass
  File "<stdin>", line 1
    pass if False else pass
         ^
SyntaxError: invalid syntax

>>> # Python parses this as `x = (1 if False else y) = 2`
>>> # The `(1 if False else x)` part is actually valid, but
>>> # it can't be on the left-hand side of `=`.
>>> x = 1 if False else y = 2
  File "<stdin>", line 1
SyntaxError: cannot assign to conditional expression

>>> # If we parenthesize it instead...
>>> (x = 1) if False else (y = 2)
  File "<stdin>", line 1
    (x = 1) if False else (y = 2)
       ^
SyntaxError: invalid syntax

(In 3.8 and above, the := "walrus" operator allows simple assignment of values as an expression, which is then compatible with this syntax. But please don't write code like that; it will quickly become very difficult to understand.)

Similarly, because it is an expression, the else part is mandatory:

# Invalid syntax: we didn't specify what the value should be if the 
# condition isn't met. It doesn't matter if we can verify that
# ahead of time.
a if True

You can, however, use conditional expressions to assign a variable like so:

x = a if True else b

Or for example to return a value:

# Of course we should just use the standard library `max`;
# this is just for demonstration purposes.
def my_max(a, b):
    return a if a > b else b

Think of the conditional expression as switching between two values. We can use it when we are in a 'one value or another' situation, where we will do the same thing with the result, regardless of whether the condition is met. We use the expression to compute the value, and then do something with it. If you need to do something different depending on the condition, then use a normal if statement instead.


Keep in mind that it's frowned upon by some Pythonistas for several reasons:

  • The order of the arguments is different from those of the classic condition ? a : b ternary operator from many other languages (such as C, C++, Go, Perl, Ruby, Java, JavaScript, etc.), which may lead to bugs when people unfamiliar with Python's "surprising" behaviour use it (they may reverse the argument order).
  • Some find it "unwieldy", since it goes contrary to the normal flow of thought (thinking of the condition first and then the effects).
  • Stylistic reasons. (Although the 'inline if' can be really useful, and make your script more concise, it really does complicate your code)

If you're having trouble remembering the order, then remember that when read aloud, you (almost) say what you mean. For example, x = 4 if b > 8 else 9 is read aloud as x will be 4 if b is greater than 8 otherwise 9.

Official documentation:

  • Conditional expressions
  • Is there an equivalent of C’s ”?:” ternary operator?
2 of 16
1028

You can index into a tuple:

(falseValue, trueValue)[test]

test needs to return True or False.
It might be safer to always implement it as:

(falseValue, trueValue)[test == True]

or you can use the built-in bool() to assure a Boolean value:

(falseValue, trueValue)[bool(<expression>)]
🌐
Sentry
sentry.io › sentry answers › python › does python have a ternary conditional operator?
Does Python Have a Ternary Conditional Operator? | Sentry
The operator checks a condition, and returns x if the condition is true or y if it’s not. Here is an example of a ternary operator use case: ... The ternary operator above checks if x is greater than y, and if it is, assigns the value of x (10) to the variable z. If you’re looking for information on Python ...
🌐
TutorialsPoint
tutorialspoint.com › ternary-operator-in-python
Ternary Operator in Python?
Many programming languages support ternary operator, which basically define a conditional expression. Similarly the ternary operator in python is used to return a value based on the result of a binary condition. It takes binary value(conditi
Find elsewhere
🌐
Finxter
blog.finxter.com › python-one-line-ternary
Python One Line Ternary – Be on the Right Side of Change
The ternary operator returns the value associated to the given key—but only if the key exists. If it doesn’t exist, it returns the default value -1. However, a more Pythonic way to accomplish the same thing in a more readable and more concise way is to use the dictionary.get(key, default) ...
🌐
Scaler
scaler.com › home › topics › python › ternary operator in python
Ternary Operator in Python - Scaler Topics
April 21, 2023 - The ternary operator in Python is a concise way of writing simple if/else statements in a single line. It returns a true or false value by evaluating a boolean condition. It is shorter and more readable than simple if/else statements.
🌐
DataFlair
data-flair.training › blogs › python-ternary-operator
Python Ternary Operator - 5 Ways To Implement Ternary Operators - DataFlair
July 14, 2025 - These are operators that test a condition and based on that, evaluate a value. Python packs a single-line “choose this or that” known as the ternary operator, or conditional expression. The form is value_if_true if condition else value_if_false.
🌐
FavTutor
favtutor.com › blogs › ternary-operator-python
Python Ternary Operator With Examples | FavTutor
Python ternary operator is the most efficient and faster way to perform the simple conditional statement. It returns the specific block of code depending on whether the condition is true or false.
🌐
DataCamp
datacamp.com › tutorial › pythons-ternary-operators-guide
Python's Ternary Operators Guide: Boosting Code Efficiency | DataCamp
May 17, 2024 - The syntax of a ternary operator in Python is value_if_true if condition else value_if_false. It evaluates the condition first; if it's true, it returns value_if_true; otherwise, it returns value_if_false
🌐
Syntaxdb
syntaxdb.com › ref › python › ternary
Ternary Operator in Python - SyntaxDB - Python Syntax Reference
The ternary operator is used to return a value based on the result of a binary condition. It takes in a binary condition as input, which makes it similar to an 'if-else' control flow block. It also, however, returns a value, behaving similar to a function.
🌐
Flexiple
flexiple.com › python › python-ternary
Python ternary operators - How to use them? - Flexiple Tutorials - Flexiple
If the result is true, then it returns the val_true, which in this case is “Yes, you can drive!”. Else it returns val_false, which is Sorry, you can’t drive yet!” · As we now have an understanding of how Python’s ternary operator ...
🌐
Towards Data Science
towardsdatascience.com › home › latest › ternary operators in python
Ternary Operators in Python | Towards Data Science
January 15, 2025 - If it evaluates to True, then x is evaluated and its value will be returned (and assigned to the variable min). Otherwise, y is evaluated and its value is returned (and assigned to the variable min).
🌐
Python Examples
pythonexamples.org › python-ternary-operator
Python Ternary Operator
a, b = 2, 5 #ternary operator print('Python') if a > b else print('Examples') Run the program. As a>b returns False, second print statement is executed.
🌐
Reddit
reddit.com › r/programminglanguages › i think the ternary operator would be more readable if the order was reversed
I think the ternary operator would be more readable if the order was reversed : r/ProgrammingLanguages
June 20, 2024 - In Python, you can do `[b,c][Condition]` (if you don't care about side effects). ... IIRC PHP has a really strange ternary, its unlike any other language. I guess its unfixable unless they break BC ... The ternary operator never needs parens when chaining them. It is unambiguously parseable, has penultimate prcedence, and is RTL associative. This allows: return pred1 ?
🌐
Career Karma
careerkarma.com › blog › python › python ternary operator: a how-to guide
Python Ternary Operator: A How-To Guide | Career Karma
December 1, 2023 - The Python ternary operator is a more efficient way of performing simple if statements. The ternary operator evaluates a condition, then returns a specific value depending on whether that condition is equal to True or False.
🌐
Igmguru
igmguru.com › blog › ternary-operator-in-python
Ternary Operator in Python: Complete Tutorial for Beginners
1 week ago - Now, based on the result only one of the two values is returned. This evaluation mechanism ensures efficiency and clarity. The ternary operator and the traditional if-else statement both perform conditional evaluation. The difference is structure and usage. The ternary operator reduces multiple lines into a single line.