Long Time, No See!
Hey there , In this short blog I am going to teach you that how can you use basic if-elif-else statement in Python.
Make sure you have seen my previous blog for more basics.
- What Is a Conditional Statement?
- Use Of if-else Statement.
- The elif statement
- Example program
What Is a Conditional Statement?
Conditional statements are those statements by using which you can determine different outputs of a situation in your program. When you use these conditions the output will be based on whether the condition given in the code is satisfied or not. In this statement, different subcodes are written which are executed if the provided condition is satisfying.
Use Of if-else Statement
Execution of certain commands only if the if condition provided is satisfied. Otherwise, the command within the else condition will be executed.
age=18 if age>=18: print("eligible") else: print("not eligible")
Keep In Mind While using if-else.
- Never forget to write colon(:) after determining the condition.
- Commands under the if and else condition is written after leaving 4 spaces in the next line.
The elif Statement
While using an if-else statement you can only determine one condition but when you want to determine more than one condition.
So, for that, comes the elif statement by which you can determine more than one condition or I can say as much as the number of conditions you want to determine.
Let me show you this through syntax.
a=49 if a%2==0: print('a is divisible by 2') elif a%3==0: print('a is divisible by 3') elif a%5==0: print('a is divisible by 5') elif a%7==0: print('a is divisible by 7') else: print('Who Knows')
Here , let me show a basic program to take food order from user through a simple python program.
a=int(input('''Enter 1 for Burger Enter 2 for Pizza Enter 3 for Noodles Enter 4 for Manchurian''')) if a==1: print('Your Burger will be delivered in 5 minutes') elif a==2: print('Your Pizza will be delivered in 5 minutes') elif a==3: print('Your Noodles will be delivered in 5 minutes') elif a==4: print('Your Manchurian will be delivered in 5 minutes') else: print('Enter Correct Order Number')
Thank you for sticking till last , feel free to ask any query in the comment box. Don’t forget to read our other blogs.