Python if-else

Conditions and If statements in Python
Following usual logical conditions from mathematics are supported by Python:

  • 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

We can use these conditions in many ways and most commonly these can be used in 'if statements and loops'

An "if statement" is always written with the use if keyword.

a = 33

b = 200

if b > a:

     print("b is greater than a")


In the above example we have 2 variables, a and b. The variable a is assigned value 33 and the variable b is assigned the value 200. After assigning the values an if statement is used to check that whether variable b is greater then a or not. As we know that 200 is greater than 33 so it will print on the screen "b is greater than a".

Indentation in Python
In Python we use identations with the use of white spaces to define the scope in the code. Unlike any other programming language, in python we use identations instead of curly brackets that are used in most of the programming languages.
If you will write if statement without any identation then it will raise an error at the time of execution of the code.

a = 33

b = 200

if b > a:

    print("b is greater than a")


Elif
The elif keyword is used in python for fulfillihng the purpose of running various condition at same time and the the conition which is true, then the identation of that condition will execute. The meaning of the keyword "elif " is that "if the previous conditions were not true, then try this condition".

a = 33

b = 33

if b > a:

     print("b is greater than a")

elif a == b:

     print("a and b are equal")


In the above example 'a is equal to b ', so, the if statement is not true as it will be true is b is greater than a, so the elif statement will be executed as it fulfills the conition of equal to. So " a and b are equal" will be printed on the screen.

Else
The else keyword is used in the situation in which the condition specified in the if block is false and we want to do some action to be performed even after that the that is done with the else statement.

a = 200

b = 33

if b > a:

     print("b is greater than a")

elif a == b:

     print("a and b are equal")

else:

      print("a is greater than b"


In this example since a is greater than b, so the if condition will not and also a is not equal to b so elif statement will also not work so then else statement will execute and will print on the screen "a is greate than b".

Short Hand If
In case you have only one statement to be executed then you can put it on the same line as an if statement.
One line if statement can be written as follows:

if a > b: print("a is greater than b")


Short Hand If ... Else
If you have only one line inn the if and the else block to be exected then you can place them in a single line as follows:

print("A") if a > b else print("B")


And
"And" is a logical operator that is used for the purpose of combining the conditional statements:
For e.g. if we want to test that if a is greter than b AND if c is greater than a, then we write the following code for that:

if a > b and c > a:

     print("Both conditions are True")


Or
"Or" is a logical operator that is also used for the purpose of combining the conditional statements:

if a > b or a > c:

     print("At least one of the conditions is True"