Boolean’s and Conditional expressions - PYTHON MADE SIMPLE (2015)


Chapter 4: Boolean’s and Conditional expressions

Most of the mathematical comparisons can be made in python. However, they lack the symbolism due to the absence of the standard keys on almost all the keyboards in existence

Below is a representative coding for arithmetic symbols used in mathematics and python.


Math Symbol

Python Symbols

Less than



Greater than



Less than or equal


Greater than or equal





Not equal


Note: There need not be space between the symbols when used in python.

Also note that a single equals sign is used for assigning values in Python as previously covered. Hence, when used in tests, a second equals sign needs to be used in Python. It is annoying, is it?

Therefore, when using a single equal sign for testing for equality in python and when not intending to use it for assigning values to a variable; this will automatically generate an error.

Testing for equality never assign values and hence never need you to place a variable on the left of the double equal symbols. Most of the expressions may be tested for equality or inequality (!=). Hence, no numbers may be required,.

Using the following values, try to make prediction of the results. Each line can be tested on its own in python shell.

x = 6


x == 6

x == 8


x != 8

x = 9

4 == x

4 != x

'hi' == 'h' + 'i'

'HI' != 'hi'

[1, 2] != [2, 1]

Note: Assignment cannot be made using an equality check. Strings are always case sensitive. Moreover, order is very important in all lists.

Using this example, try it out in Python Shell.

'b' > 6

Often, when the comparison is not what is expected or makes sense, an exception is made for the code.

The inexactness in Python is not acceptable especially when using float values and strings. The following examples can also be used: 0.3 + 0.4 to be equal to 0.7. Try creating a code that will be able to express the following expression in the Python Shell.

A perfect practical example can be expressed using the following pay code for normal and overtime. Using an individual’s work schedule for working hours for the week and regular wage for every hour worked for the week and with consideration of the hourly overtime.

When the hours worked are over 40, then they are considered as overtime. They are then paid double the normal rate. This can allow us to make a python code that will be able to do the calculation for the money that is payable per employee.

The set up for the function can be expressed as follows:

Try to read the set up for the function as expressed below:

This code will be able to handle two cases, when less than 40 hours of total time is worked and when more than 40 hours of total hours worked is covered.

When more than 40 hours of total work time is covered, then, a new variable will have to be introduced called overtimeHours. This will be able to manage the amount of time worked in excess of the 40 hours total work time and included in the total wage for the month.

Since we are training you as a programmer. Try to see how this solution can be read in English.

Then, try to run this program out and see the results:

Use this programs named as an example of a program for this exercise.

This program has adopted the use of floating stings point format. (String Formats for Float Precision) so as to allow the use of the cents in the answer to two decimal places:

In most cases, this will be able to manage the numeric values but this can accommodate the decimal conversions from strings via floats and not integers.

Alternatively, a different version of the expression code for the body of calculating weekly wages can be used in the code used above.

This is just a general formula which will be able to manage the expression in the formula and allow the parameters to accommodate the if statement. The same problem can be solved using many other ways in way that will still provide you with the same output.

Practical Exercise

Assignment One:

Develop a program name This program should be able to classify students on the basis of how many credits they have. The program should be able to print a statement on whether they have enough credits to be able to graduate. At Kenyatta University, it is a requirement that one needs about 120 credits so that they can be able to graduate.

Assignment 2: Head or Tails Exercise

Develop a program and name it Ht refers to heads and tails. The code needs to introduce a function flip(), this will represent a single flip of a coin which will be able to represent either the head or a tail. You try come up with a range of numbers by the use 0 or 1 with a random.randrange (2), Also, use the if --- else statement that will allow the printing of heads when the result is zero (0) and tails when the results is (1).

Create a simple repeat loop which will call flip () 12 times so as to try your code if can generate a random sequence of 10 heads and tails.