We have covered the basic data types and advanced data types in python in our previous blog posts.. In this blog, the conditional statements will be covered. If you are new to Python, please start from the first blog post to get a better understanding of this blog.
The conditional statements in Python regulate the flow of the code execution. In a very layman term, these statements are used when you want the program to do a task if a condition is satisfied and not to do the same task when the condition is not fulfilled.
Up till now, we have just printed out the output but never gave any input to our program. In Python input() is used for giving input to the program in python. The example is illustrated below.
For Example:
# Take input x=input() print(x)
The above code will ask for an input which will be stored in the X variable for further usage.
Output: 5 5
The input can also have a string query in it. The example is illustrated below.
# Take input x=input(âplease enter your age?â) print(x)
Output: please enter your age. 5 5
Even the input can be modified using the datatype functions used in the typecast of a datatype. The example is illustrated below.
# Take input x=int(input(âplease enter your age?â)) y=input(âplease enter your age?â) print(type(x)) print(type(y))
Output: please enter your age. 5 please enter your age. 5 <class âintâ> <class âstrâ>
In the above example, we can see that the input without any typecast function is a string value. Hence, the default value for input is string.
If a program has only a single decision to make, then one âifâ statement is used. Letâs take an example where we want to allow a person only if he or she has a mask.
#if statement mask=True if mask==True: print(âthe person can enterâ)
The syntax is quite simple, itâs followed by the condition and indentation of one tab space whenever there is something in the if statement. When we discussed the operators in the variables blog. We discussed comparison operators, logical operators, and mathematical operators.
In this condition, both comparison operators and logical operators can be used. In the above example, we can see that we used â==â operator for comparison. In the above program if the mask is True then the statement will be printed otherwise it will not print anything.
Letâs execute the program and examine the output.
Output: the person can enter
What will happen if we change the make value to False? The output will be as given below. Which is empty â nothing will be printed as the condition is not fulfilled.
Output:
In the above example, we just have a condition, which says if a person has mask they can enter. But there is not otherwise, what to do if the person doesnât have a mask. Hence it seems to be an incomplete program. Letâs say if they donât have a mask, we want them to get a mask to enter. For this we will be using else statement which executes only when the âifâ statement condition is not fulfilled.
Example is illustrated below.
#if else statement mask=True if mask==True: print(âthe person can enterâ) else: print(âplease, get a mask to enterâ)
Now if we change the value of the mask to False, then we will get âplease, get a mask to enterâ)
#if else statement mask=False if mask==True: print(âthe person can enterâ) else: print(âplease, get a mask to enterâ)
Output: please, get a mask to enter
This can also be written in the below format.
#if else statement mask=False if mask==True: print(âthe person can enterâ) print(âplease, get a mask to enterâ)
In , whenever you write a statement after the if without indentation, it taken to be under else statement.
Now letâs add a case, where if a person doesnât have a mask but is willing to buy it , can buy the mask from the guard itself and enter. For this we will change our earlier code a bit. We will give string values such as ânobuyâ ,âbuyâ, âyesâ. Now we will use these to write our if statements.
#if else statement mask= if mask==âyesâ: print(âthe person can enterâ) elif mask==âbuyâ: print(âperson bought the mask and can enterâ) print(âplease, get a mask to enterâ)
Now the according to the mask value, the execution will be done. If the mask value is ânobuyâ, we will get the output to be âplease, get a mask to enterâ.
#if else statement mask=ânobuyâ if mask==âyesâ: print(âthe person can enterâ) elif mask==âbuyâ: print(âperson bought the mask and can enterâ) print(âplease, get a mask to enterâ)
Output: please, get a mask to enter
Even if the mask is given any other value, we will get the result to be âplease, get a mask to enterâ. This is because above two if statement conditions will not be fulfilled.
#if else statement mask=âyesâ if mask==âyesâ: print(âthe person can enterâ) elif mask==âbuyâ: print(âperson bought the mask and can enterâ) print(âplease, get a mask to enterâ)
For âyesâ value in mask, the output will be âthe person can enterâ.
#if else statement mask=âyesâ if mask==âyesâ: print(âthe person can enterâ) elif mask==âbuyâ: print(âperson bought the mask and can enterâ) print(âplease, get a mask to enterâ)
Output: the person can enter
For âbuyâ in the mask, the output will be (âperson bought the mask and can enterâ).
#if else statement mask=âyesâ if mask==âyesâ: print(âthe person can enterâ) elif mask==âbuyâ: print(âperson bought the mask and can enterâ) print(âplease, get a mask to enterâ)
Output: the person bought the mask and can enter
In this blog, we have covered some basics of conditional statements in , the further topics on functions will be covered in the next blog post. From this blog onward, the reader will be given some practice questions, the answers will be available in the next blog for the questions in this blog.
You may also like our JavaScript Course from Beginner to Advanced.
Author: Robert Whitney
JavaScript expert and instructor who coaches IT departments. His main goal is to up-level team productivity by teaching others how to effectively cooperate while coding.
Pinterest, which made its debut on the social media scene a decade ago, never gained…
Thinking carefully on a question of how to promote a startup will allow you to…
A podcast in marketing still seems to be a little underrated. But it changes. It…
Video marketing for small business is an excellent strategy of internet marketing. The art of…
Are you wondering how to promote a startup business? We present crowdfunding platforms and websites…
How to use social media to increase sales? Well, let's start like that. Over 2.3…