Home »
Python
if else Conditional Operator in Python
Last Updated : April 21, 2025
Just like other programming languages, Python also provides the feature to evaluate conditional statements using the conditional operator.
In this tutorial, we will see the if else conational operator.
Python if else Conditional Operator
if else conditional operator is used to evaluate/get either value/statement (from the given two values/statements) depending on the result of the given Boolean expression.
Syntax
Below is the syntax to write if else conditional statement:
value1 if expression else value2
Here,
- value1 – represents the value for the conditional expression if it is True.
- expression – represents the condition that must be evaluated to a Boolean (i.e. we can say it is a condition)
- value2 – represents the value for the conditional expression if it is False.
In simple words, we can say – if the expression is True, value1 will be returned and if it is False, value2 will be returned.
Examples
Example 1: Printing the largest value among two values
# find the largest Value
x = 20
y = 10
# if else conditional operator
largest = x if x>y else y
# printing the values
print("x: ", x)
print("y: ", y)
print("largest: ", largest)
print()
x = 10
y = 20
# if else conditional operator
largest = x if x>y else y
# printing the values
print("x: ", x)
print("y: ", y)
print("largest: ", largest)
print()
x = 10
y = 10
# if else conditional operator
largest = x if x>y else y
# printing the values
print("x: ", x)
print("y: ", y)
print("largest: ", largest)
print()
Output
x: 20
y: 10
largest: 20
x: 10
y: 20
largest: 20
x: 10
y: 10
largest: 10
Example 2: Printing the largest value among three values
It's an example of nested if else conditional operator.
# find the largest Value
x = 10
y = 20
z = 30
# if else conditional operator
largest = x if (x>y and x>z) else (y if(y>x and x>z) else z)
# printing the values
print("x: ", x)
print("y: ", y)
print("z: ", z)
print("largest: ", largest)
print()
x = 10
y = 30
z = 20
# if else conditional operator
largest = x if (x>y and x>z) else (y if(y>x and y>z) else z)
# printing the values
print("x: ", x)
print("y: ", y)
print("z: ", z)
print("largest: ", largest)
print()
x = 30
y = 20
z = 10
# if else conditional operator
largest = x if (x>y and x>z) else (y if(y>x and y>z) else z)
# printing the values
print("x: ", x)
print("y: ", y)
print("z: ", z)
print("largest: ", largest)
print()
x = 10
y = 10
z = 10
# if else conditional operator
largest = x if (x>y and x>z) else (y if(y>x and y>z) else z)
# printing the values
print("x: ", x)
print("y: ", y)
print("z: ", z)
print("largest: ", largest)
print()
Output
x: 10
y: 20
z: 30
largest: 30
x: 10
y: 30
z: 20
largest: 30
x: 30
y: 20
z: 10
largest: 30
x: 10
y: 10
z: 10
largest: 10
Python if-else Conditional Operator Exercise
Select the correct option to complete each statement about the if-else conditional operator in Python.
- The if-else statement in Python is used to ___ a condition.
- The syntax of an if-else statement in Python starts with the keyword ___.
- If the condition in an if-else statement evaluates to ___, the else block will be executed.
Advertisement
Advertisement