1.8 conditional statements
Contents
Note
Click here to download the full example code or to run this example in your browser via Binder
1.8 conditional statements#
Important
This lesson is still under development.
The basic syntax of if statement in python is:
if (condition):
do something
chor
chor
chor
Similarly the syntax for if and elif statement is
if (condition):
do something
elif (condition):
do something
else:
do something
edhi is not chor
elif
vs multiple if
#
Multiple ifs means, the all the ifs will be checked, while with elif, the code will stop if one of the if is True.
You are a child
The age must be a positive integer!
You are a child
in
#
We can use in statement to compare a variable against multiple variables.
zardari was democratic thug
Zia was a non-democratic is chor
comparing numbers#
democratic thug ruled Pakistan
year = 2012
if 2007>year>2000:
print('Non-democratic thug ruled Pakistan')
elif 2020>year>=2007:
if 2013>year>=2007:
print('democratic thug zardari ruled Pakistan')
elif 2018>year>=2013:
print('democratic thug Nawaz ruled Pakistan')
else:
print('It seems the ruler is incapable')
else:
print('not considering')
democratic thug zardari ruled Pakistan
One liner
This is independence day not partition day.
oil = True
us_presence = 1 if oil else 0
print(us_presence)
1
We can use such one liners to set default values to a variable.
human = {"arms": 2,
"legs": 2,
"head": 1}
default_age = 14
age = human["age"] if "age" in human else default_age
print(age)
14
This is Pakistan
if not
vs !=
#
Inner working is different but the output is same however not
is preferred.
no jumma prayer
no jumma prayer
if day == 'Friday':
pass
else:
print("no jumma prayer")
no jumma prayer
any
vs all
#
go ahead
Total running time of the script: ( 0 minutes 0.007 seconds)