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.
Conditional statements in Python – table of contents:
- Conditional statements in Python – what do they do?
- Python input()
- If statement in Python
- Syntax in Python
- If else in Python
Conditional Statements in Python – what do they do?
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.
Python input()
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 statement in Python
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”)
Syntax in Python
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:
If else in Python
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 Python, 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 Python, 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.
Python Course From Beginner to Advanced in 11 blog posts:
- Introduction to Python Course. Part 1 Python Course from Beginner to Advanced in 11 blog posts
- Variables and Data Types in Python. Part 2 Python Course from Beginner to Advanced in 11 blog posts
- Python tuples, lists, sets and dictionaries. Part 3 Python Course from Beginner to Advanced in 11 blog posts
- Python sets and dictionaries. Part 4 Python Course from Beginner to Advanced in 11 blog posts
- Conditional statements in Python. Part 5 Python Course from Beginner to Advanced in 11 blog posts
- Loops in Python. Part 6 Python Course from Beginner to Advanced in 11 blog posts
- Python functions. Part 7 Python Course from Beginner to Advanced in 11 blog posts
- Advanced functions in Python. Part 8 Python Course from Beginner to Advanced in 11 blog posts
- Python classes and objects. Part 9 Python Course from Beginner to Advanced in 11 blog posts
- Files in Python. Part 10 Python Course from Beginner to Advanced in 11 blog posts
- Python applications in practice. Part 11 Python Course from Beginner to Advanced in 11 blog posts