Menu Close

Articles

Python: decisions

The most common and maybe the most important statement in programming is the decision statement. A decision is based on a “conditional expression” that is also known as “logical expression”. You must understand “propositional logic” to be able to make correct conditions.

Decisions statement

Decision is represented in Python using the if  keyword followed by a conditional expression then followed by “:”. In the example below we use multiple conditions to make decisions:

Note: This is also called multi-path decision statement because there are several blocks that can be executed in different cases. The default case else: do not have a condition and will execute if no other case condition is satisfied.

This program is available on-line: repl.it decision statement

Running program

If you save this code as a file: decision.py and run you will have:

>f:\GitHub\sandbox\snippets>python decision.py

>f:\GitHub\sandbox\snippets>python decision.py

>f:\GitHub\sandbox\snippets>python decision.py

Indentation

Python is using mandatory indentation of 4 spaces.  To exit from decision statement you can start a new statement at the beginning of next row. This will allow the interpreter to distinguish between a block of code and other statements.

Note: A good practice is to use “pass” statement to mark the end of block, and you can add a line comment to signal the end of block. This way you can make your code more readable.

Logical expression

The decision statement is using one or several conditions. A condition is a logical expression or a Boolean expression that returns True or False. In Python an expression that do not return 0 or “” or None it is considered True. You can use decisions to check if an object is initialized or a number has value not equal to 0.

Examples:

Will print:

Notes:

  • I’m using assignment statement (single equal) “=” to make a new variable x,
  • Variable x has initial value x == 0. Logical expression “x” is evaluated to False by “if”,

Logical True and False

Logical values in Python are True = 1 and False ==0. These values are constant and can be used in conditions to make decisions. You can compare a value with a logical constant or assign a value to a variable.

Relation Operators:

To compare numbers we use relation operators. Next expressions are all true:

  • Is equal: (1 == 1),
  • Is not equal:  (1 != 2),
  • Greater then: (2 > 1),
  • Less then:  (1 < 2),
  • Greater then or equal to: (2 >= 2),
  • Less the or equal to:  (0 <= 1),
  • Is the same object:  given a = b = 1; (a is b),
  • Member of collection:  given a = 1; (a in [1,2,3]).

Note: operator  “is” demonstrate that a is actually same as b. If we change ones value, the other will have the same new value. So a, b are pointing to same memory location. In mathematics we use symbol “≡” for this operation: identical.

Peculiar value: None

There is a peculiar value in Python: “None” this value is not True and not False however the logical operators work with this value as if is False. Also “if” statement evaluate None as False.

Note: A variable that has value None exist, you do not get an error if you use it in expressions. Usually an expression that use a None value will have as result None. This is not printable value though.

Logical Operators:

To make other decisions we use keywords:

  • or
  • and
  • not
  • xor

For example:

Note: operator xor is not present in Python but it can be simulated using formula:

The table of truth: 

You may have seen this before if you took the CSE: Engineering class. This table help you understand better all possible combinations of values and the result yield by logical operators {“or”, “and”, “xor”}.

PQP or QP and QP xor Q
FalseFalseFalseFalseFalse
TrueFalseTrueFalseTrue
FalseTrueTrueFalseTrue
TrueTrueTrueTrueFalse
NoneNoneNoneNoneNone
NoneTrueTrueFalseTrue
TrueNoneTrueFalseTrue

 

Read next: Repetition