6.6.1 – Built-in and library subprograms
Use built-in, library and supplied custom subprograms. Choose the right numeric operation, preserve returned values, and reason about random results and deliberate pauses.
Know where a call comes from
A subprogram is named code called to perform a task. It can return a result, perform an action, or both. Its origin is separate from its job.
A built-in is available without an import, such as len(). A library provides pre-existing code in a module that must first be imported. A user-devised subprogram is defined for the program with def.
import math
def announce(value):
print("Result: " + str(value))
root = math.sqrt(81)
announce(root)
Here math.sqrt is a library call. announce is a supplied user-devised procedure; its body runs when the final call passes in root. print and str inside it are built-ins. Defining a helper does not execute its body. The result is:
Result: 9.0
After import math, use the module prefix in math.sqrt(...). sqrt(...) alone is not provided by that import. Dot notation does not always mean an imported library: string methods such as name.upper() work on an existing value without importing a module.
Use a returned value
Read nested calls from the inside out: a returned value can become another call's argument. The familiar input, int, float, bool, str, len, range and print support input, conversion, length, looping and output. None needs an import.
text = "27"
message = "Items: " + str(int(text) + 1)
print(len(message))
The conversion gives 27, addition gives 28, and the message is "Items: 28", whose length is 9.
ord() converts one character to its Unicode number; chr() does the reverse. The first 128 numbers match ASCII. These calls convert a character code, not a digit character into its numeric quantity:
print(ord("A"))
print(chr(66))
print(ord("7"))
print(int("7"))
65
B
55
7
ord() requires one character. Passing a whole word is not a way to encode all its characters at once.
round(number, places) rounds a numeric value; omit places for a nearest integer. For example:
print(round(3.746, 2))
print(round(8.2))
print(round(5.0, 2))
3.75
8
5.0
Rounding does not guarantee two displayed decimal places. Use string formatting for a display such as 5.00. At exact halfway values Python chooses the even alternative, so round(2.5) is 2 and round(3.5) is 4. Decimal fractions are not always exact in floating-point storage; avoid assuming every apparent decimal tie rounds upwards.
Choose the mathematical operation
The math library contains ceil, floor, sqrt and the constant pi. A constant is a value, so math.pi has no call parentheses.
import math
print(math.ceil(2.3))
print(math.floor(2.3))
print(math.ceil(-2.3))
print(math.floor(-2.3))
print(math.sqrt(49))
3
2
-2
-3
7.0
Ceiling gives the smallest integer greater than or equal to a value; floor gives the largest integer less than or equal to it. Think along the number line, especially for negatives. int() instead removes the fractional part towards zero, so it is not always the same as floor.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account