Tutorials
Search…
Lesson 7. Making Decisions
When I worked at Accenture, I was embedded with another company's software engineering team. The team had a funny saying. "Put some smarts in it." That's a reference to creating business logic around whatever ever we were attempting to automate.
The whole bloody point of writing software in the first place is to automate business processes. However, business is complex. If you have not started your professional career, you may not be aware just how complex business processes can be or how ridiculous they can get.
Most business processes are filled with all kinds of exceptions to rules. As you are automating processes, you have to figure out what to do when your software has to do something different based different inputs into the software. You have to “put some smarts in it.”
That is what we are going to do in this section. We are going to learn to write software that is flexible enough to perform different functions when the situation calls for it.
Python has numerous tools that allow you to build flexible software. Most of those tools require you to use Boolean logic to test rather or not some condition is true.
Quick Overview Of Boolean Logic (Optional)
If you are transitioning to Python from another language, then you can skip this section.
It occurred to me that we’re on lesson seven, and I have been using a lot of terms that you may or may not be familiar with. It is not practical to teach you basic computer science in a coding tutorial. However, I can spend a few minutes explaining Boolean logic because it is fundamental to developing business logic.
The way computers currently work is that everything inside the box exist in one of two states. It is either on or it is off. Currently, there is no in between. Open up the case on your computer and all that whiz bang stuff that you are looking at breaks down to something being on or off.
In the computer, this on or off state is represented by the numbers 1 and 0 with 1 representing on and 0 representing off. On and off can also be represented by the Boolean data type values of True and False. In the lecture on data types, I already showed you how if you cast the Boolean True to an integer value, it will evaluate to 1. The reason why is the binary nature of the current state of computer engineering.
Synonyms for on
Synonyms for off
True
False
1
0
Comparison Operators
When you are testing values and conditions, you are testing rather or not the current state of the value or condition is either true or false. That is fairly straight forward.
Five is greater than one. This is true.
Five is less than one. This is false.
When you are writing business logic, you can write instructions based on rather or not the result of the evaluation of a condition is true or not.
If (some condition) is true  Do a series of task. If (some condition) is false  Do a different series of task.
You evaluate conditions using the comparison operators in the table below.
Operator
Description
==
Equal to
!=
Not equal to
>
Greater than
<
Less than
>=
Greater that or equal to
<=
less than or equal to
The real fun of Boolean logic begins when you have to start combining conditions or test. Now you have to decide how much or how little of the combined condition test has to be true in order for the entire thing to evaluate to true. Five is greater than one AND ten is greater than five. This is true. Five is greater than one OR five is greater than ten. This is also true. The second statement is true because you told the computer that it was ok if only one statement was true by using the “or” keyword. When it comes to Boolean logic, your basic options are:
  • In order for the condition test to evaluate to true, the entire condition test has to be true.
  • In order for the condition test to be true, at least one component has to be true.
  • In order for the condition test to be true, the entire condition has to be false.
When I say these are your basic options, I am not kidding. Condition test can be incredibly complex. Complex enough that it can give you a headache trying to figure out if something should evaluate to true or false. In the strongest terms possible, I recommend that you do everything you can to keep your condition test simple and straight forward. You combine conditions by using the operators below. Logical Operators
Operator
Description
and
Both values have to be true
or
At least one value has to be true
not
Inverts the returned Boolean value

Examples

Example #1: Using Comparison And Logic Operators
Below we are going to do some condition test and then cast the results to the Boolean data type so you can see whether or not the condition evaluates to True or False. We will start simple then get increasingly ridiculous.
bool(5>2)
bool((5>2) and (10<5))
bool(not((5>2) and (10<5)))

Now you try it!

Don't copy and past. Type the code yourself!
Copy link
On this page
Examples
Now you try it!