Python Functions Summary - CBSE Class 12
1. Built-in Functions
Function Use Example
print() Displays output print("Hello World")
input() Takes user input name = input("Enter name: ")
len() Returns length len("hello") -> 5
type() Returns type type(10) -> <class 'int'>
int(), float() Converts data type float("3.5") -> 3.5
str() Converts to string str(45) -> "45"
range() Generates sequence range(1, 6) -> [1, 2, 3, 4, 5]
sum() Sum of elements sum([1,2,3]) -> 6
max(), min() Max/Min value max([1, 5, 2]) -> 5
abs() Absolute value abs(-7) -> 7
round() Rounds number round(3.14159, 2) -> 3.14
sorted() Returns sorted list sorted([3, 1, 2]) -> [1, 2, 3]
2. User-defined Functions
Keyword Use Example
def Define a function def greet(): print("Hi")
return Send value back return a + b
parameters Accept input in def def add(a, b):
arguments Passed during call add(3, 4)
3. Math Library Functions (import math)
Function Use Example
[Link]() Square root [Link](25) -> 5.0
[Link](x, y) Power x^y [Link](2, 3) -> 8.0
[Link]() Rounds up [Link](4.2) -> 5
[Link]() Rounds down [Link](4.8) -> 4
[Link] Pi value [Link] -> 3.14159...
4. Random Module Functions (import random)
Function Use Example
[Link]() Random float 0-1 [Link]() -> 0.38...
[Link](a,b) Random int a-b [Link](1, 6)
[Link]() Random item from sequence [Link](['a','b'])
[Link]() Shuffle list [Link](mylist)
5. Statistics Module Functions (import statistics)
Function Use Example
[Link]() Average [Link]([2, 3, 4]) -> 3
[Link]() Middle value [Link]([2, 3, 4]) -> 3
[Link]() Most frequent [Link]([2, 2, 3]) -> 2
6. String Functions
Function Use Example
[Link]() Uppercase "abc".upper() -> "ABC"
[Link]() Lowercase "ABC".lower() -> "abc"
[Link]() Find index "hello".find("e") -> 1
[Link]() Replace string "cat".replace("c","b") -> "bat"
[Link]() Trim spaces " hi ".strip() -> "hi"
7. List Functions/Methods
Function Use Example
append() Add to end [Link](5)
insert() Insert at index [Link](1, 10)
remove() Remove by value [Link](3)
pop() Remove by index [Link]()
sort() Sort ascending [Link]()