5.4. Conditionals and recursion

5.4.1. Boolean expressions: comparison operators

The program below uses a comparison operator to compare two floating-point values.

x = 12.4
y = 15.2
xIsGreaterThanY = x > y
print(xIsGreaterThanY)

Copy-paste the program to your script editor and save as comp.py. Execute it. Add a statement that prints the type of x, y, and xIsGreaterThanY.


Question What is the type of xIsGreaterThanY?

  1. Integer

  2. Floating point

  3. String

  4. Boolean

Correct answers: d.

Feedback:

x = 12.4 
y = 15.2

xIsGreaterThanY = x > y

print(xIsGreaterThanY)
print(type(xIsGreaterThanY))

Copy-paste the script below to your editor, save it as comp_eq.py, and execute it. What is the meaning of ==? Is the outcome what you expected?

x = 12
y = 12
xIsEqualToY = x == y
print(xIsEqualToY)

Run the script.

Change the first two lines in comp_eq.py to:

x = 12.0001
y = 12.0

As you know this has changed the type of x and y to floating point. Execute the script. Does it return the value for xIsEqualToY that you expected?

Now, assign to x a value of 12.000000000000000001 (note: this is 17 zeros after the dot). Execute the script. You will notice that Python considers 12.0 and 12.000000000000000001 as equal values. This is caused by the (un-)precision how floating-points are stored in the computer. This exercise learns you to be careful to use == on floating points, since you can never be sure about the outcome when the values which are compared are almost equal. It should actually not be used on ==.

As a final exercise with comparison operators, try out some others. A list of all operators is given in the Python Documentation, Library Reference (on MS Windows it should be in your Python menu). Alternatively, Python Documentation is also available at http://www.python.org. Be sure to have the Library Reference bookmarked since you will use it more often. If you search for something, a good entry to the Library Reference is its index (linked at the top-right corner of the Library Reference page).

5.4.2. Logical operators

Logical operators are Boolean expressions comparing two Boolean inputs, and returning a true (represented by a integer 1) or false (represented by an integer 0). The exercises below will learn you the meaning of these operators. Use Python scripts to answer the questions when needed.


Question What is the value of c in: c = 0 and 0 ?

  1. 0 (False)

  2. 1 (True)

Correct answers: a.

Feedback: The operator and only returns True if both inputs are True (that is 1).



Question QuestionText What is the value of c in: c = not 1 and 0 ?

  1. 0 (False)

  2. 1 (True)

Correct answers: a.

Feedback: -



Question What is the value of c in: c = not (1 and 0)

  1. 0 (False)

  2. 1 (True)

Correct answers: b.

Feedback: -



Question What is the value of c in: c = not(not 0 or not 0)

  1. 0 (False)

  2. 1 (True)

Correct answers: a.

Feedback: -

5.4.3. Conditional execution

In a previous exercise, you have made a script to calculate the slope factor in the Universal Soil Loss Equation. You have saved it as s.py. If you do not have it anymore, it is given in the table below. The script does not take into account that the equation used for the slope factor is not valid for

  • negative values of the slope,

  • values of the slope above 45 degrees.

import math

# program to calculate the s factor in the USLE 

slope = input("Enter the slope in degrees ")
print("The slope you entered is", slope, "degrees.")
slopeFloat=float(slope)

# slope in radians
slopeRadians = (slopeFloat/360) * 2.0 * math.pi
print("This corresponds to a slope of ", slopeRadians, "in radians.")

# slope in m/m
slopeFraction = math.tan(slopeRadians)
print("This corresponds to a slope of ", slopeFraction, "(m/m)")

# slope factor
S = 0.065 + 0.045 * slopeFraction + 0.0065 * slopeFraction**2.0

print("The slope factor is", S)

Open the s.py script and save it as condi.py. If you execute it, you will find that it does not take these restrictions into account. It runs with negative values or values for the slope greater than 45 degrees without complaining. But the value it returns in these cases is non-sense.

Modify condi.py such that it prints appropriate error messages when the user enters a slope value below zero degrees or above 45 degrees. Otherwise, it should print the slope factor. Execute the script several times to check whether it works before answering the next question.


Question Which keyword did you use?

  1. If

  2. ifor

  3. if

  4. ifthen

Correct answers: c.

Feedback: There are several ways to solve this problem using conditionals. The script below uses a chained conditional.

import math

# program to calculate the s factor in the USLE 

slope = input ("Enter the slope in degrees ")
print("The slope you entered is", slope, "degrees.")
slopeFloat=float(slope)

if slopeFloat > 45:
  print( "The slope you entered is greater than 45 degrees. For " 
         "slope values above 45 degrees, the slope factor equation "  
         "is not valid and the slope factor cannot be calculated")

elif slopeFloat < 0:
  print("You entered a negative slope. Please enter positive values only")

else:
  # slope in radians
  slopeRadians = (slopeFloat/360.0) * 2.0 * math.pi
  print("This corresponds to a slope of ", slopeRadians, "in radians.")
  
  # slope in m/m
  slopeFraction = math.tan(slopeRadians)
  print("This corresponds to a slope of ", slopeFraction, "(m/m)")
  
  # slope factor
  S = 0.065 + 0.045 * slopeFraction + 0.0065 * slopeFraction**2.0
  
  print("The slope factor is", S)