Python If-Else

  • The if-else statement is used in python for decision making.

If Statements:

Syntax

if(condition):
      statement(s)

If Examples:

01. Example

x=2

if x>10:
    print("Number is greater than 10.")
print('This will always print.')

Output:

This will always print.

02. Example

x=20

if x>10:
    print("Number is greater than 10.")
print('This will always print.')

Output:

Number is greater than 10.
This will always print.

If-Else Statements:

Syntax

if(condition)):
    statement(s)
        
else:
    statement(s)

If-else Examples:

01. Example

N = int(input("Enter the value: "))

if N>0:
    print("Number is positive.")
else:
    print("Number is not positive.")

Output:

Enter the value: 15
Number is positive.

02. Example

x=-20

if x>0:
    print("Number is positive")
else:
    print("Number is not positive.")

Output:

Number is not positive.

03. Example

string='Lets learn Python With Zeroones'

if 'Zeroones' in string:
    print('Element Founded In String.')
else:
    print('Element Not Founded In String.')

Output:

Element Founded In String.

If-elif-else Statements:

Syntax

if(condition)):
    statement(s)
    
elif(condition):
    statement(s)
    
else:
    statement(s)

If-elif-else Examples:

01. Example

x=0

if x>0:
    print("Number is positive")
elif x==0:
    print("Number is zero!")
else:
    print("Number is not positive")

Output:

Number is zero.

02. Example

x=0

if x>0:
    print("Number is positive.")
elif x==0:
    print("Number is zero.")
else:
    print("Number is not positive.")

Output:

Number is zero.

03. Example

x=0

if x>0:
    print("Number is positive")
elif x==0:
    print("Number is zero.")
else:
    print("Number is not positive")

Output:

Number is zero.

Nested If-else Statements:

As “if-else” statement is a decision making statement in programming language. By apply if-else program execution flow can change. But for the best decision sometime “nested if-else” statements are used. “Nested if else statements’ are nothing but just “if statements” inside “if/if-else/if-elif-else” statements to make better decisions.

Syntax

if(condition)):
    statement(s)
    
    if(condition):
        statement(s)
    else:
        statement(s)
        
else:
    statement(s)

Nested If-else Examples:

01. Example

x=10.5

if x>0:
    if x==0:
        print("Number is zero.")
    else:
        print("Number is positive.")
else:
    print("Number is negative.")

Output:

Number is positive.

02. Example

x=-10.5

if x>0:
    if x==0:
        print("Number is zero.")
    else:
        print("Number is positive.")
else:
    print("Number is negative.")

Output:

Number is negative.

If Statements With Pass Keyword:

We cant keep the loops empty. But if we want to use loops and dont want anything in return, we use pass keyword. It helps in avoiding the error.

Example

a=54
b=73

if(a>b):
    pass

If Statements With And Keyword:

The Python keyword and is used to determine whether both the left and right operands are true or false. If both operands are true, the result will be true. If one is false, the result will be false.

Example

n=244

if n%2==0 and n%4==0:
    print("Both the conditions are satisfied.")

Output:

Both the conditions are satisfied.

If Statements With Or Keyword:

The Python “or keyword” is used to determine, if at least one of the operands is truth. An “or statement” returns the first operand, if it is true and otherwise returns the second operand.

Example

n=244

if (n==0 or n==244):
    print("Or Statement Working!")

Output:

Or Statement Working!

If-else One Line Statements:

Writing “if-else” in single line is more easy and concise code description as it allows you to write all the code in one line while maintaining the readability and quality of the code.

But do not overuse it for long statements as it also creates some confusion and conflict. The most important thing is to use it very wisely.

Example

a = 2000
b = 3

print("a is greater than b.") if a > b else print("b is greater than a.")

Output:

a is greater than b.

Leave a comment