atomAltera
atomAltera

Reputation: 1791

Python analog of condition? true : false

Is there python function in standard library like

def cond(condition, true, false):
  if condition:
    return true
  return false

x = 20
s = cond(x > 10, "x greater than 10", "x less or equals 10")

Upvotes: 0

Views: 5313

Answers (2)

Makoto
Makoto

Reputation: 106508

Python has a ternary-like operator (it's actually called a conditional expression), which reads like this:

s = "x greater than 10" if x > 10 else "x less or equals 10"

Upvotes: 2

wberry
wberry

Reputation: 19377

Python has a ternary operation but it is done as an "if expression" instead of with question mark and colon.

s = "x greater than 10" if x > 10 else "x less or equals 10"

Upvotes: 9

Related Questions