5.2. Variables, expressions and statements

5.2.1. Values and Types

Below is an extended version of the script used in the previous section.

print(12 + 15)
a = 12.5
b = 2.0
print(a * b)
print(a / b)
# print the type of b
print(type(b))

Create this script using copy-paste to the Python script editor, or just add the two additional lines to the script which you used in the previous exercise. Note that the note print the type of b is typed after a #. This so-called comment will be ignored by the Python interpreter. Save the script as types.py and execute it.


Question What is the type of the variable b?

  1. floating-point

  2. integer

Correct answers: a.

Feedback: The variable is defined by adding the decimal value, that is 2.0. Typing 2 would have defined the variable as an integer.


Add the following lines to types.py:

message = "Hello World!"
c = 2

And add two statements that give you the type of the variables message and c. Save the script and execute it. Note that the type of c is different from b, since b contains a dot (.).

Also note that variable names such as message, b, and a, are case sensitive. Try this by adding the following statement to types.py, at the bottom, saving it, and executing:

print(C)

All statements in the script file are executed, apart from the last one. The interpreter gives you the line number with the error: it is the last line. You tried to print C, but it is not available, since lowercase c is defined, while uppercase C isn’t.

Also add commands that give you the type of 5 + "PCRaster", 2/5, 2.0/5, 2/5.0, and 2.0/5.0.

5.2.2. Order of operations

The velocity of flowing water in a channel (v, m/s) is dependent upon the flow depth or hydraulic radius (r, m), the roughness of the surface and the slope (s, -). This relationship is commonly expressed by the Manning equation:

v = (r 2/3s 1/2) / n

where n is the Manning coefficient of roughness (between 0.01 for smooth surfaces up to 0.8 for rough surfaces).

Create a Python script (save it as manning.py) printing the flow velocity as a function of the hydraulic radius, the Manning coefficient and slope. Use three statements assigning values to variables representing the hydraulic radius, roughness of the surface and slope. Use the following values for the variables: hydraulic radius: 3 m, slope 0.1, Manning’s n 0.01. Use intuitive variable names (e.g. hydraulic_radius) and provide comments for each statement (the units used). Use one statement assigning the flow speed to another variable using the equation given above. Finally, print this variable. Execute the script.


Question What is the flow speed in m/s for the input values given in the text?

  1. 65.78

  2. 315.34

  3. 270.92

  4. 376.2

Correct answers: a.

Feedback: A correct script is given below (note the comments). The order of calculation is explicitly defined here using brackets. Look up your Python book which brackets can be removed, to make the program easier readible.

# hydraulic radius (m)
hydraulic_radius = 3.0
# slope (-)
slope = 0.1
# manning's n
n = 0.01
# flow speed
v = (((hydraulic_radius)**(2.0/3.0))*(slope**(1.0/2.0)))/n
# print flow speed
print(v)

5.2.3. Operations on strings and composition

Modify the manning.py script (see previous section) such that it prints:

With a hydraulic radius of ? m, the flow speed is ? m/s.

At the questions marks, the script should print the values of the input variable used for hydraulic radius and the resulting flow speed. Use the Python format string syntax. Save and execute the script to test it.


Question How did you print With a hydraulic radius? By defining it as as:

  1. Floating point

  2. Integer

  3. String

Correct answers: c.

Feedback:

# hydraulic radius (m)
hydraulic_radius = 3.0
# slope (-)
slope = 0.1
# manning's n
n = 0.01
# flow speed
v = (((hydraulic_radius)**(2.0/3.0))*(slope**(1.0/2.0)))/n
# print flow speed
print(v)
# print flow speed with sentence
print("With a hydraulic radius of", hydraulic_radius, "(m) the flow speed is", v, "m/s.")