python
python
Simple if Statement in Python ko samajhna bohot aasan hai. if statement ek conditional statement hai jo
ek condition check karta hai. Agar wo condition True hoti hai, toh jo code us condition ke andar likha hota
hai, wo execute hota hai. Agar condition False hoti hai, toh wo code execute nahi hota.
Python code
if condition:
# code to execute if condition is True
Explanation:
Example:
Python code
x = 10
if x > 5:
print("x is greater than 5")
Step-by-step Explanation:
Csharp code
x is greater than 5
Python code
x=3
if x > 5:
print("x is greater than 5")
Is case me x ki value 3 hai, jo 5 se chhoti hai. Isliye condition False hogi aur koi output nahi milega.
Key Points:
Agar condition True hoti hai, toh if block ke andar jo code likha hota hai, wo execute hota hai.
Agar condition False hoti hai, toh if block ka code skip ho jata hai.
Python If Statement
The if statement in Python evaluates whether a condition is true or false. It contains a logical expression
that compares data, and a decision is made based on the result of the comparison.
If the boolean expression evaluates to TRUE, then the statement(s) inside the if block is executed. If
boolean expression evaluates to FALSE, then the first set of code after the end of the if block is executed.
Learn Python in-depth with real-world projects through our Python certification course. Enroll and
become a certified expert to boost your career.
Let us consider an example of a customer entitled to 10% discount if his purchase amount is > 1000; if not,
then no discount is applicable. The following flowchart shows the whole decision making process −
First, set a discount variable to 0 and an amount variable to 1200. Then, use an if statement to check
whether the amount is greater than 1000. If this condition is true, calculate the discount amount. If a discount
is applicable, deduct it from the original amount.
Python code for the above flowchart can be written as follows −
Open Compiler
discount = 0
amount = 1200
Here the amout is 1200, hence discount 120 is deducted. On executing the code, you will get the
following output −
amount = 1080.0
Python if Statement
An if statement executes a block of code only when the specified condition is met.
Syntax
if condition:
# body of if statement
Working of if Statement
Enter a number: 10
10 is a positive number.
A statement outside the if statement.
If user enters 10, the condition number > 0 evaluates to True . Therefore, the body of if is
executed.
Sample Output 2
Enter a number: -2
A statement outside the if statement.
If user enters -2, the condition number > 0 evaluates to False . Therefore, the body of if is
skipped from execution.
Indentation in Python
Python uses indentation to define a block of code, such as the body of an if statement.
For example,
x=1
total = 0
Here, the body of if has two statements. We know this because two statements
(immediately after if ) start with indentation.
We usually use four spaces for indentation in Python, although any number of spaces
works as long as we are consistent.
You will get an error if you write the above code like this:
# Error code
x=1
total = 0
if x != 0:
total += x
print(total)
Run Code
Here, we haven't used indentation after the if statement. In this case, Python thinks
our if statement is empty, which results in an error.
Syntax
if condition:
# body of if statement
else:
# body of else statement
if number > 0:
print('Positive number')
else:
print('Not a positive number')
Sample Output 1
Enter a number: 10
Positive number
This statement always executes
If user enters 10, the condition number > 0 evalutes to True . Therefore, the body of if is
executed and the body of else is skipped.
Sample Output 2
Enter a number: 0
Not a positive number
This statement always executes
If user enters 0, the condition number > 0 evalutes to False . Therefore, the body of if is
skipped and the body of else is executed.
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3
if number > 0:
print('Positive number')
else:
print('Zero')
Output
Negative number
This statement is always executed
Here, the first condition, number > 0 , evaluates to False . In this scenario, the second
condition is checked.
The second condition, number < 0 , evaluates to True . Therefore, the statements inside
the elif block is executed.
In the above program, it is important to note that regardless the value
of number variable, only one block of code will be executed.
# outer if statement
if number >= 0:
# inner if statement
if number == 0:
print('Number is 0')
Output
Number is positive