Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Simple if

If without else is called simple if.


Syntax:
If <condition>/<Boolean expression>:
Statement-1
Statement-2
Statement-3

Note: we cannot define empty block in python. Block must have at least one
statement.

if 10>5:
print("Hello")

pass
pass is a null operation — when it is executed, nothing happens. It is useful as a
placeholder when a statement is required syntactically, but no code needs to be
executed.

Example:
if 10>5:
pass

Syntax-2:
If..else
This syntax is having two blocks.
1. If block
2. Else block
If block is executed when condition is True.
Else block is executed when condition is False

if <condition>:
Statement-1
Statement-2
else:
Statement-3
Statement-4
Statement-5
Statement-6
If condition is True it execute
statement-1,statement-2 and
statement-5,statement-6
If condition is False it execute
statement-3 ,statement-4 and
statement-5,statement-6

Example:
# write a program to find input user is elg to vote
name=input("Enter Name")
age=int(input("Enter age"))
if age>=18:
print(f'{name} your elg to vote')
else:
print(f'{name} your not elg to vote')
Output:
Enter Namenaresh
Enter age30
naresh your elg to vote
>>>
=============== RESTART: C:/Users/user/Desktop/Python11am/py29.py
==============
Enter Namesuresh
Enter age12
suresh your not elg to vote

>>>

Example:
if True:
print("ABC")
print("XYZ")
else:
print("PQR")

Output:
ABC
XYZ
>>>
# write a program a program to find input character is vowel or not
ch=input("Enter any character")
if ch in "aeiouAEIOU":
print("vowel")
else:
print("not vowel")

Output:
Enter any charactera
vowel
>>>

#write a program to find input number is divisible with 7 or not

num=int(input("Enter any number"))


if num%7==0:
print(f'{num} is divisible with 7')
else:
print(f'{num} is not divisible with 7')
Output:
Enter any number14
14 is divisible with 7
>>>
=============== RESTART: C:/Users/user/Desktop/Python11am/py32.py
==============
Enter any number12
12 is not divisible with 7
>>>

Syntax-3 if..elif..else (if..else.. ladder)


If <condition1>:
Statement-1
elif <condition2>:
statement-2
elif <condition3>:
statement-3
else:
statement-4
statement-5

if condition1 is True, it execute


statement-1,statement-5
if condition1 is False, condition2 is
True, it execute statement-2 and
statement-5
if condition1,condition2 are False and
condition3 is True, it execute statement-
3 and statement-5
if condition1,condition2,condition3 are
False it execute statement-4 and
statement-5

Problem statement
https://www.hackerrank.com/challenges/py-if-else/problem?
isFullScreen=false

n=int(input())
if n%2!=0:
    print("Weird")
elif (n>=2 and n<=5):
    print("Not Weird")
elif (n>=6 and n<=20):
    print("Weird")
elif n>20:
    print("Not Weird")

https://www.codechef.com/problems/HS08TEST
t,bal=map(float,input().split(" "))
if t%5!=0:
print(f'{bal:.2f}')
elif t>bal:
print(f'{bal:.2f}')
else:
bal=bal-(t+0.50)
print(f'{bal:.2f}')

You might also like