If Statement

If Statement:
⦁ this is a very important statement used to check the condition and accordingly execute a set of statements, based on whether the condition is true or false.
⦁ if the condition specified in the if statement is false then the statement of else will be executed

If-Else
If-Else
SYNTAX of if-else statement
if condition

-
statement 1;
-
-

 else

-
-
statement 2;
-
-
1.For example:
cars=['audi','bmw','subaru','toyota']
if car=='bmw'
print(car.upper())
else:
print(car.title())
OUTPUT:
BMW

2.Display even number 
num = int(input("enter the number:")) 
if num%2 == 0: 
print("Number is even") 
 
Output:
enter the number:10
Number is even

3.check whether the person is eligible for vote or not ?
 age = int (input("Enter your age: ")) 
if age>=18: 
print("You are eligible to vote !!"); 
else: 
print("Sorry! you have to wait !!"); 
 
Output:
Enter your age: 20
You are eligible to vote !!

4.example for if statement
requested_topping ='mushrooms'
if 
requested_topping!='anchovies':
print("Hold the anchovies!")

OUTPUT:
Hold the anchovies!

explaination:
the line at compares the value of requested_topping to the value
'anchovies'.If these two values do not match,python returns True and executes the code following the if statement .If the two values match,python return False and does not run the code following the if statement.Because the value of requested_topping is not 'anchovies',the print statement is executed:
Translate »