0% found this document useful (0 votes)
36 views

Conditionalstatements

Conditional statements in Python perform different computations or actions depending on whether a given condition evaluates to true or false. There are three main types of conditional statements: if statements execute code if a condition is true; if-else statements execute one block of code if a condition is true and another if it is false; and if-elif-else statements allow evaluating multiple conditions in order and executing the block for the first true condition. Nested if statements allow placing if statements inside other if statements to create more complex conditional logic.

Uploaded by

Sudheer Mamidala
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)
36 views

Conditionalstatements

Conditional statements in Python perform different computations or actions depending on whether a given condition evaluates to true or false. There are three main types of conditional statements: if statements execute code if a condition is true; if-else statements execute one block of code if a condition is true and another if it is false; and if-elif-else statements allow evaluating multiple conditions in order and executing the block for the first true condition. Nested if statements allow placing if statements inside other if statements to create more complex conditional logic.

Uploaded by

Sudheer Mamidala
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/ 3

Python Conditional Statements

Conditional Statements are features of a programming language, which perform different


computations or actions depending on whether the given condition evaluates to true or false.
Conditional statements in python are of 3 types

 If statement

 If else statement

 If elif statement

 Nested if else

1. If Statement : if Statement is used to run a statement conditionally i.e. if given


condition is true then only the statement given in if block will be executed.

if <condition>:
<if statement block >

For example consider the code given below


if (percentage > 33):
print (“Pass”)

Explaination : In the above code if value of percentage is above 33 then only


the message “Pass” will be printed.

2. If else Statement In the case of if else statement If given condition is true then the
statement given in if block will be executed otherwise(else) the statements written in
else block will be executed

if <condition>:
<if statement block >
else:
<else statement block>
For example consider the code given below
if (percentage > 33):
print (“Pass”)
else:
print(“Fail”)
Explaination : In the above code if value of percentage is above 33 then only
the message “Pass” will be printed otherwise it will print “Fail”
3. If elif Statement if elif is used for execution OF STATEMENTS based on several
alternatives. Here we use one or more elif (short form of else if) clauses. Python
evaluates each condition in turn and executes the statements corresponding to the first
if that is true. If none of the expressions are true, and an else clause will be executed

Syntax:-
if <condition>:
<statement(s)>
elif <condition>:
<statement(s)>
.
.
else:
<statement(s)>
Explaination : In the above code if value of percentage is above 33 then
only the message “Pass” will be printed otherwise it will print “Fail”

Example:

If (percentage >90):
Print(“Outstanding”)
elif (percentage >80):
print (“Excellent”)
elif (percentage >70):
print (“VeryGood”)
elif (percentage >60):
print (“Good”)
elif (percentage >33):
print (“Pass”)
else
print(“Fail”)

Explaination : In the above code


if value of percentage is above 90 then it will print “Outstanding”
if value of percentage is above 80 then it will print “Excellent”
if value of percentage is above 70 then it will print “Very Good”
if value of percentage is above 60 then it will print “Good”
if value of percentage is above 80 then it will print “Pass”
if no condition is true then it will print “Fail”
In above code only 1 condition can be true at a time if no condition is true then else statement will be
executed
4. Nested If else Statement A nested if is an if statement that is the target of another if
statement. Nested if statement means an if statement within another if statement.
Syntax:-
if (<condition1>):
statement(s)
if (<condition2>):
statement(s)
else
else:
if (<condition3>):
statement(s)
else
Statement(s)
# Example
if color =”red”:
if item=”fruit”:
print(“ It is an Apple”)
else :
print(“It may be Tomato or Rose”)
else:
if color=”Yellow”:
print(“It is a Banana”)
else
print(“It may be corn or Marigold ”)

OR
if color =”red”:
if item=”fruit”:
print(“ It is an Apple”)
else :
print(“It may be Tomato or Rose”)
elif color=”Yellow”:
print(“It is a Banana”)
else
print(“It may be corn or Marigold ”)

You might also like