PYTHON MADE SIMPLE (2015)

Chapter 3: Boolean’s in Python

Simple Conditions

Boolean is a test or conditional system first developed by a mathematician called George Boole. This has been derived and used in Python. It is set as a condition or a test. In this chapter, simple tests will be done to show comparison of values. This will also be translated to ensure that you comprehend every aspect of the comparison in the handling of the arithmetic in Python. Using the following examples, try them out in python and get to know what happens with each one of them in Python Shell.

The conditions as used in this case can either be expressed as true or false. They are also not expressed in quotes because that will change their meaning and operations completely. There is a perfect example for the use of Boolean values developed in the 19th century.

The application of Booleans in python is the short form of the name to bool. This is applied in tests results for the use of expressions: True or False conditions or tests.

This is the simple history, how the Boolean’s started and what they represent in Python.

Simple if, else, elif Statements use and application in Python

If statement

There are various statements that can be use in Python. They are a perfect set of conditional expressions which  can be used to express logic and provide a response in the form of an output in Python.

For example, try out this short code and save it as luggage.py. Run it twice or thrice but changing the values of the input 30, 40 and then 56.

Note that the input determines the nature of the result.

I am providing you with screen shots so that in the process of writing the codes on your program, you also learn and see how the programs is supposed to be indented and many other applications covered previously.

The second line of the code is an if statement. It can be interpreted to be like a story being read in an English book. When it is true that the weight of the luggage is greater than 50, then a statement of the extra charge is made. But, when it is not true and the value is lower than 50, then don’t do the indented part of the code: skip the indented parts of printing the charge value for the extra luggage.

In every of the events, whenever the if code has been executed. It makes a decision on whether to proceed with the indented parts or not.

Hence, the next line will be printing the statement, “thank you for doing business”

Overally, the if statement general Python syntax is expressed as follows

if condition :

Statement (s) block

What this represents is that: If the condition will be in deed true as stated, then proceed to the indented statements. But, if the condition set is not applicable in this case and not true, then skip the indented statements.

For more practice, let us consider a teller machine in a supermarket which executes the following code in every transaction is conducts:

This is an if expression with a block of indented statements. The block may have a single or several lines of statements. The main meaning of the code is that whenever the balance of the account goes to negative. It is returned to 0 through a series of other steps which will be able to transfer some some from the back up account to ensure the account is always at 0 (zero).

This means that when the condition is true, then it has a choice of doing something. But, when the condition is false, then it has nothing to do and only say “Thank you and Good Bye”

if-else Statements

The general syntax for this statement is expressed as follows:

If expression:

              Statement (s)

else:

              Statement (s)

The statement blocks can have as many numbers of statements as they can be included in the program.

Let’s try to understand the if ---else statements by looking at the following example:

Create a program below in your python and name it environmental_cloths.py. Then run the program and see the results. You can also play around with the input values as 60 and 85 respectively to see what the results will look like.

You are expected to find different results with different inputs. Here is the code for you application.

Just like I have expressed in the previous example, this codes can be read like an English story. Other words such as otherwise can be used instead of the word else, however, we use shorter words in Python to reduce the bulk of wording in programming.

Note also, that the code has two indented blocks of code. The after the if and after the else. The first block is the if statement. The indentation is executed only of the if is true otherwise, nothing happens. Next, when it is false, the else is executed as well. Hence, when using the if.. else statement, only one of the headings can be executed at any one, when if is true, else indented block is not executed, but when if is false, then the else indented block is executed.