Python Decision Making
Decision-making is the way where a program shift execution from one side to another based on a condition if the condition evaluates to True then the next statement(s) will be executed other wise if there is any else statement its block(or body) will be executed instead.
True vs False vs not
True
All non 'None' values, not empty string, non-zero numbers, whitspace-only strings, and not False values are True
False
All None, empty string, False values are considered False
not
not keyword is used to counter the condition value:
#!/usr/bin/env python if not True == False: print("not True equals False") if not False == True: print("not False equals True")
Output
not True equals False not False equals True
.
-
if statements
An if statement consists of a boolean expression followed by one or more statements. -
if...else statements
An if statement can be followed by an optional else statement, which executes when the boolean expression is False. -
Nested if statements
You may use one if or else if statement inside another if or else if statement(s). however keeping decision flow flat and simple is encouraged
#!/usr/bin/env python home = 'coderme.com' if 'coderme' in home: print("Home contains 'coderme'")
Output
Home contains 'coderme'