Ternary Operators in Python (One-Line IF) – Conditional Statements in Python

🎓 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:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒 Shopping and Discounts: http://digital.academy.free.fr/store/

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap