Python if else condition

Python if else

The if-else statement in a programming language lets a computer make some decisions on its own. An important reason why we use these statements is due to the need of execution of code only if a specific condition is satisfied.
The code only runs when the condition is satisfied, if the condition is not satisfied the compiler skips over the whole bit of code. To understand further, let’s have a look on the syntax of if-else condition statements-

Syntax
if condition:
    Statement(s)
else
    Statement(s)

Important things to keep in mind while writing if-else statements:

  • While writing this code, one thing you should always keep in mind that the statements that are written under the if statement will only be counted if there is some space left behind the statement, this is called indentation and Python relies on indentation extensively.
  • The indentation should be left before every statement following the if statement.
  • It should be noted that Python interpreter will not give you an error if the condition is not satisfied, it will just not execute it.
  • Python uses 0 for a False condition and 1 for a True one.
  • In the form of different conditions, Boolean logical operators are used, and we have learned that Boolean logical operators always output either a 1 or a 0. The interpreter will only execute the code written under if statement only if there is a 1 as an output.
  • The statements under the else statement will only be executed if the conditions of the if statement remain unsatisfied.

Let us have a look on an example Python if else statements:

Example

Python if else statement

In the above code the condition is satisfied because y is greater than x. Also, let’s see how removal of indentation effects the code execution-

Example

Python code not indented

Example

Python single statement

The elif statement

elif is short for else-if and it means that “if the last condition wasn’t satisfied then try this condition”. This statement just gives the computer some more options to choose.

Let us have an example –

Example

Python elif statement

Nested Statements

We can also write many conditional statements inside an if-statement, this is called Nesting and the statements are called Nested Statements.

The Nesting is identified by indentation and you have to be really careful when writing the nested statements. Let’s have an example –

Example

Python nested if else statement

Empty if statement

At many instances, you want to leave an if statement empty but you cannot leave an if statement, that’s why you have to use the keyword pass inside the statement like this

Example

Python pass

Logical Operators

There are two logical operator keywords, i.e., and and or that can be used for an if statement to make decisions more efficiently. These logical operators are very useful, let’s see how they can be used

AND

The interpreter only executes the if statement if and only if both the conditions are satisfied.

Example

Python logical and statement

OR

The interpreter executes the if statement if either of the condition is true.

Example

Python logical OR statement

Tutorials for all brains!