Python- Conditional Operator to Compare Two Numbers

Use of conditional operator to compare two numbers

# Program to find the compare two numbers using conditional operator
# Author: Mukesh N Tekwani
# Languages such as C, C++, C# and Java support the conditional operator 
# represented by the pair ? : 
# This operator is a compact replacement for if...then statement
# Python supports conditional operator in another way, as shown in this example
# Syntax of this operator is: a if condition else b
# First, the condition is evaluated, then exactly one of the two statements 
# either a or b is evaluated and returned based on the 
# Boolean value of condition. 
# If condition evaluates to True, then a is evaluated and returned but b is ignored # If condition evaluates to false, then b is evaluated and returned but a is ignored.
# This allows short-circuiting because when condition is true only a is evaluated 
# and b is not evaluated at all, but when condition is false only b is evaluated 
# and a is not evaluated at all.
# Here is a way to compare the two numbers using conditional operator in 
a = 433 
b  = 268

print("a is larger") if a > b  else print ("b is larger")

%d bloggers like this: