- The if Statement
- The if-else Statement
- The if-elif Statement
- The nested if Statement
The if statements are the condition statements in python. And we will also try to learn about the if statements in this article and as you know the if statements are the condition statements in python. Without condition, some programs become useless because the condition is necessary for the program to accept some small program. So, let's start with the if statement.
The if Statements in python
The if statements are used to test a condition and if the condition is equal to true, then it performs some task and if the condition is equal to false, then it performs some other task or stops.
The general form( syntax ) of if statement
Now, we will do some examples to understand the if statements.
So, we will make a program of even or odd numbers checker. That program identified the number is odd or even.
So, first, we make a variable that contains one number. And we apply the condition to checker the number is divisible by 2 or not, if the number is divisible by two then we print "number is even" otherwise we did not print anything.
num = 2if (num%2)==0:print("Number is even")
Here "%" operator is used for division without goes to answer in the float. Therefore we use this operator here to identify that the number is completely divisible by 2 or not.
In an, if statement, if the conditional expression equal to true, then the statements in the body of if are executed, otherwise, the statements have been ignored.
The if-else Statement
In general words, the if-else statements test a condition, if the condition is true then the statements are executed and if the condition becomes false it executive the else statements.
The general form ( the syntax ) of the if-else statements.
We, understand the if-else condition by using an example.
if a >=0:print( a, "number is positive" )else:print( a, "number is negative" )Output:55 number is positive-3-3 number is negative
Unlike the previously discussed plain-if statement, which does nothing when the condition results in false, the if-else statement performs some action in both cases whether the condition is true or false. Take one more example.
if sales >=10000:discount = sales*0.10else:discount = sales*0.05
The above statements calculate discount as 10% of the sales amount if it is 10000 or more otherwise it calculates discount as 5% of the sales amount.
The if-elif Statement
Sometimes, we need to check multiple conditions in case the test- condition of if evaluates to false. That is, you want to check a condition when control reaches else part, i.e., condition test in the form of else if.
In general words we say, if sometimes, we need to check the condition multiple times at that time we use elif statements.
The general form( the syntax ) of the elif statement:
if condition:statementselif condition 1:statements 1elif condition 2:statements 2else:statements3
a = input()if a < 0:print("Number is negative")elif a == 0:print ("Number is equal to zero")else:print ("Number is positive")
In this above example we use multiple conditions by using elif condition first we take the input
from the user now, we apply multiple conditions to check the number is negative or positive or zero by using elif statement.
The nested if Statement
Sometimes you need to apply condition in the condition. For example want to compare x, y, z, and so first we will compare x or y and x or z and in nested we compare y or z, and finally, we get the output what we need.
The general form( the syntax ) of the nested if statement:if condition:if condition:statementselse:statementselse:statements
So, in this post, we will learn about, how we apply conditions in the python program and get output with accuracy. And we will also discuss some examples that make the if else easy to understand. And we will also learn about the nested if and elif in python.