IF Statements in Python – 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!

🖥️ IF Statements in Python – Conditional Statements in Python

In order to write useful programs, we almost always need the ability to test conditions, and change the behaviour of the program, accordingly. Fortunately, conditional statements give us this ability.

IF statements in Python are one of the most commonly used Conditional Statements, in many programming languages. It decides whether certain statements need to be executed – or not.

IF statement in Python checks for a given condition: when the boolean expression is True, the set of code inside the if block is executed. On the other hand: when the expression is False, the set of code inside the if block is skipped over, and not executed at all!

Now, Let’s take a look at its syntax. The boolean expression after the IF statement is called the condition. A few things to keep on mind about IF statements are:

1. The colon (:) is significant and required. It separates the header of the compound statements, from the body.
2. The line after the colon must be properly indented. In Python, it is standard to use four spaces, for indenting.
3. All lines indented the same amount after the colon will be executed, whenever the Boolean expression is True.



○ One-Line if Statements in Python

It is customary to write IF on one line, and indented on the following line. But, if you only have a unique line of code to be executed, rather than multiple lines in the code following the condition, you can place it all in the same line. This is not a rule, but just a common practise amongst coders. Thus, both of the following are functionally equivalent:

     if expression:
         statement
         
     if expression: statement

There can even be more than one statement on the same line, separated by semicolons. But what does it mean? If the expression is True, execute all of the statements. Otherwise, do not execute any of them.

     if expression: statement_1; statement_2; ...; statement_n




○ The Python PASS Statement

Occasionally, it is useful to have a section with no statements: usually as a place keeper, where you will eventually put a block of code, that you have not implemented yet.

Because Python uses indentation instead of delimiters, it is not possible to specify an empty block. If you introduce an IF statement, something has to come after it: either on the same line, or indented on the following line.

To keep the interpreter happy in any situation where a statement is syntactically required, you can use the “pass” statement – which does nothing, except act as a placeholder. Here is an example.

      if True:
          pass
     
     print("After pass")

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