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.
Submitted by IncludeHelp, on May 30, 2020

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:

    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.

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.