In this article, we are going to tell about the if-else loops used in Python. So, before coming to loops we first need to know what is loop and why we need a loop.
If you have a prior idea of knowing other programming languages you can get it. That when we want to access a number of conditions we need the loop is the if-else loop. We put the condition in the if part and then print the statement. After that, we print the statements of the else part which are not included in the condition. These are the contents of this article :
Introduction to If-Else
So, the if-else part which supports logical conditions are :
- Equals: a=b
- not equals: a!=b
- less than: a<b
- greater than: a>b
- less than equal: a<=b
- greater than equal: a>=b
The if statement has shown below:
a=26
b=21
if a>b:
print("a is greater than b") #returns a is greater than b
Look in the above statements a value is 26 and b value is 21. That means a >b this condition satisfies and it prints the value “a is greater than b“
But did you see the indentation? Yes, the indentation is the main thing in if-else rather any loop in Python. Now see the below statement and error occurred due to not using proper indentation.
a=26
b=21
if a>b:
print("a is greater than b")
We use elif or else if there’s more than one statement to print according to more no of conditions.
a=50
b=50
if a>b:
print("a is greater than b")
elif a==b:
print("a is equals to b")#returns a is equals to b
In the above statements as a value is equals to be it comes to the elif or else part and print the statement.
Else
If the above all conditions fails then the else statement is printed. We have also used else loop in while loop
a=5
b=10
if a>b:
print("a is greater than b")
elif a==b:
print("a is equals to b")
else:
print("a is less than b") #returns a is less than b
In the above example, a value is less than b value. So that the else part have been printed only.
Short Hand If
In case of single-lined statement to execute we use this in the same line of the if.
a=10
b=16
if a<b: print("a is less than b") #returns a is less than b
Shorthand Else-If
We can also use this if there are two statements. One is for if and another one is for else-if.
a=10
b=16
print("a") if a<b else print("b") #returns a
As the value of a is less than b so a is printed according to the condition.
This one-lined condition-statement technique is known as the ternary operator or conditional operator. For more to know you can refer to this site.
We can use this for three conditions
a = 10
b = 10
print("a") if a > b else print("=") if a == b else print("b")
And
In order to add conditions we use and keyword.
a=10
b=50
c=100
if b > a and c > a:
print("True")#returns true
Or
We use this statement if only one condition among the conditions are true the statement will be printed.
a=10
b=3
c= 20
if a > b or a > c:
print("At least on condition satifies") #returns true
Nested If
An If within an If can also be possible. It is known as nested if.
x=50
if x>10:
print("It is greater than 10 ")
if x>40:
print("It is greater than 40")
else:
print("It is not greater than 100")
The pass Statement
If there is no statement under the if condition then we use the keyword pass to avoid getting an error.
a=50
b=100
if a<b:
pass