PYTHON MADE SIMPLE (2015)
Chapter 7: Compound Boolean Expressions in Python
Perhaps, this can be best expressed using valid examples. Let us consider this case below:
For one to be eligible for graduation from a university in US, it was set that students needed 120 credits and a total GPA of minimum 2.0.
Using Python, create a program that can be used to select students that would be graduand’s for the year.
This suggests that this can only be true when credits will be equal to or greater than 120.
While GPA was also greater or equals to 2.0.
A good program will look like this:
Application of compound expressions
This will show that a compound condition will be true if both of the conditions are true. However, it will be seen as false when at least one of the conditions is false.
Previously, we had discussed the use of if and elif statements in cases where both tests where they will appear in the same block when the condition to be applied was true.
However, this can be stated in a much easier way as shown below: For instance, if x < xLow or x > xHigh, switch the sign of dx. This can be translated directly into Python:
The use of the word or creates a compound condition:
It is recommended that most of the complicated tests can be enshrined inside a function.
It is often convenient to encapsulate complicated tests inside a function. Think how to complete the function starting:
A rectangle is defined by two diagonally opposite points. This is how a rectangle can be prepared. The two corner points are defined by the function getP1 and getP2. Python calls the points obtained as pt1 and pt2. The values of the points can be expressed in the coordinate of x and y of pt1, pt2. Similarly, the point can also be expressed in the point form of getX() and getY().
Just in case we introduce variables for the x coordinate points of pt1, point, p2. The same coordinates can also be expressed as end1, va1 and end 2. The relationship between the two coordinates can be expressed using mathematical concepts.
For instance, this can be expressed as:
end1 <= val <= end2
However, this may not be sufficient to express the position of the corners. It is expected that the corners points are supposed to be diagonally opposite, and the second coordinates also need to be high compared to the first points. For instance, end1 may be 300; end 2 may be 150, while va1 may be 180. Note that the va1 is between the two coordinates of end1 and end2. These values can be substituted into the expression.
300 <= 180 <= 150
This expression is false and the values of 150 and 300 will need to be reversed. This can only be a complicated situation. Also this is an issue which must be revisited for both the x and y coordinates. The solution this can be solved using a new function isBetween to handle one coordinate at a time. Introduce an auxiliary function isBetween to deal with one coordinate at a time.
This expression may only be true when the original expression, end1 is less of equal to va1 and is also less or equals to end2. This will be true. There is also a likelihood that the case may be reversed. This two options can be combined as two unique possibilities using Boolean connectors andand or. This will make the relationship true in one of the relationship and using or, this will be the right connective.
Therefore, the complete code in python that will be able to function here would be expressed in the following ways:
A correct but redundant function body would be:
As a technique of understanding your code: try to extract the meaning of the code by interpreting the code to English.
This means that if the statement is True, return True. Similarly, if the condition is False, return False. In both condition, as described, the same values will be returned as set out in the test conditions.
It is not a requirement that you need an if-else statement so that you can be able to choose between True or False values. This can be used directly using the Boolean expressions as described before.
Similarly, other expressions when using the compound expressions as in this case:
end1 <= val <= end2
The main characters are end1 and end2. This is simply a standard mathematical syntax which can be used in chaining comparisons. The numbers of comparisons can be chained using these expressions using approximate notations. Note also that this can only be successfully coded in Python unlike other programs like Java, and C++.
They can also be translated in ways that can be read in many other common languages as shown by the expression below:
end1 <= val
val <= end2
On the other hand, an auxiliary function isBetween can also be used. This is an isInside function. The isBetween function can be used to find the values of the x coordinates; and also to check for the values of the why coordinates as well.
The two questions can be combined and the two point may be required to be between the sides and between the top and bottom, similarly, the right connectors to be applied is and.
But, how do you complete the isInside method? Probably, you will need to find this out. Find out more information from the link below:
Also note that when you want to solve for an opposite condition. In many cases borrowing from English, a word not is introduced in the expression.
If a point is not inside the required position such as not in a rectangle, you will be prompted to state that the point is not inside a rectangle (rect).This condition can also be applied in this case.
Therefore, this a not condition.
It is only True when condition set is False. It is also False when condition is True.
A program below is an example of creating values inside a rectangle and is referred to as selectbutton.py.
This is a complete program that applies the use of isInside expression as a simple application for selecting colors. Do not care so much about the lengths of the rectangles.
A number of efforts can be done to ensure that the code can be shortened to ensure that small rectangles can be used in this case and make it a more powerful code:
This program applies the use of isBetween and isInside which are discussed below: This programs makes make colored rectangles which can be used as buttons and which can also be used as picture component. The code that has been used in creating the rectangle is similar and is inside a function makeColoredRect. This is a good example for use of graphical tools in Python.
The code that has been developed is quite long and is enshrined with the graphical codes for drawing pictures and buttons and has sections with queries for asking the use to select the colors for a picture. You will also observe the codes have an if-elif-else test to check on which buttons have been pressed and set the colour of the picture element as commanded by the program.
The only new aspect with this program will be the use of the long statements isInside as shown below:
Python is an interactive program that will be able to read the statements as they continue to the next line even when there is an unmatched pair of brackets and parenthesis. It is recommended that when you have a long line that will be able to run over the screen or paper. This should be continued to the next line.
To make a final character on a line with a backslash (\) will be useful since it suggests that the line continues to the next line. This is the best way of making your work neat so that almost all the statements will be able to fit into the same line.
In addition, this will also be the best way of making your codes simple. Unlike Python, many other programs will require a special way of terminating a line such as ‘;’ which serves as command for the program not to pay attention to the new lines created. In some cases in Python, an extra parenthesis can also be used and it will do no harm to the program as shown in the above example using the isBetween.
You are eligible to vie for a US senator and US Representative position when you are at least 30 and 25 years old respectively. In addition, you must have been a US Citizen for at least 9 and 7 years respectively for these positions.
Write a program and call it congress.py to obtain the age and length of citizenship from the user to contest for the two positions.
Enhance your program so that you can be able to obtain the age, length of citizenship and print one of the statements listed below
1. You are eligible for both senate and representative and Senate.
2. You eligible only senate
3. You are ineligible representative.