Python If Else

In your daily life, you perform a task according to certain condition. For example, you goes to school trip with your friends, if your parent allowed. if they doesn't, then you doesn't go.

Conditional Expression

Similarly these conditions are also used in programming, that tells what to do next to a program if the given conditions doesn't meet. If Elif and Else keywords are said to be conditional expressions in python. It allows the program to make decisions.

Mostly, Comparison operators are used for giving conditions.

  •  ==  : Equal to.
  •  !=  : Not equal to.
  •  <  : Less than.
  •  <=  : Less than or equal to.
  •  >  : Grater than.
  •  >=  : Grater than or equal to.


Note :- Python always take an indentation or tab, if you want to write a code inside 'if statement' you have to insert a tab at the beginning of the code line.


Elif

In python, Elif stands for else if. It is always written after 'if statement'. You can insert any number of 'elif statement' in a program.



Else

'Else statement' is written at the last. Else is a optional statement because it is only executed if all the condition of other statements fail.



Short hand If Else

It means you can write, 'if else statements' within a line.



Using Logical Operators

You can also use a logical operator for giving conditions.

  •  and  : Return true if both conditions are true.

  •  or  : Return true if at least on condition is true.


Nested If

Writing 'if statements' inside a 'if statement' is called nested if statements.



Pass Statements

A conditional statements must not be empty, because empty statement throws an error. Hence, 'pass statement' is used to avoid these errors.




Try to create a program, Which find greatest of two numbers entered by the user.

Comments