Python Decision Making

Published:2021-11-28
Share:

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'

Read also

Python Loops

Loops allow you to execute a statement or group of statements repeatedly. Loop Architecture Python programming language has two typed of loop while loop Repeats a statement or group of statements while a given condition is True . It tests the condition before executing the loop body. for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.   Loop Control The Loop control statements change the execution from its ...

Iptables For Securing Webservers

Iptables and ip6tables used to build firewall on linux servers, in this article we'll go over some rules that could help securing your webserver by restricting internet traffic to your server, if you're looking for openbsd pf  then you may check  openbsd pf rules for webservers .     Whats iptables? Is a collection of tables which contains chains in which we define rules for incoming/outgoing traffic, each rule checked one by one within a certain chain till a matc...