Conditional Statements within Python

 

Boolean values and expression:

As with practically all other programming languages Python allows Boolean values and expressions to be used. Boolean algebra is a mathematical concept developed by a person by the name of George Boole and is used as the basis for all computer based arithmetic.

There are only two possible Boolean values and they are True and False. As I have mentioned before, Python is a case sensitive programming language and so the above mentioned values must be capitalised. If you don’t use a capital Python will not recognise it as a Boolean value and an error will result.

In Python, Boolean values are used in conjunction with comparison operators which allow data sets to be compared. When two variables are compared and they fulfil the comparison criteria, the Boolean value, ‘True’ will be returned. Conversely if the comparison criteria are not met the Boolean value, ‘False’ will be returned.

On the following page I have included a table (Fig. 17) with all the comparative operators available in Python and what you can expect from them.

Comparative Operator

Description

x == y
Compares the values of x & y and returns ‘True’ if they are equal.
x != y
Returns ‘True’ if the values are NOT equal.
x > y
Returns ‘True’ if x is bigger than y.
x < y
Returns ‘True’ if x is smaller than y.
x >= y
Returns ‘True’ if x is bigger than or equal to y.
x <= y
Returns ‘True’ if x is smaller than or equal to y.

As I mentioned earlier there are amazing similarities between Computer Science and Mathematics and this you can see in the way that the comparative operators are. There is one error that often comes up though – you must never get ‘=’ and ‘==’ confused. The first operator ‘=’ is an assignment operator and so what you do here is make one variable the same as another. The second operator is the actual comparison operator and will return either ‘True’ or ‘False’ depending on whether the two variables are equal or not.

Logical operators:

A concept that forms part of Boolean algebra is that of logical operators. There are essentially three of these operators that can be used in Python. They are and, or and not. This allows us to construct more advanced conditional statements where more than one condition can be evaluated.

In the following example there are three integer variables: x, y and z. Firstly ‘x’ is assigned the integer 1, ‘y’ is assigned the integer 5 and ‘z’ is assigned the value 9.

53
Fig.53

In the forth line we are checking to see if x is smaller than y while z is bigger than y at the same time using the ‘and’ operator. Python returned the Boolean value ‘True’ which means it is!

Conditional Execution:

One of the areas where the above concepts are used extensively is within the conditional part of an ‘if’ statement. Lets look at the structure of ‘if’ statements within a Python environment.

As you will see in the example given on the next page (Fig. 54), various combinations of conditional and logical operators were used. Writing the program and testing it with a variety of values will give you an idea of the logic. I encourage you to get into the habit of commenting your code. This will help you at a later stage for revision purposes.

54
Fig.54

The function, isWorthy(), is the function that has a chain of conditional ‘if’ statements. The first statement simply checks to see if the user’s input was within the expected range (1 to 10). If that isn’t the case the getOpinion() function gets called again allowing the user to re-enter their opinion. After that there are 5 ‘elif’ statements. That keyword is actually an abbreviation for ‘else if’ and means just that. The last conditional statement (else) caters for any input that wasn’t covered by the previous conditional statements. Generally this would be a completely unexpected entry (like a letter – not number) and would generally allow the user to re-enter their opinion again.

Nested Conditionals:

In addition to being able to chain the conditional statements it is also possible to nest conditional statements within other ones. What this means is that you can have one master set of conditionals and then further decisions can be made within the blocks of code.

To illustrate this concept, imagine that you are writing a restaurant billing system and that the menu is divided up into four main sections: Breakfast, Lunch, Supper and Drinks. Once the first choice has been made (breakfast, lunch, supper or drinks) you get to chose the actual item in the sub-menu which would be handled in the nested conditional statement. See Fig.20 on the next page.

55
Fig.55

In the example given above there are three functions: The first one that the Python interpreter sees in the main function. All main does is it calls the menu() function. In this function the user is presented with a choice between drinks and food. Once that choice has been correctly made the program proceeds to the next function (subMenu), along with the ‘choice’ parameter.

In the subMenu() the program goes straight into the conditional ‘if’ statements using the choice parameter that in this function is called ‘chs’. By now, the choice between food and drinks has been made and so the program goes straight into one of the two nested if statements where the user can then choose what food or drinks they want.

pl

« Previous || Next »