Python programming: How to use If Else in Python

0
176
Python programming How to use If Else in Python
Python programming How to use If Else in Python

Python If Else

How to use If Else in Python: In the program we are often told that we have to make decisions almost all the time. In real life, there is no such decision as we have. As if we were thinking in bed one morning. If today is Friday, I’ll sleep a bit more. And if it’s not Friday, you’ll have to wake up fast. There are many decisions that need to be made in the programming. And programming is for decision-making if-else. These are also called conditional logic.

How to use If Else in Python


Python If Else

It is used to verify if and if any other expressions are true. Let’s say we have two numbers. Comparing one to the other will see if the first one is bigger. And write it like this:

x = 21
y = 19
if (x>y):
    print("X is greater than y")

Here x = 21 is taken, y = 19 is taken. Then we check if (x> y). After that, I have asked to print X is greater than y. Now if we run the program by substituting something smaller than the value of x, then nothing will print.
Now if x is smaller than y, then user will not get any message. We want to give the user a message, even though small. And there is others for that.

x = 15
y = 19
if (x>y):
    print("X is greater than y")
else:
    print("x is less than y")

Now, we have x = 15 and y = 19. Then I checked first what is greater than x? If you are older, I have asked to print a message. Then else if I say x is not greater than y, then I have asked to print another message.

How easy! Many simple programs can be written using this simple if else. No matter how many programs we write, we often have to write code using this simple logic.

Now we can write a dynamic program. We can input the numbers from the user to find out whether this is small or big.
x = int(input('Enter 1st number: '))
y = int(input('Enter 2nd number: '))
if (x>y):
    print("X is greater than y")
else:
    print("x is less than y")

In some cases we may have to check the condition more than once. In the above program we have verified the condition only once. So it worked with an if and else. But we may need to use more than one if, then we can use elif or else if. For example, if the user gives the same input to two numbers, then the above program will give the wrong output. So we will check if two numbers are equal:

x = int(input('Enter 1st number: '))
y = int(input('Enter 2nd number: '))
if (x>y):
    print("X is greater than y")
elif (x<y):
    print("x is less than y")
elif(x==y):
    print("Both are equal")

We know in previous chapters whether two variables are equal, it is verified with two equal signs.And we’ve used if, elif, etc for so long, it’s called Control Flow. The above programs and the way we make decisions in real life are the same. We always say when talking, it will do it or else it will. Same thing in programming. if else.

Suppose you have a friend named Bholanath who forgets everything. He wants to create a robot to convince him when to do what to do. The robot is like a rotten object, unless we tell him what to do. Now you have to write a program for your robot.  Who will tell your friend what to do when. If it is morning, he will tell you to go to school. In the afternoon he will tell the game to dust. And in the evening, he will ask you to sit down. And in the night, he will get to sleep with Bholanath telling the story. Other times he would not disburse her. We wrote a program like this in Python:


time = “morning”

if time== "morning":
     print ("pack you bag and go to school")
elif (time=="afternoon"):
     print ("it’s gaming  time")
elif (time=="evening"):
     print ("it’s reading  time")
elif (time=="night"):
     print ("it’s sleeping time")
else:
     print ("You can do anything you like")

You can try running the program with afternoon, evening, night, etc. instead of time = morning in the first line.

Nested if else:


We can use another if else if we want. Which is called nested if else.

x = 5
if(x > 0):
    if(x%2==1):
        print("The number is positive and an odd number.")

Here we have a variable whose value is set to 5. Then we first check if the value is greater than 1. Since it is greater than zero, the number is positive. Then check if the number is divisible by 2. If dividing by 2, the remainder is 1, that means a number is an odd number. The above program will print as follows:
The number is positive and an odd number.
Now we can write a dynamic program:

x = int(input("Enter a number:"))
if(x > 0):
    if(x%2==1):
        print("The number is positive and an odd number.")
    else:
        print("The number is positive and an even number")
else:
    print("You entered a negative number")

That way we can use one IF inside another, another IF inside it. Here the indentation should be noted. If the indentation is wrong, then the program will not work properly.

Note: More Info (Tech24InfoBD) (How to use If Else in Python)

LEAVE A REPLY

Please enter your comment!
Please enter your name here