Decision-making is as important in any programming language as it is in life. Decision-making in a programming language is automated using conditional statements, in which Python evaluates the code to see if it meets the specified conditions.
The conditions are evaluated and processed as true or false. If this is found to be true, the program is run as needed. If the condition is found to be false, the statement following the If condition is executed.
This article was published as a part of the Data Science Blogathon
Let’s take a glance at how each of those works.
The If statement is the most fundamental decision-making statement, in which the code is executed based on whether it meets the specified condition. It has a code body that only executes if the condition in the if statement is true. The statement can be a single line or a block of code.
Syntax of if-statement is given below:
if expression:
Statement
If the condition is true, the statement will be executed.
num = 5
if num > 0:
print(num, "is a positive number.")
print("This statement is true.")
Output:
5 is a positive number.
a = 25
b = 170
if b > a:
print("b is greater than a")
Output :
b is greater than a
This statement is used when both the true and false parts of a given condition are specified to be executed. When the condition is true, the statement inside the if block is executed; if the condition is false, the statement outside the if block is executed.
The if…Else statement in Python has the following syntax:
if condition :
#Will executes this block if the condition is true
else :
#Will executes this block if the condition is false
num = 5
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
Output :
Positive or Zero
Also Read: 90+ Python Interview Questions to Ace Your Next Job Interview in 2024
In this case, the If condition is evaluated first. If it is false, the Elif statement will be executed; if it also comes false, the Else statement will be executed.
The If…Elif..else statement in Python has the subsequent syntax:
if condition :
Body of if
elif condition :
Body of elif
else:
Body of else
We will check if the number is positive, negative, or zero.
num = 7
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Output:
Positive number
A Nested if statement (if else conditional statement) is one in which an If statement is nestled inside another If statement. This is used when a variable must be processed more than once. If, If-else, and If…elif…else statements can be used in the program. In Nested If statements, the indentation (whitespace at the beginning) to determine the scope of each statement should take precedence.
The Nested if statement in Python has the following syntax:
if (condition1):
#Executes if condition 1 is true
if (condition 2):
#Executes if condition 2 is true
#Condition 2 ends here
#Condition 1 ends here
num = 8
if num >= 0:
if num == 0:
print("zero")
else:
print("Positive number")
else:
print("Negative number")
Output:
Positive number
price=100
quantity=10
amount = price*quantity
if amount > 200:
if amount >1000:
print("The amount is greater than 1000")
else:
if amount 800:
print("The amount is between 800 and 1000")
elif amount 600:
print("The amount is between 600 and 1000")
else:
print("The amount is between 400 and 1000")
elif amount == 200:
print("Amount is 200")
else:
print("Amount is less than 200")
Output :
The amount is between 400 and 1000.
Also Read: A Complete Python Tutorial to Learn Data Science from Scratch
Short Hand if statement is used when only one statement needs to be executed inside the if block. This statement can be mentioned in the same line which holds the If statement.
The Short Hand if statement in Python has the following syntax:
if condition: statement
i=15
# One line if statement
if i>11 : print (“i is greater than 11″)
Output:
i is greater than 11.
It is used to mention If-else statements in one line in which there is only one statement to execute in both if and else blocks. In simple words, If you have only one statement to execute, one for if, and one for else, you can put it all on the same line.
#single line if-else statement
a = 3
b = 5
print("A") if a > b else print("B")
Output:
B
#single line if-else statement, with 3 conditions
a = 3
b = 5
print("A is greater") if a > b else print("=") if a == b else print("B is greater")
Output:
B is greater
If you’re reading this, you’re most likely learning Python or trying to become a Python developer. Learning Python or another programming language begins with understanding the fundamental concepts that form its foundation.
By the end of this text, you should understand the various If else conditions used in python.
A. The conditional operator in if-else statements is the ternary operator (?:), which takes three operands: condition ? value_if_true : value_if_false.
A. An example of a conditional statement is: if (x > 0) { printf(“x is positive”); } else { printf(“x is non-positive”); }
A. The if-then-else statement is a control flow statement that executes different blocks of code based on the evaluation of a condition. It allows for branching and decision-making in programs.
The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.
it's very easy to learn
Comments are Closed