After Python operators, we are moving to Conditionals Statement in Python. We will discuss How many types, how to use them, etc.
Like other programming languages Python also have decision-making statements.
Types of Conditionals Statement in Python:
Python programming language provides the below mention types of Conditionals statements.
- if statement.
- if…else statements
- if…elif statement
- Nested statements
- shorthand if.. else
- Pass statements
What is If Conditionals Statement in Python?
If Statement uses for decision-making operations. It contains a body of code that runs only when the condition given is true.
Syntax:
if condition expression
:
Statement
Example:
a = 55
b = 200
if b > a:
print("a is less than b")
What is If..Else Statement?
if-else Statement also uses for decision-making operations. It contains a body of code that runs only when the condition given is true, and if the conditions are false, then the code runs the else conditions.
Syntax:
if condition expression
Statement
else
Statement
For Example:
a = 100
b = 40
if b > a:
print("b is greater than a")
else:
print("a is greater than b")
Output:
How to use “elif” condition statements?
If you are familiar with the elseif statement in other programming languages, then it is easier to understand. In python, the only name is different, i.e., elif. If first, if statements are false, then code check the elif condition. If both conditions are false, then code execute the else statement.
Syntax:
if <condition>:
<statement>
elif <condition>:
<statement>
else:
<statement>
Example:
a = 100
b = 40
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Python Nested if Statement
We can use Nested if conditionals Statement in Python. Nested statement be like if…elif…else statement inside another if…elif…else statement.
After one example it will be clear:
'''Here we are taking input for nested program'''
x= float(input("Enter a number: "))
if x >= 0:
if x == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Output:
Shorthand property of if..else:
If you have only one statement for executing, one for if, and one for else, you can put it all in the one line:
I am showing an example, have a look at it:
a = 100
b = 230
print("A") if a == b else print("B")
Output:
How to use’ OR’ & ‘AND’ Keywords in Python?
The ‘AND and ‘OR’ are logical operators, and mostly, it is used to combine the two or more conditional statements.
Here, One Example and given below:
#'AND' keyword example:
a = 100
b = 40
c = 400
if a > b and c > a:
print("Both the mentioned conditions are True")
#'OR' keyword example:
a = 100
b = 40
c = 200
if a > b or a > c:
print("At least one of these conditions are True")
The pass Statement
If statements cannot be empty, But for any reason, if the statement has any type of error, then through the pass statement, it will pass out. Python pass statement is a null statement. It does not affect the results.
a = 33
b = 100
if b > a:
pass
FAQs:
Can we write if..else into one line in python?
ANS: Yes
The empty list could be created with a list() alone. True or False
ANS: False
Dictionary could be copied to another dictionary using which syntax?
ANS: dicta=dictb.copy()
Python supports automatic garbage collection. True or False.
ANS: True
Must Read:
In the above, we have discussed the Conditionals Statement in Python, their types, how to use them with suitable examples. I hope you guys like it. In the coming one, we will discuss the loops in Python.
Thank You 🙂