You are on page 1of 4

If Statement in Python

● They are conditional statements in python.


● It implements selection constructs(decision constructs).
● It is a program which has only choice or condition.
● If the condition evaluates to true, it carries out some instructions.
● Syntax =

if <conditional>:

statements
Change the example
● Example:
1. If 6:
print("Conditional True")

● 2. If -3:
print ("Conditional True")

if-else Statement

● if-else statement is a form of if statement which is required to


select one statement for processing on the basis of a
condition,if-else statement is to be used.
● It is a program which has two choices or conditions.
Delete the 3rd point
● Syntax = if<conditional>:
statement
● if-else statement executes some code if the test expression
is true ( nonzero) and some other code if the test expression
is false.
i.e,
(s) when condition is true
else:
statement (s) when condition is false
Change the example
● Example:
marks= int(input("Enter the Marks")

if marks>= 80
print("Excellent")
else:
print(" Well Done !!!")

if-elif statement

● if - elif statement is required to select one statement for


processing on the basis of a condition.
● It is a program that has more than two choices. For example;
The number is positive, negative or zero.
● Syntax:
if <condition 1>:
statement(s) when condition 1 is true

elif<condition2>:
statement(s)
when condition 2 is true

● elif<condition 3>:
statement(s)
when condition 3 is true

else
● Example:
a= int(input("Enter the number")
if a==10:
print("a is equal to 10")
elif a==20:
print ("a is equal to 20")
elif a==30:
print ("a is equal to 30")
else:
print ("Again Give the number")

Nested Statement

● The nested if…else statement allows us to check for


multiple test expressions and execute different codes for
more than two conditions.
● When an if condition contains another if condition inside a
block, it is known as nested if.
● Syntax:
if (condition)
statements
elif (condition)
statements
else:
statements
● For Example:

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


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

Output
Enter a number:5
Positive number

You might also like