Python Conditions and If statements |Short Hand If … Else | Indentation | Elif condition | else | One line if statement | OR | AND

Python Conditions and If statements |Short Hand If … Else | Indentation | Elif condition | else | One line if statement | OR | AND

Python Conditions and If statements

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 oftenemployed inmanyways in whichmost typically in “if statements” and loops.

An “if statement” is written by mistreatment the if keyword.

Example

If statement:

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

In this example we have a tendency to use 2 variables, a and b, thatar used as a part of the if statement to checkwhether or not b is larger than a. As a is 33, and b is two hundredwe all know that two hundredis larger than thirty threeand thenwe have a tendency to print to screen that “b is larger than a”.

Indentation

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

Example
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

Elif

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

Example
a = 33
b = 33
if b > a:
print(“b is larger than a”)
elif a == b:
print(“a and b ar equal”)

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

Else

The else keyword catches somethingthatis not caught by the preceding conditions.

Example
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”)

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

You can even haveAN else while not the elif:

Example
a = 200
b = 33
if b > a:
print(“b is larger than a”)
else:
print(“b isn’tlarger than a”)

Short Hand If
If you have gotonly 1 statement to execute, you’ll be able toplace it on a similar line because the if statement.

Example

One line if statement:

if a > b: print(“a is larger than b”)

Short Hand If … Else

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

Example
One line if else statement:

print(“A”) if a > b else print(“B”)

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

Example
One line if else statement, with three conditions:

print(“A”) if a > b else print(“=”) if a == b else print(“B”)

And

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

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

if a > b and c > a:
print(“Both conditions ar True”)

Or

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

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

if a > b or a > c:
print(“At least one in every of the conditions is True”)

Python Conditions and If statements |Short Hand If … Else | Indentation | Elif condition | else | One line if statement | OR | AND