Monday, November 9, 2020

Conditional Statement if, if and else, if and elif with else using Arithmetic Operators and Logical Operators in python3

 If you are new to coding means conditional statement will be unfamiliar to you. For that we can take a real-time example for understanding it, for example if we need to check a condition, like above 50% is the pass mark means we need to put a condition for that to check whether mark is above 50 or not, so for that we need the if statement in python to write the condition. The code should be stated as below


if mark > 50:
print("You are pass")

Here ‘if’ is the keyword to check the condition and ‘mark’ is the variable which carries the input of the student’s mark, as per the above condition we are checking whether the student’s mark is greater than pass mark(50) or not. 

We can see a flow chart




I think it should be clear, if the condition satisfies means only "a is greater than b" will be printed 

Now we can see the whole program so that it will be clear.

mark = input('Enter the mark:')
mark = float(mark)
if mark > 50:
print('You are pass')

mark variable is getting input from the user and then we are converting that value into float value and then we are checking whether it is greater than 50 or not, if yes means the statement 'You are pass' will be printed.


Next we can see another scenario

now assume that if mark is not greater than 50 means we need to print a message that ‘you are fail’, in this situation we have to put else keyword, let’s see the code

 

mark = input ('Enter the mark:')
mark = float(mark)
if mark > 50:
print('You are pass')
else:
print('You are fail')

Then another important thing is indent a tab can be seen in the next line of the if statement, this is used to mention that those statements are under the if block, likewise for if statement what indent is given should be the same for else block also, indent spacing is very important in python, as in java we will use braces and all, instead of that only indent is used in python, this will make sure the blocks. For example if mark is greater than 50 means we may want to display some three or four statements, so how it will be understandable, it is through indent only, see the below code

 

 if mark > 50:
print('Congrates')
print('You are pass')
print('The mark you got is:',mark)
else:
print('You are fail')

So as per the coding the three print statements will be coming under the ‘if’ block, if the ‘if condition’ is true means, these three lines will be printed.


Now we can see where we want to use elif, let we assume that we want to print the below statements for the appropriate scenarios means

“You are pass” for mark above than 50

“You got distinction” for mark above than 75 

“You are fail” for mark less than 51


if mark > 50 and mark <= 75:
print('You are pass')
elif mark > 75;
print('You got distinction')
else:
print('You are fail')

For better understanding, I will write the values for what mark we will get what message

If the mark is between 50 and 76 (ie., 51 to 75) means message "You are pass" will be printed

If the mark is above 75 means message "You got distinction" will be printed

If mark is less than 51 means message "You are fail" will be printed


Likewise it is not compulsory to have else statement on every if or elif statements, as per the need we can use, likewise if needed we can have as many as elif we needed like


if mark > 50 and mark <= 75:
print('You are pass')
elif mark > 75 and mark < 100:
print('You got distinction')
elif mark > 100 or mark < 0:
print('Please enter valid mark from 0 to 100')
else:
print('You are fail')

Ok, now we can see what are the conditions we can use
Double Equals == 
Not Equals != 
Less than < 
Less than or equal to <= 
Greater than > 
Greater than or equal to >=

Finally we can see a flow chart for if elif and else



Now we can see Arithmetic Operators in python

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus

** Exponentiation

// Floor division



Then the Logical Operators

and

or

not


You had seen the above mentioned logical operators in the condition we had used the sample program


If you are having doubts still means you can view the below video




Conditional Statement if, if and else, if and elif with else using Arithmetic Operators and Logical Operators in python3

 If you are new to coding means conditional statement will be unfamiliar to you. For that we can take a real-time example for understanding ...