#7 Conditional Statement in Python

Why Auto driver that python makes can make an adjustment to an unpredictable situation. For example, if a car faces another stopped car in front of it, the auto driver can also stop accordingly. That is amazing.

Python can do that, because python have conditional statement or If statement.

The conditional statement is useful for creating a decision structure. Decision depends on condition. This allows a program to create more than one scenario. The if statement has an instruction for one ,, and another different instruction for another condition.

If statement have component, boolean and relational expression to check condition. Beside that, this if statement also have instruction to execute if condition as request.

Boolean and Relational Expression

A Boolean value has two value, True or False. Statement that use this value is Check Statement. Check Statement is statement that evaluate whether the statement true or false. In English language, is A is same with B? in Python language, A==B. faces statement tests an expression to determine whether it is true or false.
Boolean value is named after George Boole formulate some rules for reasoning about and combining this binary value (True and False). It is suit for computer that have binary value, 0 and 1.


We can get list of boolean value in Python by type : dir(True).

>>> dir(True)
       ['abs', 'add', 'and', 'class', 'cmp', 'coerce', 'delattr', 'div', 'divmod', 'doc', 'float', 'floordiv', 'format', 'getattribute', 'getnewargs', 'hash', 'hex', 'index',
       'init', 'int', 'invert', 'long', 'lshift', 'mod', 'mul', 'neg', 'new', 'nonzero', 'oct', 'or', 'pos', 'pow', 'radd', 'rand', 'rdiv', 'rdivmod', 'reduce', 'reduce_ ex', 'repr', 'rfloordiv', 'rlshift', 'rmod', 'rmul', 'ror', 'rpow', 'rrshift', 'rshift', 'rsub', 'rtruediv', 'rxor', 'setattr', 'sizeof', 'str', 'sub', 'subclasshook', '_
       truediv_', 'trunc', 'xor', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real'] "
      

That is a list of several function related to boolean type data.

Another alternative, we can read the documentation by type “help(True) ” or “help(False) in python command line.

>>> help(True)
       Help on bool object:class bool(int)
  |  bool(x) -> bool
  |
  |  Returns True when the argument x is true, False otherwise.
  |  The builtins True and False are the only two instances of the class bool.
  |  The class bool is a subclass of the class int, and cannot be subclassed.
  |
  |  Method resolution order:
  |      bool
  |      int
  |      object"
...
 l

Usually, Boolean operator use equal (==), greater(>) and less to determine whether a specific relationship True or False.

SymbolMeaning
> Greater than
>= Greater than or equal to
< Less than
<=Less than or equal to
== Equal to
!=Not equal to

Boolean Value in Conditional Expression

How boolean value in conditional expression. In Python language, the syntax of Conditional is like this

>>> If condition :
       statement

Condition in the syntax is boolean statement. That is something that computer can evaluate whether that is True or False. True or False in the term of meet condition. That is not about morality or epistemology.

In flowchart, Conditional Statement use symbol like this.

Logical Operator


Python also accommodate more complicated expression in boolean mode. There are logical operator to combine simpler boolean operator. The logical operator is” and”, “or” and “not”.

“and” Operator


The and and or combine two boolean expression. “And” operator evaluate expression as true only if the both expression is true. The truth table is like this.

XYX and Y
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

“or” Operator

or operator evaluate two statement. Then if one or more statement is true, the or operator return to True.

XYX or Y
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

“not” Operator

not operator can apply to one statement or more.
if the not operator apply to True value, it flowcharts to False.

Xnot X
TrueFalse
FalseTrue

Conditional Execution

A conditional statement has a ability to check condition and execute instruction or statement based on condition.
The syntax is like below.

>>> if True: # This is always True,
        pass # so this is always executed, but it does nothing
>>>  else:
        pass"

Chained conditionals/ nested decision

if we need more than two condition and more than two instruction, python also accommodate. Chained or nested decision structure check more than one condition.

Notation of chained conditional statement is like this.


That is the conditional statement inside another conditional statement.