Types of Function
Quick answer Python offers three kinds of function — built-in (ready to use), module (must be imported and called through the module name), and user-defined (written by you with def) — differing only in whether you had to import it and whether you wrote it, not in how you call it.
A function is a named block of code that you write or obtain once and then run many times just by using its name. Python gives you three kinds. The board asks you to name all three, so learn the labels and not merely the idea.
- Built-in functions — supplied by Python itself and available the moment the program starts. No import needed. Examples:
len(),max(),min(),sum(),round(),type(),int(),print(),input(). - Functions defined in a module — they live in a module, which is a library of ready-made code kept outside your program. You must
importthe module first, then reach the function through the module name. Examples:math.sqrt(),random.randint(),statistics.mean(). - User-defined functions — the ones you write yourself with
def. Everything from the next section onwards is about these.
Every module on this syllabus comes bundled with Python, so there is nothing to install. Where a module physically sits varies and does not matter to you: statistics and random are ordinary .py files in Python’s library folder, while math has no .py file at all because it is compiled into the interpreter itself. The distinction that does matter is simpler — did you have to import it, and did you write it?
| Type | Where the code sits | Import needed? | How you call it |
|---|---|---|---|
| Built-in | Always available in the interpreter | No | len(marks) |
| Module | A library outside your program, bundled with Python | Yes | math.sqrt(144) |
| User-defined | Your own program file | No | gst_amount(1200, 18) |
Built-in functions first. Note that type() is itself listed among Python’s built-in functions, and it reports what something is.
marks = [78, 92, 65, 88, 55]
print(len(marks))
print(max(marks))
print(min(marks))
print(sum(marks))
print(round(sum(marks) / len(marks), 2))
print(type(marks))
Output:
5
92
55
378
75.6
Now module functions. The random.seed(7) line is there only so that the number printed below is the same one you will get; remove it and randint gives a fresh value on every run.
import math
import random
print(math.sqrt(144))
print(math.floor(87.9), math.ceil(87.1))
print(math.pow(2, 10))
random.seed(7)
print(random.randint(1, 6))
Output:
12.0
87 88
1024.0
3
Two things worth noticing. First, math.sqrt(144) gives 12.0 and not 12, because sqrt and pow hand back floats. Do not turn that into a rule about the whole module, though: math.floor and math.ceil return plain integers, which is exactly why the second line of output reads 87 88 rather than 87.0 88.0. The same is true of math.factorial and math.gcd. Check the one function you are using rather than assuming.
Second, forgetting the import does not produce any special “module missing” error. As far as the program is concerned the name math was simply never created, so you get an ordinary NameError:
print(math.sqrt(144))
Output:
NameError: name 'math' is not defined. Did you forget to import 'math'?
That second sentence is a hint Python 3.12 and later attach to the error. On an older lab machine you will see only NameError: name 'math' is not defined, and that shorter form is what an exam answer should give. Keep it separate in your mind from ModuleNotFoundError: No module named 'xyz', which is what happens when you do write an import but name a module that does not exist.
Finally, a user-defined function. GST at 18% on a purchase of Rs 1200:
def gst_amount(price, rate):
return price * rate / 100
print(gst_amount(1200, 18))
print(gst_amount(45000, 5))
Output:
216.0
2250.0
One warning before you leave this section, because it is a trap students fall into. type() tells you what kind of object a name holds, but it is not a test of which of the three categories a function belongs to:
import builtins
import math
import statistics
def gst_amount(price, rate):
return price * rate / 100
print(type(len))
print(type(math.sqrt))
print(type(statistics.mean))
print(type(gst_amount))
print(len(dir(builtins)))
Output:
160
len and math.sqrt both report builtin_function_or_method because both are written in C. But statistics.mean reports function — identical to the gst_amount you wrote yourself — because the statistics module is itself written in ordinary Python. So type() is telling you what language a function was written in, not whether it is built-in, imported or yours. Classify by the table instead.
That last line counts the names Python hands you for free in this version (Python 3.13). The exact figure changes between versions, so never memorise it — the point is that the built-in namespace is a fixed, modest list, and everything else you must either import or write.
Worked example — all three types in one program
A CGPA helper for a Class 12 marksheet: built-ins do the counting, two module functions do the arithmetic, and one function of your own holds the rule that is specific to your school.
import math
import statistics
def cgpa(marks):
return round(statistics.mean(marks) / 9.5, 2)
marks = [88, 92, 76, 95, 84]
print("count :", len(marks))
print("highest :", max(marks))
print("mean :", statistics.mean(marks))
print("sqrt sum:", math.sqrt(sum(marks)))
print("cgpa :", cgpa(marks))
Output:
count : 5
highest : 95
mean : 87
sqrt sum: 20.85665361461421
cgpa : 9.16
The mean prints as 87 and not 87.0 because the five marks add up to 435, which divides exactly by 5, and statistics.mean keeps an exact whole answer as an int. Look also at what cgpa() does inside its own body: it calls a module function and a built-in. Functions calling other functions is completely normal, and understanding the exact order in which that happens is the subject of the next section.
- Built-in means ready to use with no import; module means you must import first and call it as module.function(); user-defined means you wrote it yourself with def.
- Forgetting an import gives an ordinary NameError: name 'math' is not defined, because the module name was never created. Python 3.12 and later append the hint "Did you forget to import 'math'?", but the error class is still NameError.
- Do not confuse that with ModuleNotFoundError: No module named 'xyz', which is what an import of a module that does not exist raises.
- math.sqrt(144) returns 12.0, but math.floor and math.ceil return plain ints — the math module is not uniformly float, so check the one function you are using.
- type() does not classify functions for you: statistics.mean reports function, exactly like a def you wrote, because that module is itself written in Python. Only C-level functions such as len and math.sqrt report builtin_function_or_method.
- Your own functions can freely call built-ins and module functions from inside their bodies.
