In this tutorial, we will learn about If-else Statements in Python and we will also cover different examples related to Python If-else statements and, we will cover these topics.
- Python If-else statements
- Indentation in Python
- The if statement
- The if-else statement
- The elif statement
Python If-else statements
In python or any other language, Decision Making is a very important feature. Decision-making helps to run the specific block of code on a particular decision. Conditions are very important and play the role of backbone in decision making. We can perform the decision-making by using the following Statements.
Statements | Description |
If- Statement | It is used to run the specific block and if the condition is true it is only executed at that time only. |
If-else Statement | This condition is almost the same as the if statement but in this, it also provides us the block for the false condition where it checks that if the condition is true it will run that block only but if the condition is false then it will run the else block. |
Nested-If Statement | In Nested- if statement is the if statement that targets another if statement inside the if statement. Python allows us to use nested if inside the if statement. |
Indentation in Python
In Python, it doesn’t allow the use of the parentheses in the block-level code. It uses the indentation to declare the block. Basically, it refers to the spaces and tabs that we used at the beginning of the statement. If two statements are inside the same indentation then it is the same block of code.
In python, the indentation takes four spaces to indent the statements. We will further see how the indentation is used in decision-making statements with different python examples.
The if statement
The if statement is used as the most simple decision-making statement. It is used to run the specific block and if the condition is true it is only executed at that time only. The condition of the if statement is only valid when the logical expression is either appraise as true or false.
Flow Chart:
Syntax Code:
if expression:
statement
Example:
Code:
In the following code, we are showing the example of the If condition.
number = int(input("enter the number?"))
if number%2 == 0:
print("Number is even")
Output:
After running the following code we get the following output where it checks the number is even using the if condition.
Example 1.0:
Code:
In the following code, we are checking the value with the largest number.
p = int(input("Enter p? "));
y = int(input("Enter y? "));
t = int(input("Enter t? "));
if p>y and p>t:
print("p is largest");
if y>p and y>t:
print("y is largest");
if t>p and t>y:
print("t is largest");
Output:
After running the following code we get the following output where it prints the largest value after comparing from the other assigned values.
The if-else statement
The If- else statement is almost the same as the if statement but in this, it also provides us the block for the false condition where it checks that if the condition is true it will run that if block only but if the condition is false then it will run the else block.
Flow Chart:
Syntax Code:
if condition:
#block of statements
else:
#another block of statements (else-block)
Example: WAP to check whether a person is eligible to vote or not.
Code:
In the following code, we are checking the eligibility of a person to vote where it uses the condition if py_age>=18 if it is true then it will run the if block but on another, side if age is less than 18 then it will run the else block.
py_age = int (input("Enter your age? "))
if py_age>=18:
print("You are eligible to vote !!");
else:
print("Sorry Your are not eligible");
Output:
After running the following code we get the following output we can see a person who is 17 is unable to vote but the person who is 24 is eligible to code.
Example: WAP to check whether a number is even or not.
Code:
In the following code, We are checking whether the number is even or odd. Where it matches the condition if the number is divisible by 2 then it is an even number but if the number is not divisible by 2 then it is an odd number.
number = int(input("enter the number?"))
if number%2 == 0:
print("Number is even...")
else:
print("Number is odd...")
Output:
After running the following code we get the following output where it matches the condition if the number is divisible by 2 then it is an even number but if the number is not divisible by 2 then it is an odd number.
The elif statement
In Nested- if statement is the if statement that targets another if statement inside the if statement. Python allows us to use nested if inside the if statement.
Flow Chart:
Example Code:
In the following code, we are finding the equal number using the elif condition.
num = int(input("Enter the num?"))
if num==10:
print("num is equals to 10")
elif num==50:
print("num is equal to 50");
elif num==100:
print("num is equal to 100");
else:
print("num is not equal to 10, 50 or 100");
Output:
After running the following code we get the following output where it checks the number if it is equal to 10,50, and 100 then it will print the value with the num is equal to 50 if no same value is assigned then it will print the number is not equal.
Example 2:
In the following code, we are giving the grade to students according to marks scored in exam subjects using the elif condition.
score = int(input("Enter the score? "))
if score > 85 and score <= 100:
print("Congrats ! you scored grade A ...")
elif score > 60 and score <= 85:
print("You scored grade B + ...")
elif score > 40 and score <= 60:
print("You scored grade B ...")
elif (score > 30 and score <= 40):
print("You scored grade C ...")
else:
print("Sorry you are fail ?")
Output:
After running the following code we get the following output where we can see the two different outputs where one student who scores marks less than 40 is failed and having marks between 85-100 is having the grade A.
So, in this tutorial, we discussed Python If-else statements and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- Python If-else statements
- Indentation in Python
- The if statement
- The if-else statement
- The elif statement
Do follow the following tutorials also:
Yesterday, while I was at work, my sister stole my iPad and tested to see if it can survive a thirty foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views. I know this is totally off topic but I had to share it with someone!
The root of your writing whilst sounding agreeable originally, did not really work very well with me after some time. Someplace throughout the sentences you managed to make me a believer but just for a very short while. I however have a problem with your jumps in assumptions and one might do nicely to help fill in all those gaps. If you actually can accomplish that, I would definitely end up being amazed.
Great post, I think website owners should acquire a lot from this web site its real user pleasant.
Nice post. I was checking constantly this blog and I’m inspired! Very useful info particularly the final phase š I maintain such info a lot. I was looking for this certain info for a long time. Thanks and good luck.
Hey, you used to write excellent, but the last several posts have been kinda boringK I miss your super writings. Past several posts are just a little out of track! come on!
I am often to running a blog and i really admire your content. The article has actually peaks my interest. I am going to bookmark your website and maintain checking for new information.
Your style is so unique compared to many other people. Thank you for publishing when you have the opportunity,Guess I will just make this bookmarked.2
You really make it appear so easy along with your presentation but I in finding this topic to be really something that I believe I might never understand. It kind of feels too complex and extremely broad for me. I am having a look ahead on your subsequent publish, I will try to get the dangle of it!
You completed some good points there. I did a search on the matter and found the majority of people will consent with your blog.
I was wondering if you ever thought of changing the layout of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or two pictures. Maybe you could space it out better?
I always was interested in this subject and stock still am, thanks for putting up.
Wonderful site. Lots of useful information here. I?Ā¦m sending it to a few buddies ans also sharing in delicious. And of course, thank you to your sweat!