Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

python

good

Uploaded by

skhan660954
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

python

good

Uploaded by

skhan660954
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Simple if Statement in 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.

Syntax of Simple if Statement:

Python code
if condition:
# code to execute if condition is True

Explanation:

 if ek keyword hai jo condition check karta hai.


 condition ek logical expression hoti hai, jo True ya False return karti hai.
 Agar condition True hai, toh indented code block (jo if ke neeche likha hota hai) execute hota hai.

Example:

Python code
x = 10

if x > 5:
print("x is greater than 5")

Step-by-step Explanation:

1. Variable Initialization: Humne variable x ko value 10 di hai.


2. Condition Check: if x > 5: yeh condition check kar raha hai ki x ki value 5 se badi hai ya nahi.
3. True Condition: Kyunki x ki value 10 hai, jo 5 se badi hai, yeh condition True hai.
4. Code Execution: Condition True hone par, jo code block if ke andar hai (print("x is greater than
5")), wo execute hoga aur output hoga:

Csharp code
x is greater than 5

Example with False Condition:

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.

Syntax of the if Statement


if expression:
# statement(s) to be executed

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.

Flow Diagram (Flowchart) of the if Statement

The below diagram shows flowchart of the if statement −

Example of Python if Statement

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

# Check he amount value


if amount > 1000:
discount = amount * 10 / 100

print("amount = ", amount - discount)

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...else Statement


In computer programming, the if statement is a conditional statement. It is used to
execute a block of code only when a specific condition is met. For example,
Suppose we need to assign different grades to students based on their scores.

1. If a student scores above 90, assign grade A


2. If a student scores above 75, assign grade B
3. If a student scores above 65, assign grade C
These conditional tasks can be achieved using the if statement.

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

Here, condition is a boolean expression, such as number > 5 , that evaluates to


either True or False .

 If condition evaluates to True , the body of the if statement is executed.


 If condition evaluates to False , the body of the if statement will be skipped from
execution.
Let's look at an example.

Working of if Statement

Example: Python if Statement


number = int(input('Enter a number: '))

# check if number is greater than 0


if number > 0:
print(f'{number} is a positive number.')

print('A statement outside the if statement.')


Run Code
Sample Output 1

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

# start of the if statement


if x != 0:
total += x
print(total)
# end of the if statement

print("This is always executed.")


Run Code

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.

Python if...else Statement


An if statement can have an optional else clause. The else statement executes if the
condition in the if statement evaluates to False .

Syntax

if condition:
# body of if statement
else:
# body of else statement

Here, if the condition inside the if statement evaluates to


 True - the body of if executes, and the body of else is skipped.
 False - the body of else executes, and the body of if is skipped
Let's look at an example.
Working of if…else Statement

Example: Python if…else Statement


number = int(input('Enter a number: '))

if number > 0:
print('Positive number')
else:
print('Not a positive number')

print('This statement always executes')


Run Code

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.

Python if…elif…else Statement


The if...else statement is used to execute a block of code among two alternatives.
However, if we need to make a choice between more than two alternatives, we use
the if...elif...else statement.
Syntax

if condition1:
# code block 1

elif condition2:
# code block 2

else:
# code block 3

Let's look at an example.


Working of if…elif…else Statement

Example: Python if…elif…else Statement


number = -5

if number > 0:
print('Positive number')

elif number < 0:


print('Negative number')

else:
print('Zero')

print('This statement is always executed')


Run Code

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.

Python Nested if Statements


It is possible to include an if statement inside another if statement. For example,
number = 5

# outer if statement
if number >= 0:
# inner if statement
if number == 0:
print('Number is 0')

# inner else statement


else:
print('Number is positive')

# outer else statement


else:
print('Number is negative')
Run Code

Output

Number is positive

Here's how this program works.


Working of Nested if Statement

You might also like