Introduction
To perform decision making in Python, we will use the conditional statement if/elif/else. Before we perform a decision, we need to understand the concept of a Boolean statement.
How to make a Boolean statement: relations between conditions
A Boolean statement is a result of any operation that results in a True or False value.
We use the relational operators in Python, to perform these actions. To perform a decision, we need at least two values to make a comparison or a current state of a control variable. To make this possible we can use the six relational operators
>
<
>=
<=
==
!=
For Example, if we run the following relational operations using the Python interpreter, we get the following:
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 5 > 2
True
>>> 5 < 2
False
>>> 5 >= 2
True
>>> 5 <= 2
False
>>> 5 == 2
False
>>> 5 != 2
True
Delimitation, Indentation, and Scoping
Python uses the white space as a delimiter for the scope for any structure, also known as indentation. Be careful how you use those delimiters because Python may generate an error if one space is missing. You can also use the Tab as a delimiter, but we recommend to use spacing with the white space.
Structure of the if:
The if-statement structure requires a Boolean expression to make a decision. If the Boolean expression is True, then it will execute a set of statements. In case the Boolean expression is False, in the if-statement, then the execution process will “jump” into the else statement. This is the structure:
if():
statement
elif():
statement
else:
statement
Check Point
Task 1: Write a script called task01.py that requests the user to enter two numbers, print the largest number that was entered by the user. In case both numbers are the same, print: “duplication of values”. E.g., your program shall run with the following prompts:
servin@linux-csit-club:~/pythonscripts$ python3 task01.py
enter a value: [5]
enter a value: [5]
Duplication of number
servin@linux-csit-club:~/pythonscripts$ python3 task01.py
enter a value: [5]
enter a value: [7]
The largest value is: 7
servin@linux-csit-club:~/pythonscripts$ python3 task01.py
enter a value: [7]
enter a value: [5]
The largest value is: 7
Task 2: Modify Task 1. Instead of asking the user to use the arguments to perform the actions. E.g., your script shall be called through the terminal as follows:
servin@linux-csit-club:~/pythonscripts$ python3 task01.py 5 5
Duplication of number
servin@linux-csit-club:~/pythonscripts$ python3 task01.py 5 7
The largest value is: 7
servin@linux-csit-club:~/pythonscripts$ python3 task01.py 7 5
The largest value is: 7
Task 3: Modify Task 2 to check if there are enough arguments to perform the action. Your program shall provide an error message in case there are not enough arguments provided.
Poner output
Task 4: Python owns Built-in functions that permit to simplify work done in programming. Use the function max() that extracts the largest value between two numbers.
poner output
Task 5: (optional) Modify Task 3, to extend the program to provide additional information in the arguments other than only 2.
poner output
// put solutions
Decision and Program Flow
Logical Operators
Logical operators are used to combining, disjoining or negating Boolean expressions. Python supports the logical and (and), logical or (or), and the not (not). To understand these operators, we must understand the truth tables to visualize the Boolean expressions.
Truth Table: Logical AND
The purpose is to make conjunctions between two Boolean statements. Both statements must be true to generate a True statement.
// put table
Truth Table: Logical OR
The purpose is to disjoint two Boolean statements. At least one statement must be true to generate a true statement using logical or.
// put table
The logical or is often used to have at least one value to proceed. It is also to discard values or ranges of numbers. For example, if we want to discharge all numbers that are not between 0 and 10, we can use the logic or to reduce the search.
Truth Table: Logical Not
Logical not, basically “flips” the boolean value to its opposite by using the not operator.
// put table
Example
x = True
y = False
# Output: x and y is False
print('x and y is',x and y)
# Output: x or y is True
print('x or y is',x or y)
# Output: not x is False
print('not x is',not x)