Python - Condition Statement
conditional statements, also known as "if" statements, are used to make decisions based on certain conditions. These statements allow you to control the flow of your program by executing specific blocks of code only when certain conditions are met. Python supports several types of conditional statements, including "if," "elif" (short for "else if"), and "else."
- if statement: The if statement is the most basic form of a conditional statement. It tests a given condition, and if the condition is true, the block of code indented under the if statement is executed. If the condition is false, the block of code is skipped, and the program continues with the next line of code after the if block.
- elif statement: The elif statement is used when you have multiple conditions to check. It is short for "else if" and is used after an initial if statement. If the first condition is false, the program checks the next elif condition. If that condition is true, the block of code indented under the elif statement is executed. If the condition is false, the program continues checking any subsequent elif statements. You can have multiple elif statements in a single conditional block.
- else statement: The else statement is used when you want to provide a default block of code that should be executed if none of the previous conditions are met. It is used at the end of a conditional block, after any if and elif statements. If none of the preceding conditions are true, the block of code indented under the else statement is executed.
Conditional statements can be used with various types of expressions, such as comparisons (e.g., x < y), logical operations (e.g., x and y), or membership tests (e.g., x in y). They can also be combined using parentheses and logical operators (e.g., x < y and (z > w or a in b)).
Examples
Following is standards format of Python if statement. Be careful that you should not miss ':' at the end of the condition and also be careful that you are using proper indentation. Check the examples in this page.
NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x
NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.
< Type 1 > --------- see Example 1, Example 2
if condition :
indentedStatementBlock
< Type 2 >
if condition :
indentedStatementBlock
else :
indentedStatementBlock
< Type 3 > --------- see Example 3
if condition1 :
indentedStatementBlock
elif condition2 :
indentedStatementBlock
elif condition3 :
indentedStatementBlock
elif condition4 :
indentedStatementBlock
elif condition5 :
indentedStatementBlock
else :
indentedStatementBlock
|
For the if statement in any language, what you need to know first is how they represent 'Comparison' and 'Logical Operation/Binary Operaton'.
Followings are the list of comparison operators in if statement
== |
Equal |
|
!= |
Not Equal |
|
<> |
Not Equal |
|
> |
Greater than |
|
< |
Less than |
|
>= |
Greater than or equal |
|
<= |
Less than or equal |
|
Followings are the list of logical operators used in if statement
and |
Logical AND |
Example 3 |
or |
Logical OR |
|
not |
Logical NOT |
|
Followings are some examples for if statement.
< Example 1 >
check = 0.4
if check > 0.5 :
print("Run A...")
print("Run B...")
print("Run C...")
Result : -----------------------------------
Run B...
Run C...
< Example 2 >
check = 0.4
if check > 0.5 :
print("Run A...")
print("Run B...")
print("Run C...")
Result : -----------------------------------
Run C...
< Example 3 >
score = 75
if score >= 90 :
print("Grade A")
elif (score < 90) and (score >= 80) :
print("Grade B")
elif (score < 80) and (score >= 70) :
print("Grade C")
elif (score < 70) and (score >= 60) :
print("Grade D")
else :
print("Grade F")
Result : -----------------------------------
Grade C
|
|
|