Skip to content

Computer Science · Ch 7 — Functions

Built-in functions

7.5.1

Built-in functions

Built-in functions are the ready-made functions of Python — functions so frequently needed that they are permanently available in every program, with no import and no definition required. The instructions they execute are already defined inside the Python interpreter.

Look at this small program:

# Program to calculate the square of a number
a = int(input("Enter a number: "))
b = a * a
print("The square of", a, "is", b)

Here input(), int() and print() are all built-in functions — we called them without ever writing a def for them.

Reading a function call like a detective

Take one Python statement containing a call to a built-in function and interrogate it:

fname = input("Enter your name: ")
  • Which function is being used? input().
  • Does it accept a value (an argument)? Yes — the parentheses contain the string "Enter your name: ".
  • Does it return a value? Yes — the assignment operator = before the function call means the function hands back a value, which is stored in the variable fname.

So input() both accepts a value and returns a value. Now apply the same two questions to int() and print() in the square-of-a-number program above: does each accept an argument, and does each return a value? (Hint: int() behaves like input() in both respects; print() accepts arguments but its call is never assigned to anything.)

Categories of built-in functions

Some of the frequently used built-in functions, grouped by what they do:

CategoryFunctions
Input / Outputinput(), print()
Datatype conversionbool(), chr(), dict(), float(), int(), list(), ord(), set(), str(), tuple()
Mathematicalabs(), divmod(), max(), min(), pow(), sum()
Other__import__(), len(), range(), type()

Commonly used built-in functions in detail

Several of these deserve a closer look (compare Table 7.1 of the textbook). Each row gives the syntax, the arguments accepted, what is returned, and a worked example.

abs(x) — x may be an integer or a floating point number; returns the absolute value of x.

abs(4)       # 4
abs(-5.7)    # 5.7

divmod(x, y) — x and y are numbers; returns a tuple (quotient, remainder).

divmod(7, 2)      # (3, 1)
divmod(7.5, 2)    # (3.0, 1.5)
divmod(-7, 2)     # (-4, 1)

max(sequence) or max(x, y, z, ...) — returns the largest element of a sequence, or the largest of two or more arguments. For strings the comparison is by character (ASCII) value.

max([1, 2, 3, 4])    # 4
max("Sincerity")     # 'y'  (based on ASCII values)
max(23, 4, 56)       # 56

min(sequence) or min(x, y, z, ...) — returns the smallest element of a sequence, or the smallest of two or more arguments.

min([1, 2, 3, 4])    # 1
min("Sincerity")     # 'S'  (uppercase letters have LOWER ASCII values than lowercase)
min(23, 4, 56)       # 4

pow(x, y[, z]) — returns x raised to the power y; if the optional third argument z is given, it returns (x raised to y) modulo z.

pow(5, 2)        # 25
pow(5, 2, 4)     # 1   (25 % 4)
pow(2, 0.5)      # 1.[PII:card]  (float arguments give a float result)
``` …
Table 7.1Commonly used built-in functions
Function SyntaxArgumentsReturnsExample Output
abs(x)x may be an integer or floating point numberAbsolute value of x>>> abs(4)
4
>>> abs(-5.7)
5.7
divmod(x,y)x and y are integersA tuple: (quotient, remainder)>>> divmod(7,2)
(3, 1)
>>> divmod(7.5,2)
(3.0, 1.5)
>>> divmod(-7,2)
(-4, 1)
max(sequence) or max(x,y,z,...)x,y,z,.. may be integer or floating point numberLargest number in the sequence/ largest of two or more arguments>>> max([1,2,3,4])
4
>>> max("Sincerity")
'y' #Based on ASCII value
>>> max(23,4,56)
56
min(sequence) or min(x,y,z,...)x, y, z,.. may be integer or floating point numberSmallest number in the sequence/ smallest of two or more arguments>>> min([1,2,3,4])
1
>>> min("Sincerity")
'S' #Uppercase letters have lower ASCII values than lowercase letters.
>>> min(23,4,56)
4
pow(x,y[,z])x, y, z may be integer or floating point numberx^y (x raised to the power y)
if z is provided, then: (x^y) % z
>>> pow(5,2)
25.0
>>> pow(5.3,2.2)
39.2
>>> pow(5,2,4)
1