if else Conditional Operator in Python

Python | if else conditional operator: Here, we are going to learn about the if else conditional operator with its usages, syntax, and examples. By IncludeHelp Last updated : December 18, 2023

Python if else Conditional Operator

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.

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.

Python if else Conditional Operator: 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 Tutorial


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.