If number > 11: print(0) elif number != 10: print(1) elif number >= 20 or number < 12: print(2) else: print(3)
Answers
Answer:
Introduction
Print,print,print
Print again
Read,read,read
Boolean
Decide if/else
While...
Make a list
String
Do you know tuples?
Go for and for
What about dictionary?
Have your own function
Your class?
Subclass of a class
Let's override
Enjoy with files
Try Except
More to do
if/else in Python
This chapter will be a new milestone in learning about the programming world and you will be one step nearer to being called a programmer. You will learn about 'if' and 'else' in this chapter.
'if' and 'else' are used for making decisions. For example, you can think of - if your marks is more than 35% then you pass, else fail. If today is your birthday then it's a special day, else normal day.
One more example you can think of is that if you are making any website and want to allow some features to users with an account only, then you can think of - if a user has account, then this else not.
if and else in python
Now let's implement this in Python.
Python 2 Python 3
a = int(input()) #taking input from user
if a>10:
print("your number is greater than 10")
else:
print("your number is smaller than 10")
Output
11
your number is greater than 10
How to write?
In the first line, we are taking input.
if a>10: - Here we have used 'if'.
a>10 - This is the condition of given to 'if'. This condition is comparing a with 10 and if a is greater than 10, then it is True and if smaller then False. So, the whole story is that if a>10 ( if a is greater than 10 ), then it will execute print("your number is greater than 10"), otherwise, it will execute print("your number is smaller than 10").
See the flow given diagram below.
if condition:
statement
statement
...
else:
statement
statement
...
We first write if condition: ( condition → What has to be judged. In the first example, the condition was 'a>10' ). Statements under if are executed only when this condition is true otherwise, the statements under else are executed.
Notice that : is written after if and else.
After writing if condition:, we write statements (in the previous example, the statement was print("your number is greater than 10")).
Statements written inside if or else must be indented equally from the left margin. It means that all the statements written inside if must be equally spaced from the left. We have used indentation of 8 spaces. It means that before every statement inside if, there is a space left of 8 spaces.
if number > 11: print(0)
elif number != 10: print(1)
elif number >= 20 or number < 12: print(2)
else: print(3)
if-elif
- The first if condition (if number > 11) checks if the given number is greater than 11 or not, if it is greater than 11 then the code prints '0'.
- The second elif condition is executed if the above condition is false. Now if the elif condition is true, that is number is not equal to 10, then '1' is printed in the console.
- If the above condition is false, the second elif condition is checked, that is if the number is greater than or equal to 20 or the number is less than 12. If any of the two conditions hold, then the whole condition is true and '2' is printed.
- Now if the above condition also fails, then '3' is printed in the console.
#SPJ2