Python if, if_else, if_elif_else, Shorthand if else, Indentation, OR, AND

Python Statement:

Python supports the standard logical conditions from mathematics:

Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
These conditions are often employed in many ways in which, most typically in "if statements" and loops.
An "if statement" is written by mistreatment the if keyword.

If statement in python:

In this example, we have a tendency to use 2 variables, a and b, that are used as a part of the if statement to check whether or not b is larger than a. As a is 33, and b is two hundred, we all know that two hundred larger than thirty-three, and then we have a tendency to print to screen that “b is larger than a”.

Example:

a = 33
b = 200
if b > a:
    print("b is larger than a")

Output

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe
C: / Users / Pramod / PycharmProjects / pythonProject / main.py
b is larger than a

Process finished with exit code 0

What is the else statement in Python?

The else keyword catches something that is not caught by the preceding conditions.

In this example a is larger than b, therefore the initial condition isn’t true, In conjointly the elif condition isn’t true, thus we have a tendency to move to the else condition and print to screen that “a is larger than b”.

Example-1

a = 200
b = 33
if b > a:
    print("b is larger than a")
elif a == b:
    print("a and b ar equal")
else:
    print("a is larger than b")

Output

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
a is larger than b

Process finished with exit code 0

You can even have else while not the elif:

Example-2

a = 200
b = 33
if b > a:
    print("b is larger than a")
else:
    print("b isn'tlarger than a")

Output

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
b isn'tlarger than a

Process finished with exit code 0

What is Elif in Python?

The elif keyword is pythons manner of claiming “if the previous conditions weren’t true, then do this condition”.

In this example a is capable b, therefore the initial condition isn’t true, however, the elif condition is true, thus we have a tendency to print to screen that “a and b are equal”.

Example-3

a = 33
b = 33
if b > a:
    print("b is larger than a")
elif a == b:
    print("a and b are equal")

Output

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
a and b are equal

Process finished with exit code 0

What is an indentation?

Python depends on indentation, and mistreatment of whitespace, to outline scope within the code. different programming languages usually use curly-brackets for this purpose.

Example-4

If statement, while not indentation (will raise AN error):

a = 33
b = 200
if b > a:
    print("b is larger than a") # you may get a slip-up

Output

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
b is larger than a

Process finished with exit code 0

Python Shorthand If Else

If you have got only 1 statement to execute, you’ll be able to place it on a similar line because of the if statement.

Shorthand if Statement:

a = 200
b = 33
if a > b: print("a is larger than b")

ShortHand If  Else Statement:

If you have got only 1 statement to execute, one for if, and one for else, you’ll be able to place it all on a similar line:

Example-1:

One line if-else statement:

a = 200
b = 33
print("A") if a > b else print("B")

Output

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
A

Process finished with exit code 0

You can even have multiple else statements on a similar line:

Example-2

One line if else statement, with three conditions:

a = 200
b = 33
print("A") if a > b else print("=") if a == b else print("B"

Operator in ShortHand

The and keyword could be a logical operator, and is employed to mix conditional statements:

AND Operators

Test if a is larger than b, AND if c is larger than a:

a = 200
b = 33
c=300
if a > b and c > a:
    print("Both conditions are True")

OR Operator

The or keyword could be a logical operator, and is employed to mix conditional statements:

Test if a is larger than b, OR if a is larger than c:

a = 200
b = 33
c=300
if a > b or a > c:
    print("At least one in every of the conditions is True")

Recommended Post:

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment