The ternary operator in Python allows us to perform conditional checks and assign values or perform operations on a single line. It is also known as a conditional expression because it evaluates a condition and returns one value if the condition is True and another if it is False.
ex 1:-
n = 5
res = "Even" if n % 2 == 0 else "Odd"
print(res)
ex 2:-
n = -5
res = "Positive" if n > 0 else "Negative" if n < 0 else "Zero"
print(res)
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి