Python-IF command
Good Morning Boys!!
Welcome Back..
In our last class we studied RELATIONAL OPERATORS.
You must have read in my previous blog that the Relational Operators are used for comparison.
Today we will use these Relational Operators in IF Statements.
▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺
| Learning Outcomes
By the end of this period you will be able to
|
Let's start...
Till now our programs were running in SEQUENTIAL order only.
But sometimes a programmer is caught in a situation when she/he has two sets of statements in hand, to be executed SELECTIVELY by taking a DECISION.
This is solved by the 'IF' statement in Python.
The word 'IF' is used by us in our English Language to state the conditions.
e.g. If it is not raining outside then you can go to play otherwise not.
Here, going to play and going not to play are to different actions. Ans only one of them would be selected on the basis of the condition mentioned after the word IF.
Similarly, we can use IF in our Python to create a condition and execute the selected set of statements.
As your book says:
A simple, 'if ' statement is used to execute the specific block of code based on a condition. If the condition is 'true' the True statement block will be executed and if condition is 'false' the False statement block will be executed.
▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺
Let's learn it practically...
Let's learn it practically...
The syntax of if statement is -
if (<expression>):
Statement(s)
Here,
<expression> is a condition evaluated in Boolean value either True or False.
Please note the use of colon (:) after the closing bracket of expression.
Then in the next line you will find that your STATEMENT begins after a little gap from the beginning. This is called INDENTATION.
▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺
Now look at the following Python code window. The condition in this code is created using GREATER THAN operator ( > ).
Now look at the following Python code window. The condition in this code is created using GREATER THAN operator ( > ).
In this program, A = 50 and B = 10. Our IF statement has created a condition. Due to this condition the sentence - "A is greater" would be printed on the screen ONLY if A is bigger then B.
As we all can see A is obviously greater than B in this case, so the output should be - "A is greater".
Let's see the output.
So, boys as you can see the output has come as expected.
▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺
Now, lets try to print something if A is not greater than B. That means either B is greater than A or equal to A.
That means we are talking about the statements, which should be executed when an IF Statement is FALSE.
Please see this code to understand.
Please note the two changes in the code:
- Now A's value is 5 but B is still at 10
- there is an entry of two new lines of code.
- first is -->> else: This is used for the beginning of the alternative path, i.e. the statements to be executed when the IF Condition is FALSE.
Please Note:- the use of colon(:) with else. - second is -->> print("B is greater than or equal to A") This is the statement to be executed by when the IF condition is FALSE.
Now, let's see the output.
As now B is greater than A, so our IF condition (i.e. if (A>B): ) now stands FALSE.
Therefore the alternative statement has been executed.
▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺
Alright boys...
Do you remember the following code:
Yes boys... this was done by us in our last to class, just to revise our class-9's concepts.
Now, please read the same code but after a litter editing in it.
Could you please try to explain that what will happen when this code would be executed.
Yes boys.. you are right.
In addition to what all was happening in this program in last class, this code is comparing the percentage of the student with 33.
If the Percentage is greater than or equal to 33 then following message would be printed.
- "You are PASS
Otherwise the following message would be printed.
- "Sorry! you need to repeat the same class again"
Let's see the output:
According the the marks entered by the student, the percentage has come - 51.6. This is greater than 33. Therefore the first print statement is executed i.e. "You are PASS".
Let's see another output of the same code:
Please note that the marks entered this time were not very high. Therefore the percentage has gone below 33. Therefore the alternative print statement is executed, i.e. "Sorry! you need to repeat the same class again".
▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺▬ ☻▬ ☻▬☺
I hope by now you must have understood the concept.
So take the following questions as Homework and do the practice yourself.
Q1. Write a program to accept the age of a person and then tell whether she/he can vote in Elections in India or Not.
Q2. Write a program to accept the value of Basic Salary of an employee and then show the following:
# HRA = 24% of Basic Salary
# DA = 9% of Basic Salary
# PF = 12% of Basic Salary
# Gross Salary = Basic + HRA + DA - PF
# Income Tax = 10% of Gross Salary
# Net Salary = Gross Salary - Income Tax
# If the INCOME TAX is lower than 1500 then she/he should get the following message
Otherwise the she/he should get the following message:
|
That's all for today.
In our next class, we will do some more programs based on IF Condition.
Thank you boys
God Bless You
Computer Science Departement
(Middle School)
Present ma'am
ReplyDeleteMa'am what about the meet class for today?
ReplyDeletePragyan , Today we conclude the introduction. Next class will be application based so have planned Google meet for that. Be Ready
DeleteGood morning maam neev kumar present
ReplyDeletePresent ,mam
ReplyDeleteGood morning boys. Next will be Google meet class based teaching. Pls revise relational operators and IF construct. Will give questions to solve during the class
ReplyDeleteThis comment has been removed by the author.
Deletepresent ma'am
ReplyDeleteGood morning ma'am.
ReplyDeleteGautam.
Present ma'am
ReplyDeletePresent maam
ReplyDeletePresent ma'am
ReplyDeleteMa'am Jossam is Present
ReplyDeletePresent ,mam
ReplyDeletePresent ma'am
ReplyDeleteMatthew Jayakumar 10-C
Jaigopal Longani present
ReplyDeletePresent Ma'am
ReplyDeleteArchbill Xalxo 10-C
KRISH Vadhera is present
ReplyDeleteGood morning ma'am Arjun is present
ReplyDeletePresent maam
ReplyDeleteSanatan Ashish Hembrom is present 10-C
ReplyDeleteGood morning ma'am
ReplyDeleteAshwanth is present
Krish Kumar is present ma'am
ReplyDeleteDev Shaurya present ma'am
ReplyDeleteAtiyab Ahmad 10-C
ReplyDeletePresent maam
Ma'am Alen Thomas is present
ReplyDelete