Professional Documents
Culture Documents
Statement-2: Simple If
Statement-2: Simple If
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
>>>
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}')