1

my problem is to use elif in this ternary operator like i want to use 3 conditions in ternary operator

a = 3
if a>0:
    print("is odd" if a %2 !=0  else "is even")

Make a code which return 3 strings one for its odd one for its even and atlast for its zero

mozway
  • 194,879
  • 13
  • 39
  • 75
likith
  • 11
  • 1
  • 1
    Does this answer your question? [Python - One line if-elif-else statement](https://stackoverflow.com/questions/20888693/python-one-line-if-elif-else-statement) – Ignatius Reilly Nov 04 '22 at 06:05

2 Answers2

3
print("atlast" if x == 0 else "even" if x%2 == 0 else "odd")

(Thanks Ignatius Reilly for pointing out that I don't need inner parentheses)

Martin Cook
  • 554
  • 5
  • 15
0

You can check for a condition within a condition. In this case, it will first check whether the value is zero or not. If not zero, then it will move on to check the else condition, which checks the condition inside it for odd or even.

a=3
print("zero" if a==0 else "even" if a%2==0 else "odd")
Darshil Jani
  • 800
  • 2
  • 13