if-else - I



👈Previous Topic


Hello Folks....!!!! 

Today we are going to look at conditional statements in Python. 

The conditional statements are used for decision making. If yes, one specified path will be followed, if not, other path will be followed. 

I hope you all know what conditional statements other languages have. They are "if-else" and "switch case". But Python does not have switch case.

General syntax for writing conditional and looping statements :

  • These statements are not enclosed within curly braces as in C or C++. The indentation matters here. For example : in C an if statement is written as given below :

in C

in Python , it is much simpler.

Conditional statements and looping

  • The conditions are not enclosed within brackets. They are written after a space continuously. Refer the example above. You can write if a==10, it is not mandatory to write if(a==10).
  • All  the statements that have to be executed on certain conditions must be in the correct indentation level. Any statement different from the indentation will be considered as instruction outside the loop/conditional statement. For example :

Python conditional statement and looping

Here there are three print() statements. Look at their indentation. The first two print() are slightly indented inside. This specifies that they are the statements that have to be executed if the condition is true. The last print() is out of if-block. So it is not restricted by the condition.

if-else statements :

  • These are used to take yes or no decisions.
  • The syntax is - 
             if condition:
                    statement1
                    statement2
          else :
                    statement1
                    statement2

Let us learn with some examples:
Example 1:
Let us get an integer input from the user. If input is less than 10, we have to print its square. Otherwise let us print some stars. Let us try to code it now :

Python- if else

Python-if else

So here, when you input 3, which is below 10 your if part works and you get its square 9 as output. When you type 44, your else part works.

We all know that if-else can contain else-if block in between. Here we don't write else if, we write elif. 

Example 2: 
Now let us slightly modify the above example. Let us introduce more conditions :
  • if num is less than 10, output is its square.
  • if num lies between 10 and 30, output is its cube.
  • if num lies between 30 and 60, output is its square+num.
  • Otherwise '*****no matches*****' will be printed.
Let us code now :

Python-if else if

Here, the relational operators such as <(less than) and  >(greater than) are used to check conditions. You can also check: <=,>=,==. To combine more than 1 condition, we use the logical operators and,or,not. Here we need to check whether the number lies in specific range. So we use and operator. Here there are no symbols used for these logical operator. You can write 'and', 'or', 'not'.

Python allows nesting of these relational operators. Instead of joining the conditions with 'and' one can write as given below. The above example can be modified as :

Python-if-else if

if-else and data-structures :

Let us look at an example using lists :
Example 3:
Check whether the number in given index is less than or greater than 5 :

Python-if else

Example 4:
Check whether the given string is in upper or lower case :

Python-if else

Today I have shown you very basic and simple examples. Tomorrow we will continue with some more complex examples. 



For today try the exercise below:

1. Get an integer input from the user, check divisibility by 2,3,5 and print by which of these numbers the input is divisible. Note that you have to print the two numbers, if it is divisible by any two of these numbers and all the three, if it is divisible by all the three. 

2. S1="LET us LEARN python."

Make a list out of this string. Check every word's case. If it is upper change it to lower, if it is lower change it to upper.

Next Topic👉

Comments