filmov
tv
Ternary Operators in Python (One-Line IF) - Conditional Statements - Python Tutorial for Beginners

Показать описание
🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ Ternary Operators and Conditional Expressions in Python (One-Line IF Statement)
Python supports one additional decision-making entity, called: a Conditional Expression, or even Ternary Operator. This is quite different from the IF statement forms listed earlier, because it is not a control structure that directs the flow of execution of your program. It acts more like an operator, that defines an expression.
In its simplest form, the syntax of the Conditional Expression is as follows:
expression_1 if conditional_expression else expression_2
In the example, this conditional expression is evaluated first. If it is True, the expression evaluates to expression 1. And if it is False, then it evaluates to the expression 2. Notice the non-obvious order: the middle expression is evaluated first, and based on that result: one of the expressions or values on the ends is returned.
As you may have guessed, a common use of the conditional expression is for variable assignment. For instance, Let's suppose that you want to find the larger of two numbers. Of course, there is a built-in function max() that does just this - and more - that you could use. But, suppose you want to write your own code, from scratch. Then, you could use a standard IF-ELSE statement this way, or even a shorter and one-line way.
# Standard
if a == b:
m = a
else:
m = b
# One-Line
if a == b: m = a
else: m = b
But, you would agree, that a conditional expression is even shorter, and arguably more readable as well:
m = a if a == b else b
Let's play this video, stick around and watch until the end of this video! 👍🏻
- Digital Academy™ 🎓
***
☞ WATCH NEXT:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
🖥️ Ternary Operators and Conditional Expressions in Python (One-Line IF Statement)
Python supports one additional decision-making entity, called: a Conditional Expression, or even Ternary Operator. This is quite different from the IF statement forms listed earlier, because it is not a control structure that directs the flow of execution of your program. It acts more like an operator, that defines an expression.
In its simplest form, the syntax of the Conditional Expression is as follows:
expression_1 if conditional_expression else expression_2
In the example, this conditional expression is evaluated first. If it is True, the expression evaluates to expression 1. And if it is False, then it evaluates to the expression 2. Notice the non-obvious order: the middle expression is evaluated first, and based on that result: one of the expressions or values on the ends is returned.
As you may have guessed, a common use of the conditional expression is for variable assignment. For instance, Let's suppose that you want to find the larger of two numbers. Of course, there is a built-in function max() that does just this - and more - that you could use. But, suppose you want to write your own code, from scratch. Then, you could use a standard IF-ELSE statement this way, or even a shorter and one-line way.
# Standard
if a == b:
m = a
else:
m = b
# One-Line
if a == b: m = a
else: m = b
But, you would agree, that a conditional expression is even shorter, and arguably more readable as well:
m = a if a == b else b
Let's play this video, stick around and watch until the end of this video! 👍🏻
- Digital Academy™ 🎓
***
☞ WATCH NEXT:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
Комментарии