Python if Statement

In programming sometime we have to make decisions either to execute or skip statement or set of statement depending on particular input or results. Almost every programming language provide solution to handle this situation. In python programming language this is done with the help of if statement. This is also called selection statement.

if is a conditional statements in python that uses relational expresion to skip or execute statements. This relation expression returns either TRUE or FALSE. If it retur TRUE then statements will execute otherwise statement will be skipped and control transfer to the next instructions.

There are also other control statements in python taht perform the same action depending on the decision / result. But most commonly we use if or if else statement.

if statement syntax in python:

if statement has the simple syntax in python programming.

if expression:

     statement

Python if:

In syntax if is keyword specified by the language and written in lowercase letters.

Expression:

Expression is the relational expression which will result in TRUE or FALSE. It also may be composite relational expression having logical operator (AND, OR, NOT). Expression is ending with colon operator, it is also part of syntax. It is also called if condition in python.

Statements:

There may be single or multiple statements after expression in python, that will be skipped if the result of expression will  FALSE .

if multiple conditions:

if statement may have multiple conditions or relational expressions. These relational expression are combined or collectively evaluated using logical operators such as AND, OR operators. Another logical operator can also be used in relational expression that is called NOT operator.

Python if statement Multiple Conditions Examples:

In python examples of multiple if statements are as given.

Example AND Operator:

# Use of AND Logical operator

a = 5
b = 10
if a < 10 and b < 20:
    print("Both Less by Double")
else:
    print("Both Not Less by Double")

Output:

Both Less by Double

Example OR Operator:

# Use of OR Logical operator

x = 5
y = 30
if x < 10 or y < 20:
    print("At Least One of them is Less by Double")
else:
    print("Both Not Less by Double")

Output:

At Least One of them is Less by Double

Example NOT Operator:

# Use of NOT Logical operator

x = 5
if not x < 10:
    print("X is not Less by its Double")
else:
    print("X Less by Double")

Nested if in Python:

In python we can also put if conditional statement in another if statement. In this case first outter if is evaluated and then inner if will be check. If outer if condition become FALSE the the remaining inner blocks if statement will not check. Almost every high level programming provide the feature of nesting control structures.

There is no limit of nesting if statement, but it is recomended to nest if statement upto only two levels for better performance. The first come if block is called outer if and the next to it is called inner if statement.

Python Inline if:

Python inline if is a shorthand and quick solution of if else statement. This feature is particularly provided by the python programming language. We can write python one line if using this feature. This is also called python inline if else statement.

Syntax:

Statement  if  expression  else  statement

Example:

# Use of inline if statement

x = False
print("HELLO") if x else print("WORLD")

Output:

WORLD

if Condition in Python:

In python any valid relational expression can be used as condition. This condition may be simple or compund. In case of compound condition there will be logical operators used in condition. In python we can also use boolean keywords TRUE or FALSE.

 
Comments
Login to TRACK of Comments.