This is the Python Tutorial: Python if … else Statements. In this Python Tutorial, you will learn the Python if, if … elif, else Statements. At the end of this tutorial, you will have full knowledge of Python if, if … elif, else Statements.
Hello, & Welcome!
Part 6- Python Tutorial: Python if, if … elif, else Statements
In this article of Python Tutorial, you will learn following-
- If statement and its syntax.
- elif statement and its syntax.
- if-elif-else statement and its syntax.
- Nested if-else and its syntax.
- Logical operators.
- Logical conditions.
If statement and its syntax
These conditional statements in python are used, when you want to run your code with certain conditions.
If statement is used when you test some condition.
Syntax of if statement is-
if test condition:
action(a)
Here, first, it checks the condition, if the condition is true then the action executes otherwise, no statement executes.
Let’s have a look in the example-
age=5
if age<10:
print('You are young')
OUTPUT-
You are young
Here, age=5 and it’s less than 10, which means the condition is true, that’s why it prints a message.
Indentation–
Indentation in python is compulsory because if we don’t do indentation, code doesn’t work.
For example, if you write code without indentation, then it will give you an error.
age=5
if age<10:
print('You are young') #wrong indentation, gives error.
elif statement and its syntax.
you can use elif statement when you wanna use more than one condition to check. When if condition is false, then it switches to elif condition and execute the elif statement.
Syntax of elif statement is-
if test condition:
action(a)
elif test condition:
action(b)
Here, first, it checks the if condition, if it is true so the action is executed, but if the condition is not true then elif condition is checked and if it’s true, then its action is executed.
Let’s take a look in the example-
a=5
b=5
if a<b:
print('a is less than b')
elif a==b:
print('a and b are equal')
OUTPUT-
a and b are equal
Here, the first condition is not true, which is a<b, but second condition a==b is true, that’s why elif statement is executed.
if-elif-else statement and its syntax.
if-elif-else is used when if and elif statement gets false, so else will execute. But if anyone in if or elif is true, so else will not execute.
Syntax of if-elif-else-
if test condition:
action(a)
elif test condition:
action(b)
else test condition:
action(c)
Here, first it checks if and elif conditions, if both are false then else will execute.
Let’s see in the example-
a=50
b=10
if a<b:
print('a is less than b')
elif a==b:
print('a and b are equal')
else a>b
print('a is greater than b')
OUTPUT-
a is greater than b
Nested if-else and its syntax.
Nesting means use if-else statements inside another if-else statement. Indentation is important to perform nesting.
Syntax of Nested if-else statement-
if test condition:
if test condition:
action(a)
else:
action(b)
else
action(c)
Here, there are two cases-
- When If is true- It checks the first if condition, if it is true then it will go inside another if and test condition if it is true, the action is executed. If it is not true, then else statement is executed.
- If is not true- If the starting if condition is not true, then without going inside another if, directly the last else statement is executed.
Example of nested if-else-
age = 30
if age <= 19:
if age == 2:
print("Infant")
else:
print("Teenager")
else:
print('Adult')
OUTPUT-
Adult
Here, age=30, which is not less than 19, that’s why, the whole if statement is truncated, and control goes to the last else statement, and it is executed.
Logical operators.
There are basically two logical operators-
- AND
- OR
AND Operator-
AND operator is used to combining two or more conditional statements.
Lets’ see in the example-
John=35
Alex=30
Max= 40
if John> Alex and Max>John:
print("Max is elder than John and Alex")
OUTPUT-
Max is elder than John and Alex
- AND operator executes only when both the conditions are true. If anyone of them is false, then it doesn’t execute.
OR Operator-
You can use the OR operator to combine two or more than two conditional statements. It executes if any one of the statements is true.
Suppose you have two conditional statements and one is true another one is false, still OR operator executes.
Let’s see in the example-
John=35
Alex=30
Max= 40
if John> Alex or Max==John:
print("Max is elder than John and Alex")
OUTPUT-
Max is elder than John and Alex
Logical Conditions-
There are following Logical conditions-
- < (less than)
- For eg. x<y
- > (greater than)
- For eg. x>y
- == (Equal)
- For eg. x==y
- != (not equal to)
- For eg. x!=y
- => (equal to or greater than)
- For eg. x=>y
- =< (equal to or less than)
- For eg. x=<y
That’s all for Python if … else Statements, I hope now you have a better understanding of Python if … else Statements.
congratulations! You successfully learned Python if … else Statements.
In the next tutorial, we will start learning Loop in python.
Enjoy Learning Python!
Are you looking for Best Books on Python for Beginners and for Intermediate/Experts?
Check these Books, These Books are the best selling and covered everything in detail.
I hope you will find these Books helpful-
Best Selling Python Books for Beginners
Best Selling Python Books for Intermediates/Experts
All the Best!
Thank YOU!
Though of the Day…
‘“Live as if you were to die tomorrow. Learn as if you were to live forever.”
– Mahatma Gandhi
Written By Aqsa Zafar
Founder of MLTUT, Machine Learning Ph.D. scholar at Dayananda Sagar University. Research on social media depression detection. Create tutorials on ML and data science for diverse applications. Passionate about sharing knowledge through website and social media.
Pingback: Python Tutorial: Standard Input Output in Python- Beginners Guide(2020)