Python Functions - Formula Sheet
Python Functions Reference and Syntax Sheet
Function Definition Pattern
The standard syntactic formula for creating a function in Python:
def function_name(parameter_1, parameter_2, ...):
Where:
- def: The mandatory keyword used to begin a function definition block.
- function_name: The unique identifier name of the function (following variable naming rules).
- parameter_1, parameter_2: The optional, comma-separated local variables that receive values (arguments) when called.
- return: The statement used to exit a function and optionally pass an output expression back to the caller.
Anonymous (Lambda) Function Formula
The mathematical format of a single-line lambda function:
lambda argument_list : expression
Where:
- lambda: The keyword used to define an anonymous function.
- argument_list: Comma-separated inputs passed to the function.
- expression: A single mathematical or logical operation whose calculated result is automatically returned.
Recursion Recurrence Relation (Fibonacci & Factorial Examples)
Recursive definitions can be represented as mathematical recurrence relations:
Factorial: F(n) = n * F(n-1) for n greater than 0; Base case: F(0) = 1
Fibonacci: Fib(n) = Fib(n-1) + Fib(n-2) for n greater than or equal to 2; Base cases: Fib(0) = 0, Fib(1) = 1
Where:
- n: The input parameter representing the size of the problem.
- F(n-1) / Fib(n-1): Recursive self-calls with reduced parameter sizes.
- Base case: The stopping condition that prevents infinite call stacks.
Built-in Mathematical Functions
Core mathematical relations supported by the math library:
- math.floor(x): Returns the largest integer less than or equal to x.
- math.ceil(x): Returns the smallest integer greater than or equal to x.
- math.sqrt(x): Returns the square root of x where x greater than 0.
- pow(a, b): Returns a raised to the power of b (mathematically equivalent to a**b).
More for this chapter
Book Back Questions10 textbook MCQs · solved
Additional MCQs15 extra MCQs · solved
Practice TestInteractive · instant score
Book Back TestTest yourself on the textbook set
Additional MCQ TestTest yourself on the extra set
Study NotesConcepts & methods
More chapters in Computer Science
View all
1 Function
2 Data Abstraction
3 Scoping
4 Algorithmic Strategies
5 Python -Variables and Operators
6 Control Structures
8 Strings and String manipulation
9 Lists, Tuples, Sets and Dictionary
10 Python Classes and objects
11 Database Concepts
12 Structured Query Language (SQL)
13 Python and CSV files
14 Importing C++ programs in Python.
15 Data manipulation through SQL
16 Data visualization using pyplot: line chart, pie chart and bar chart