Program to demonstrate ternary operators in Python
marks = input('Enter the marks: ')
print("The result is Pass" if int(marks)>=35 else "The result is Fail")
condition = True
print("This condition is true!") if condition else print("This condition is false!")
# The if statement in one line! (Ternary operator)
smaller, larger = 7, 50
# Copy value of a in min if a < b else copy b
min = smaller if smaller < larger else larger
print(min)
#equivalent to the (condition)?(if true) :(if false) in javascript
MALE = True
FEMALE = False
# (if_test_is_false, if_test_is_true)[test]
gender = ("female", "male")[MALE]
print("You are a ", "male")
# Output: You are a male
# (if_test_is_false, if_test_is_true)[test]
gender = ("female", "male")[FEMALE]
print("You are a ", "female")
# Output: You are a female
condition = True
# (if_test_is_false, if_test_is_true)[test]
print(2 if condition else 1/0)
#Output is 2
condition = False
# (if_test_is_false, if_test_is_true)[test]
print(2 if condition else 5)
#Output is 5