0% found this document useful (0 votes)
53 views9 pages

CH2 If Else

The document discusses Python's if, elif, and else conditional statements. Python uses indentation to define blocks of code for conditionally executing statements. The if statement executes code if a test expression is True. elif blocks check additional expressions if all previous tests are False. The else block runs if all tests are False. Nested if statements allow conditional logic within other conditional blocks.

Uploaded by

radivals
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views9 pages

CH2 If Else

The document discusses Python's if, elif, and else conditional statements. Python uses indentation to define blocks of code for conditionally executing statements. The if statement executes code if a test expression is True. elif blocks check additional expressions if all previous tests are False. The else block runs if all tests are False. Nested if statements allow conditional logic within other conditional blocks.

Uploaded by

radivals
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Python if...

else Statement

What is if...else statement in Python?


Decision making is required when we want to execute a code only if a certain condition is
satisfied.

The if...eiif...eise statement is used in Python for decision making.

Python if Statement Syntax

if test expression:
statement(s)
Here, the program evaluates the test expression and will execute statement(s) only if the te
expression is True .

If the test expression is False , the statement(s) is not executed.

In Python, the body of the if statement is indicated by the indentation. The body starts witt
indentation and the first unindented line marks the end.

Python interprets non-zero values as True . None and 0 are interpreted as False .

Python if Statement Flowchart

Test False
Expression

True

Body of if

Fig: Operation of if statement

Flowchart of if statement in Python programming

Example: Python if Statement

# If the number is positive, we print an appropriate message

num = 3
if num > 0 :
print ( num, " is a positive number " ) .
print ( " This is always printed " ).
num = -1
if num > 0:
print ( num, " is a positive number " ) .
print ( " This is also always printed " ).
Run Code

When you run the program, the output will be:

3 is a positive number
This is always printed
This is also always printed .

In the above example, num > 0 is the test expression.

The body of (i?) is executed only if this evaluates to True ] .

When the variable num is equal to 3, test expression is true and statements inside the body
if are executed.

If the variable [ num | is equal to -1, test expression is false and statements inside the body of
The printQ statement falls outside of the [if] block (unindented). Hence, it is executed
regardless of the test expression.

Python if...else Statement


Syntax of if...else

if test expression:
Body of if
else:
Body of else

The if..else statement evaluates test expression and will execute the body of if onlywl
the test condition is True .

If the condition is False , the body of else is executed. Indentation is used to separate the
blocks.

Python if..else Flowchart

Body of if Body of else

Fig: Operation ot if ...else statement

Flowchart of if...else statement in Python

Example of if...else
# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well .


# num = - 5
# num = 0

if num > = 0 :
print ( " Positive or Zero " )
else :
print ( " Negative number " )

Run Code

Output

Positive or Zero

In the above example, when [W| is equal to 3, the test expression is true and the body of [l
is executed and the [ body ] of else is skipped.

If num is equal to -5, the test expression is false and the body of else is executed and the
body of fi?) is skipped.

If num is equal to 0, the test expression is true and body of |TF| is executed and body of els
skipped.

Pvthon if...elif...else Statement


Syntax of if...elif...else

if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else

[
The elif is short for else if. It allows us to check for multiple expressions.

If the condition for (T?) is False], it checks the condition of the next elif block and so on.

If all the conditions are False , the body of else is executed.

Only one block among the several if...elif...else blocks is executed according to the
condition.

The (Tf) block can have only one|eise|block. But it can have multiple [ elif ] blocks.

Flowchart of if...elif...else

,
Test False
S' Expression
of if

True
Test False
Expression
Body of if | of elif

rue

Body of elif Body of else

i
Fig: Operation of if ...elif... pise statement

Flowchart of if...elif....else statement in Python


Example of if...elif...else

' ' ' In this program,


we check if the number is positive or
negative or zero and
display an appropriate message ' ' '

num = 3.4

# Try these two variations as well :


# num = 0
# num = - 4.5

if num > 0 :
print ( " Positive number " )
elif num == 0 :
print ( " Zero " )
else :
print ( " Negative number " )

Run Code

When variable num is positive, Positive number is printed.

If num is equal to 0, zero is printed.

If num is negative, Negative number is printed.

Python Nested if statements


We can have a if ...elif ...else : statement inside another if ...elif ... else ] statement. This
called nesting in computer programming.
Any number of these statements can be nested inside one another. Indentation is the only w
to figure out the level of nesting. They can get confusing, so they must be avoided unless
necessary.

Python Nested if Example

' ' ' I n this program, we input a number


check if the number is positive or
negative or zero and display
an appropriate message
This time we use nested if statement ' ' '

num = float ( input ( " Enter a number : " ) )


if num > = 0 :
if num == 0 :
print ( " Zero " )
else :
print ( " Positive number " )
else :
print ( " Negative number " )

Run Code

Output 1

Enter a number : 5
Positive number

Output 2

Enter a number : -1
Negative number
Enter a number: 0
Zero

You might also like