Conditional Statements In Python
Last updated
Last updated
What are Conditional Statements?
We have often used conditional statements in our daily life, such as :
if it rains ,i will go to office.
Syntax:
In [ ]:
Above Figure Shows , Actions or decisions based on the Condition. If the condition is True, i will not go to office. Here are some Examples for if statement:
How to Find if a number is even?
In [2]:
Python indentation is a way of telling python intrepreter that the group of statements/code/instrtuctions belong to a particular block.
In more simpler terms , indentation is nothing but a bunch of spaces or tabs signifying a particular line of code belongs to a same group.
Now You saw what will happen if the condition is true in above case. All the code with same indentation will run. But what will happen if the condition is False?
In [3]:
So, Here the codes with same indentation did not run, it shows that if condition is True, Body of if will run. Body of if (Codes written under if statement by using indentation.), if Condition is False, Body of if will not run.
Note:We generally use Tab to define indentations. One Tab means 4 spaces.
The if-else statement is similar to if statement except the fact that, it also provides the block of the code for the false case of the condition to be checked.
If the condition provided in the if statement is false, then the else statement will be executed.
For Example : . if coffee house is open i'll go there else i will be at home.
In [ ]:
To find if a number is even or odd:
In [2]:
In [3]:
The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon what condition is True.
Syntax:
In [ ]:
So , elif - statement , in more simpler terms runs body of first if or elif statement with true condition and if any of the condition is not true , body of else executes.
For Example:
Program to find grade of a student based on marks:
In [1]:
There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if construct.
In more simpler terms, Nested if is actually if statement under an if statement.
Syntax:
In [ ]:
Program to find Largest Number among three numbers:
In [1]:
Note:Run above code in your editor and change values to see , how this code is working.
1.A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years. Ask user for their salary and year of service and print the net bonus amount.
2.A school has following rules for grading system: a. Below 25 - F b. 25 to 45 - E c. 45 to 50 - D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A Ask user to enter marks and print the corresponding grade.
3.A 4 digit number is entered through keyboard. Write a program to print a new number with digits reversed as of orignal one. E.g.- INPUT : 1234 OUTPUT : 4321 INPUT : 5982 OUTPUT : 2895
4.Write a program to check if a year is leap year or not. If a year is divisible by 4 then it is leap year but if the year is century year like 2000, 1900, 2100 then it must be divisible by 400.