Hello Pythonistas, In this article I’m going to share with you the basics of condition blocks using if statement in python and how you can use it in building your python programs.
For instance, you want to make a python program to determine whether a given number is odd or even, to do that you will need If else statement.
Syntax of if else statement
if condition: do this else : do that
If the condition is true, our code will execute the do this statement else they will execute the do that statement which can be a series of python instruction.
The block in python is specified using Indentation just as shown in the syntactical example above.
Now that we know the syntax of how to structure our conditional statement using the if statement Let’s do some examples.
Program to check whether the number is positive or negative
First of all, we have to prompt the user to enter any number and then we gonna cast the number entered to integer and then finally using the number to compare if it is above or below 0
How to check whether a number is odd or even
Same wise you could write a program to determine where a given number is odd or even, whereby our condition will be whether a number can be divided by 0 without remainder using the modulus operator
Using multiple If statement in python
There some problems which can turn out too hard or impossible sometimes to solve using a single if-else statement therefore in those scenarios it requires us to use multiple if-else statements.
A good example of those problems is building a basic arithmetic calculator that receives the first number, an operation, and a second number and then evaluate the result based on the operation.
In coding multiple conditional statements you have to use elif statement which is like the normal if statement which stays in the middle of the if-else statement.
how to build Arithmetic calculator in Python
Multiple conditions in one if statement
With Python, you can also write multiple expressions or conditions in a single conditional statement and then python will evaluate the whole thing as a whole.
For instance, you are given a task to automate student grading and on specifying grades with logic we need to have at least two conditions.
For instance for a grade to be C it need to be above 40 and less than 60
How do we join multiple conditions ?
We basically use keywords ( and & or ) to join the conditions for them to evaluate as one.
- or -> will evaluate to True if any of the conditions are true and False if both of them are False
- and -> will evaluate to False if any of its conditions are False and True if all of them are True
Student Automatic marks grading program
Below is the sample Python program using multiple condition statement to automatically grade the marks of the students.
Using or to Evaluate Multiple expression and conditions
Hope you find this post interesting, Now don’t be shy feel free to share it with your fellow developers.
I recommend also checking this , based on your interest;
- Learn how to work with files in Python
- Understand python args and kwargs
- The basics of OS Module in Python
- A beginner guide to Web scraping in Python
- An Introductory Guide to Python List comprehension
- How to speed up a python program using memorization
- Beginners guide to Integrating Python and SQL
In case of any suggestion or comment , drop it on the comment box and I will reply to you immediately.
Don’t forget to subscribe to this blog to stay updated on upcoming Python tutorials and tips.