Built-in Function Lab
Pick a built-in function, type in an argument the way you would inside Python's IDLE Shell — numbers as-is, text in quotes, comma-separated values for multi-argument functions — and watch it evaluate.
Try a Built-in Function
● liveFunctions in Python
As problems grow larger, program code becomes longer and harder to read, understand, and maintain. Breaking a large program into smaller, manageable parts — called subprograms — solves this. Related subtasks are grouped into a single unit called a module; this process of breaking a problem into smaller sub-problems is called modularisation. Each subtask within a module is placed in a function.
Arguments and Return Values
A function receives zero or more input values — called arguments (sometimes called parameters) — and returns zero or more output values, called return values.
Why Functions Help
A function written once — for example, to find total marks — can be reused several times across a program. This enhances code reusability and reduces both duplication and overall code size.
Python classifies functions into three kinds: built-in functions, imported functions (from modules), and user-defined functions.
Built-in Functions
Python's standard library ships with a large collection of predefined functions, ready to use without being defined by the programmer. Here are the ones this chapter covers.
a. Type Conversion Functions
| Function | Input argument(s) | Return type & value | Remarks |
|---|---|---|---|
| int() | Numeric expression as number or a string containing whole numbers | Integer — integer part of the value | If a string argument contains non-numeric characters, it returns an error |
| float() | Numeric expression as numbers or a string | Float — value of the expression | Non-numeric characters in a string argument cause an error |
| str() | Any data type | String — the object converted to string type | Works with any input |
| bool() | Numeric expression, number, string, list, tuple, or dictionary | Boolean — True/False | Returns False for False, None, 0, an empty string, or any empty collection; True otherwise |
b. eval() Function
Takes an expression as a string and returns the resulting value. It works only with Python expressions — not statements like if, for, or while.
c. max() and min() Functions
max() returns the largest value in a group; min() returns the smallest. When comparing strings, dictionary order applies, and the comparison is case sensitive — since upper-case characters have lower Unicode values, all upper-case letters are treated as "earlier" than all lower-case letters.
abs(x)
Returns the positive equivalent of a numeric argument. Errors if the input doesn't evaluate to a number.
type()
Accepts a variable, literal, or expression and returns its data type, e.g. <class 'int'>.
len()
Finds the number of characters in a string, or the number of elements in a list, tuple, set, or dictionary.
id()
Returns a unique ID representing an object's memory reference — useful to check whether two variables point to the same object.
round(n, d) Function
Rounds n to d decimal places. If d is omitted, it rounds to the nearest integer. If d is negative, it rounds to the nearest 10|d| — e.g. round(2673.23, -2) gives 2700.
Imported Functions
Related predefined functions are grouped into modules according to purpose — the math module holds mathematical functions, statistics holds statistical ones. The keyword import brings a module's functions into a program.
a. Importing Entire Modules
import math import math, statistics import statistics as stat
Functions are then accessed with the dot operator: math.sqrt(), or via an alias: stat.mean().
b. Importing Selected Methods
from math import sqrt from math import sqrt, pow, cos from math import *
Importing only what's needed keeps the translated program code smaller than pulling in an entire module.
7.3.1 math Module
| Function | Description | Example |
|---|---|---|
| sqrt(x) | Square root of x; runtime error if x is negative | math.sqrt(23) → 4.795… |
| pow(x, y) | xy; result is always a float | math.pow(4, 3) → 64.0 |
| ceil(x) | Closest integer ≥ x | math.ceil(23.34) → 24 |
| floor(x) | Closest integer ≤ x | math.floor(-23.78) → -24 |
| fabs(x) | Absolute value, always returned as a float | math.fabs(-43) → 43.0 |
| sin(x) / cos(x) | Sine / cosine of x, given in radians | math.sin(1.5708) → ≈1.0 |
7.3.2 statistics Module
Part of Python's standard library — no external installation needed. All functions work on a group of values: a list, tuple, or set.
| Function | Description |
|---|---|
| mean(data) | Arithmetic mean (average) of the numeric data |
| median(data) | Middle value of the numeric data |
| mode(data) | Most frequently occurring value (central tendency) |
| variance(data) | How far the data is spread out from the mean; large variance = more spread |
| stdev(data) | Standard deviation — the square root of variance, in the same units as the data |
User-defined Functions
A user-defined function is written to perform a specific task according to the programmer's requirements — defined once, then called wherever it's needed.
7.4.1 Defining a Function
def starts the definition. The function body is written in proper indentation. A return keyword is used only in functions that have at least one value to send back to the caller — a function with no return statement (or a bare return) defaults to returning None.
7.4.2 Calling a Function & Flow of Execution
A function executes only when it is called — welcome(). Python processes all function definitions first when reading the program but skips over them; execution begins at the first statement outside any function. When a call is reached, control transfers into the function, runs until the last statement or a return, then shifts back to right after the call.
7.4.3 Passing Values and Returning Values
Actual Parameters
The values/variables used in the function call — here, principal, years, rate.
Formal Parameters
The variables used in the function definition — here, P, N, R.
When values separated by commas are returned, Python automatically packs them into a tuple; at the call site, the tuple is unpacked into the receiving variables. A function's argument type matters too — passing a float like 5.5 to a factorial() function built around range() causes a runtime error, since range() only accepts integers. Validating argument types avoids such errors.
Types of Arguments
Python supports four ways of passing arguments into a function call.
7.5.1 Positional Arguments
Passed in the same order as the formal argument list. interest(1000, 2, 12) maps 1000→P, 2→N, 12→R strictly by position.
7.5.2 Default Arguments
Given a default value in the definition using = — used only when the caller omits that argument. Default arguments must be the right-most ones in the list.
7.5.3 Keyword Arguments
Passed by explicitly naming the argument: interest(R=12, P=1000, N=2). Order no longer matters. Any positional arguments present must come first, left of the keyword ones.
7.5.4 Variable Length Arguments
Prefixing a parameter with *, e.g. *numbers, lets a function accept any number of values — Python packs them into a tuple internally.
The print() function's sep and end parameters — print(value, sep='', end='\n') — are a familiar everyday example of keyword arguments.
Scope Rules
An assignment to a variable creates a reference to an object — but that reference remains valid only within certain parts of the program. Python's scope rules define exactly where.
7.6.1 Local Scope
A variable assigned inside a function is accessible only inside that function. Accessing it from outside raises NameError: name 'x' is not defined.
7.6.2 Global Scope
A variable defined outside all functions is globally valid — accessible from any part of the program, including inside functions (with one exception, below).
7.6.3 Overriding
Redefining a global variable's name inside a function creates a new local variable that shadows it — the global value is left untouched outside the function.
Declaring global x inside a function tells Python to use and modify the global x instead of creating a new local one with the same name.
Recursive Functions
Recursion is a technique where a function solves a problem by calling itself on a simpler version of the same problem, until a base case is reached that can be answered directly. Every recursive function has a base case (stops the recursion) and a recursive part (calls itself with modified arguments, moving closer to the base case).
Trace a Recursive Call
● liveRecursion makes it easier to write and understand solutions for problems that naturally break into smaller subproblems, reducing lengthy code into shorter, cleaner functions. The trade-off: recursion uses more memory and can run slower than an equivalent loop.
Creating and Using User-defined Modules
Beyond built-in modules like math and statistics, programmers can create their own — a user-defined module is simply a .py file containing function, variable, or class definitions.
Importing the Whole Module
import calculator a = 20; b = 5 print("Addition: ", calculator.add(a, b))
Output: Addition: 25
Importing One Function
from calculator import divide a = 20; b = 5 print("Division: ", divide(a, b))
Output: Division: 4.0
Breaking a program into modules produces cleaner, more maintainable, and more reusable code.
Hierarchical Building Blocks of Python Programs
A Python program is organised as a clear hierarchy, growing from small units of code into larger collections of related tools — helping programmers manage large projects by reusing and organising code.
7.9.1 Function
A block of organised, reusable code performing a specific task — breaks a large program into smaller parts, easier to write, test, and maintain.
7.9.2 Module
A file containing Python code — functions, classes, or variables — logically organised and reusable across programs via import.
7.9.3 Package
A collection of related modules grouped in a directory. Must contain a special (possibly empty) file named __init__.py to be recognised as a package. NumPy, Pandas, Matplotlib are external packages.
7.9.4 Library
A collection of built-in and external modules and packages providing ready-to-use functionality for mathematics, data science, web development, and more.
Quick Recap
Tap a question to reveal the answer.
What does int('hello') do, and why?
It raises a ValueError: invalid literal for int() with base 10: 'hello' — int() can only convert a string to an integer if that string contains numeric characters representing a whole number.
What does eval(34) return — an error, or a value? Why?
It raises a TypeError — eval() requires its argument to be a string (or bytes/code object) containing an expression, not a bare number.
Why does round(126.5) give 126 while round(127.5) gives 128?
Python's round() uses Banker's Rounding (round half to even) — a value exactly halfway between two integers rounds to whichever of the two is even, reducing cumulative bias over many calculations.
In max('Kozhikode', 'kozhikode', 'Calicut', 'calicut'), why does 'kozhikode' (lower-case k) win over 'Kozhikode'?
String comparison in Python is case sensitive, and all upper-case characters have lower Unicode values than all lower-case characters — so a lower-case-starting string is treated as "greater" in dictionary order than the same word starting with an upper-case letter.
What is the default return value of a function that has no return statement, or a bare return?
None — the keyword representing the absence of a value.
What's the difference between actual parameters and formal parameters?
Actual parameters are the values/variables used in the function call statement. Formal parameters are the variable names used in the function definition. Values flow from actual to formal parameters when the function is called.
Why must default arguments be the right-most arguments in a function definition?
Because arguments are matched to parameters by position unless named explicitly — if a default argument came before a required one, Python couldn't tell which values the caller intended to skip.
Spot the issue: a global variable total = 0 is reassigned inside a function as total = total + 5 without declaring global total first.
This raises an UnboundLocalError. The moment Python sees an assignment to total anywhere inside the function, it treats total as local for the whole function body — so total + 5 tries to read a local variable that hasn't been assigned yet. Adding global total fixes it.
What happens if a recursive function is written without a base case?
The function keeps calling itself indefinitely and never terminates, eventually causing the Python interpreter to flag an error (a recursion depth / stack error) instead of returning a result.
Why does factorial(5.5) fail in a factorial function built around range(1, n+1)?
Because range() only accepts integer arguments — passing a float raises a runtime error. This is why validating the type of arguments before processing is important.
What must a folder contain for Python to treat it as a package rather than just a directory of scripts?
A special file named __init__.py, which can be empty — its presence is what Python uses to recognise the directory as a package.