All Topics

Conditionals



All the conditional statements are same as of other languages.

Python supports the usual logical conditions from mathematics:

Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

Python has the following conditional statements:

Use if to specify a block of code to be executed, if a specified condition is true


In this example we use two variables, a and b,
which are used as part of the if statement to test whether b is greater than a.
As a is 33, and b is 200, we know that 200 is greater than 33,
and so we print to screen that "b is greater than a".

Python relies on indentation (whitespace at the beginning of a line) to define scope in the code.
Other programming languages often use curly-brackets for this purpose.

The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".


The else keyword catches anything which isn't caught by the preceding conditions.


There is no switch case supported by python.

Short Hand If ... Else
If you have only one statement to execute, one for if, and one for else, you can put it all on the same line
Can also have multiple else on same line.


Logical operators can also be used to combine the conditional statements
Nested if is supported by python.

Pass statement:-
if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.



Hello