Loading...
「ツール」は右上に移動しました。
利用したサーバー: wtserver3
11いいね 733回再生

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

🖥️ 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 -    • DATA TYPES in Python (Numbers, Strings, Li...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assignmen...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF, ELIF...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Continu...  

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

📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners:    • 🐍 Python 101: Learn Python Basics for Abso...  

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

🧑‍🎓 [COURSE] digital.academy.free.fr/courses

📘 [BOOK] Python for Absolute Beginners: amzn.to/3NvyOWV

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

💌 Weekly Newsletter for Developers: www.getrevue.co/profile/digital_academy

#Python #Tutorial #Beginners #Shorts

***

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

***

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

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

コメント