L1 Conditional Statements
L1 Conditional Statements
if expression
Statement
else
Statement
1
#
2
#Example file for working with conditional statement
3
#
4
def main():
5
x,y =2,8
6
7
if(x < y):
8
st= "x is less than y"
9
print(st)
10
11
if __name__ == "__main__":
12
main()
Run
Example:
This code is editable. Click Run to Execute
1
#
2
#Example file for working with conditional statement
3
#
4
def main():
5
x,y =8,4
6
7
if(x < y):
8
st= "x is less than y"
9
else:
10
st= "x is greater than y"
11
print (st)
12
13
if __name__ == "__main__":
14
main()
Run
Here both the variables are same (8,8) and the program output is “x is greater
than y”, which is WRONG. This is because it checks the first condition (if condition
in Python), and if it fails, then it prints out the second condition (else condition) as
default. In next step, we will see how we can correct this error.
This code is editable. Click Run to Execute
1
#
2
#Example file for working with conditional statement
3
#
4
def main():
5
x,y =8,8
6
7
if(x < y):
8
st= "x is less than y"
9
else:
10
st= "x is greater than y"
11
print(st)
12
13
if __name__ == "__main__":
14
main()
Run
Example
This code is editable. Click Run to Execute
1
#
2
#Example file for working with conditional statement
3
#
4
def main():
5
x,y =8,8
6
7
if(x < y):
8
st= "x is less than y"
9
10
elif (x == y):
11
st= "x is same as y"
12
13
else:
14
st="x is greater than y"
15
print(st)
16
17
if __name__ == "__main__":
18
main()
Run
Syntax
A If B else C
Example:
2
def main():
3
x,y = 10,8
4
st = "x is less than y" if (x < y) else "x is greater than or equal to y"
5
print(st)
6
7
if __name__ == "__main__":
8
main()
Run
Instead of writing long code for conditional statements, Python gives you
the freedom to write code in a short and concise way.
1
total = 100
2
#country = "US"
3
country = "AU"
4
if country == "US":
5
if total <= 50:
6
print("Shipping Cost is $50")
7
elif total <= 100:
8
print("Shipping Cost is $25")
9
elif total <= 150:
10
print("Shipping Costs $5")
11
else:
12
print("FREE")
13
if country == "AU":
14
if total <= 50:
15
print("Shipping Cost is $100")
16
else:
17
print("FREE")
Run
Uncomment Line 2 in above code and comment Line 3 and run the code again
Example
function(argument){
switch(argument) {
case 0:
return "This is Case Zero";
case 1:
return " This is Case One";
case 2:
return " This is Case Two ";
default:
return "nothing";
};
};
def SwitchExample(argument):
switcher = {
0: " This is Case Zero ",
1: " This is Case One ",
2: " This is Case Two ",
}
return switcher.get(argument, "nothing")
if __name__ == "__main__":
argument = 1
print (SwitchExample(argument))
Python 2 Example
Above codes are Python 3 examples, If you want to run in Python 2 please consider
following code.
# If Statement
#Example file for working with conditional statement
#
def main():
x,y =2,8
if __name__ == "__main__":
main()
if __name__ == "__main__":
main()
# When "else condition" does not work
#Example file for working with conditional statement
#
def main():
x,y =8,8
if __name__ == "__main__":
main()
elif (x == y):
st= "x is same as y"
else:
st="x is greater than y"
print st
if __name__ == "__main__":
main()
if __name__ == "__main__":
main()
# Nested IF Statement
total = 100
#country = "US"
country = "AU"
if country == "US":
if total <= 50:
print "Shipping Cost is $50"
elif total <= 100:
print "Shipping Cost is $25"
elif total <= 150:
print "Shipping Costs $5"
else:
print "FREE"
if country == "AU":
if total <= 50:
print "Shipping Cost is $100"
else:
print "FREE"
#Switch Statement
def SwitchExample(argument):
switcher = {
0: " This is Case Zero ",
1: " This is Case One ",
2: " This is Case Two ",
}
return switcher.get(argument, "nothing")
if __name__ == "__main__":
argument = 1
print SwitchExample(argument)
Summary:
A conditional statement in Python is handled by if statements and we saw various
other ways we can use conditional statements like Python if else over here.
“if condition” – It is used when you need to print out the result when one of
the conditions is true or false.
“else condition”- it is used when you want to print out the statement when
your one condition fails to meet the requirement
“elif condition” – It is used when you have third possibility as the outcome.
You can use multiple elif conditions to check for 4th,5th,6th possibilities in your
code
We can use minimal code to execute conditional statements by declaring all
condition in single statement to run the code
Python If Statement can be nested