Conditional expressions in Python
Ternary operators, also known as conditional expressions, are operators that evaluate based on whether a condition is true or false. It was added to Python in the 2.5th edition.
This only allows testing a condition in a single line, replacing multi-codes if-and makes the code compact.
Syntax:
[on_true] if [expression] else [on_false]
Program to Check number is positive or Not using conditional Operator
n=int(input("Enter the number:-"))
print("Positive number"if n>0 else "Negative number")
Output:
shivam@coder:~/Desktop$ python Pos.py
Enter the number:--2
Negative number
Comments
Post a Comment
If you have any query, please let us know