- What are inbuilt functions โ Python's ready-to-use tools
- Mathematical functions โ abs(), max(), min(), sum(), round()
- Type conversion โ int(), str(), list(), tuple(), dict()
- Sequence functions โ len(), sorted(), reversed(), enumerate()
- Utility functions โ print(), input(), type(), isinstance()
- Functional tools โ map(), filter(), zip(), all(), any()
What are Inbuilt Functions?
Python comes with a rich set of built-in functions that are always available without importing any modules. These functions provide essential functionality for everyday programming tasks.
๐ก Key concept: Python has over 70 built-in functions covering mathematical operations, type conversions, sequence manipulation, input/output, and more. They are optimized and ready to use.
Mathematical Functions
Math and Number Functions
# 1. abs() โ absolute value
print(abs(-10)) # 10
print(abs(-3.14)) # 3.14
print(abs(5)) # 5
# 2. max() โ maximum value
print(max(3, 7, 2, 9, 1)) # 9
print(max([10, 20, 30, 40])) # 40
print(max("hello")) # 'o' (highest ASCII)
# 3. min() โ minimum value
print(min(3, 7, 2, 9, 1)) # 1
print(min([10, 20, 30, 40])) # 10
print(min("hello")) # 'e'
# 4. sum() โ sum of iterable
print(sum([1, 2, 3, 4, 5])) # 15
print(sum((10, 20, 30))) # 60
print(sum([1, 2, 3], 10)) # 16 (start=10)
# 5. round() โ round to n decimals
print(round(3.14159)) # 3
print(round(3.14159, 2)) # 3.14
print(round(2.675, 2)) # 2.67 (floating point precision)
# 6. pow() โ power calculation
print(pow(2, 3)) # 8
print(pow(2, 3, 5)) # 3 (2^3 % 5)
print(pow(5, 2)) # 25
# 7. divmod() โ quotient and remainder
quotient, remainder = divmod(10, 3)
print(f"10 รท 3 = {quotient} remainder {remainder}") # 3 remainder 1
# 8. oct(), hex(), bin() โ number base conversion
print(bin(10)) # 0b1010
print(oct(10)) # 0o12
print(hex(10)) # 0xa
Mathematical functions summary:
- abs() โ absolute value
- max()/min() โ find extremes
- sum() โ add all elements
- round() โ round numbers
- pow() โ exponentiation
- divmod() โ quotient and remainder
Quick Check: What does abs(-5) return? (Answer: 5)
Type Conversion Functions
Converting Between Types
# 1. int() โ convert to integer
print(int("123")) # 123
print(int(45.67)) # 45 (truncates)
print(int("1010", 2)) # 10 (binary to decimal)
# 2. float() โ convert to float
print(float("3.14")) # 3.14
print(float(10)) # 10.0
# 3. str() โ convert to string
print(str(123)) # "123"
print(str(45.67)) # "45.67"
print(str([1, 2, 3])) # "[1, 2, 3]"
# 4. list() โ convert to list
print(list("abc")) # ['a', 'b', 'c']
print(list((1, 2, 3))) # [1, 2, 3]
print(list({1, 2, 3})) # [1, 2, 3]
# 5. tuple() โ convert to tuple
print(tuple("abc")) # ('a', 'b', 'c')
print(tuple([1, 2, 3])) # (1, 2, 3)
# 6. dict() โ convert to dictionary
print(dict([("a", 1), ("b", 2)])) # {'a': 1, 'b': 2}
# 7. set() โ convert to set
print(set([1, 2, 2, 3, 3, 4])) # {1, 2, 3, 4}
# 8. bool() โ convert to boolean
print(bool(0)) # False
print(bool(1)) # True
print(bool([])) # False
print(bool([1, 2])) # True
print(bool("")) # False
print(bool("hello")) # True
# 9. chr() and ord() โ character codes
print(ord('A')) # 65
print(ord('a')) # 97
print(chr(65)) # 'A'
print(chr(97)) # 'a'
Type conversion functions:
- int(), float(), str() โ basic conversions
- list(), tuple(), dict(), set() โ collection conversions
- bool() โ truth value testing
- chr(), ord() โ character code conversion
Quick Check: What does bool([]) return? (Answer: False)
Sequence & Iterable Functions
Working with Sequences
# 1. len() โ get length
print(len("hello")) # 5
print(len([1, 2, 3, 4])) # 4
print(len((1, 2, 3))) # 3
print(len({"a": 1, "b": 2})) # 2
# 2. sorted() โ return sorted list
numbers = [3, 1, 4, 1, 5, 9, 2]
print(sorted(numbers)) # [1, 1, 2, 3, 4, 5, 9]
print(sorted(numbers, reverse=True)) # [9, 5, 4, 3, 2, 1, 1]
# Sort strings by length
words = ["apple", "banana", "cherry", "date"]
print(sorted(words, key=len)) # ['date', 'apple', 'banana', 'cherry']
# 3. reversed() โ iterate in reverse
for num in reversed([1, 2, 3, 4]):
print(num) # 4, 3, 2, 1
# 4. enumerate() โ get index and value
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
# 0: apple, 1: banana, 2: cherry
# With start index
for index, fruit in enumerate(fruits, start=1):
print(f"{index}: {fruit}")
# 1: apple, 2: banana, 3: cherry
# 5. zip() โ combine iterables
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
cities = ["NYC", "LA", "Chicago"]
for name, age, city in zip(names, ages, cities):
print(f"{name} is {age} from {city}")
# Convert to list of tuples
pairs = list(zip(names, ages))
print(pairs) # [('Alice', 25), ('Bob', 30), ('Charlie', 35)]
# 6. range() โ generate sequence
print(list(range(5))) # [0, 1, 2, 3, 4]
print(list(range(2, 8))) # [2, 3, 4, 5, 6, 7]
print(list(range(2, 10, 2))) # [2, 4, 6, 8]
Sequence functions:
- len() โ length of sequence
- sorted() โ returns sorted list
- reversed() โ reverse iterator
- enumerate() โ index and value pairs
- zip() โ combine iterables
- range() โ generate number sequence
Quick Check: What does enumerate() return? (Answer: An iterator of index-value pairs)
String Functions
String Manipulation Functions
# Python has many built-in string methods
text = " Hello World! "
# 1. strip() โ remove whitespace
print(text.strip()) # "Hello World!"
# 2. lower() and upper() โ case conversion
print("Hello".lower()) # "hello"
print("Hello".upper()) # "HELLO"
# 3. replace() โ replace substring
print("Hello World".replace("World", "Python")) # "Hello Python"
# 4. split() โ split into list
words = "apple,banana,cherry".split(",")
print(words) # ['apple', 'banana', 'cherry']
# 5. join() โ join list to string
print("-".join(["a", "b", "c"])) # "a-b-c"
# 6. startswith() / endswith() โ check beginning/end
print("Hello".startswith("He")) # True
print("Hello".endswith("lo")) # True
# 7. find() โ find substring index
print("Hello World".find("World")) # 6
print("Hello World".find("Python")) # -1 (not found)
# 8. count() โ count occurrences
print("Hello Hello".count("Hello")) # 2
# 9. isdigit(), isalpha(), isalnum() โ character checks
print("123".isdigit()) # True
print("abc".isalpha()) # True
print("abc123".isalnum()) # True
print("abc123".isalpha()) # False
# 10. format() โ string formatting
name = "Alice"
age = 25
print("Hello, {}! You are {} years old.".format(name, age))
print(f"Hello, {name}! You are {age} years old.") # f-string (Python 3.6+)
String functions:
- strip() โ remove whitespace
- lower()/upper() โ case conversion
- replace() โ replace substrings
- split()/join() โ convert between string and list
- find() โ locate substrings
- isdigit(), isalpha() โ character type checking
Quick Check: What does "Hello".lower() return? (Answer: "hello")
Utility Functions
General Utility Functions
# 1. print() โ display output
print("Hello World!")
print("Hello", "World", sep=", ", end="!\n")
print("Hello World", end="!")
# 2. input() โ get user input
# name = input("Enter your name: ")
# print(f"Hello, {name}!")
# 3. type() โ get object type
print(type(10)) #
print(type("hello")) #
print(type([1, 2, 3])) #
# 4. isinstance() โ check type
print(isinstance(10, int)) # True
print(isinstance("hello", str)) # True
print(isinstance([1, 2], list)) # True
# 5. id() โ object identity
a = 10
b = 10
print(id(a)) # Memory address of a
print(id(b)) # Memory address of b
# 6. dir() โ list object attributes
print(dir(str)) # List all string methods
print(dir(list)) # List all list methods
# 7. help() โ get documentation
# help(print)
# help(str)
# 8. eval() โ evaluate expression
x = 5
print(eval("x * 2")) # 10
print(eval("3 + 4")) # 7
# 9. exec() โ execute code
code = """
for i in range(3):
print(i)
"""
exec(code) # Prints 0, 1, 2
# 10. sorted() โ sort any iterable
print(sorted("python")) # ['h', 'n', 'o', 'p', 't', 'y']
Utility functions:
- print()/input() โ I/O operations
- type() โ get object type
- isinstance() โ check type relationship
- id() โ object memory address
- dir() โ list attributes
- help() โ get documentation
Quick Check: What does type(3.14) return? (Answer:
Functional Programming Tools
map(), filter(), and Other Functional Tools
# 1. map() โ apply function to all elements
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
print(squared) # [1, 4, 9, 16, 25]
# Map with multiple iterables
a = [1, 2, 3]
b = [10, 20, 30]
sums = list(map(lambda x, y: x + y, a, b))
print(sums) # [11, 22, 33]
# 2. filter() โ filter elements based on condition
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens) # [2, 4, 6, 8, 10]
# Filter strings by length
words = ["cat", "dog", "elephant", "ant", "tiger"]
long_words = list(filter(lambda x: len(x) > 3, words))
print(long_words) # ['elephant', 'tiger']
# 3. all() โ all elements True?
print(all([True, True, True])) # True
print(all([True, False, True])) # False
print(all([1, 2, 3])) # True (non-zero is True)
print(all([0, 1, 2])) # False (0 is False)
# 4. any() โ any element True?
print(any([False, False, False])) # False
print(any([False, True, False])) # True
print(any([0, 0, 1])) # True
# 5. sum() with start value
print(sum([1, 2, 3], 10)) # 16
# 6. zip() with unpacking
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
# 7. Combining functional tools
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Get sum of squares of even numbers
result = sum(map(lambda x: x**2, filter(lambda x: x%2==0, numbers)))
print(result) # 220 (4+16+36+64+100)
Functional programming tools:
- map() โ apply function to every element
- filter() โ keep elements that satisfy condition
- all() โ check if all elements are True
- any() โ check if any element is True
- zip() โ combine multiple iterables
Quick Check: What does all([True, True]) return? (Answer: True)
Try It Yourself
Experiment with Python's built-in functions in the editor below.
INBUILT FUNCTIONS PRACTICE
========================================
1. MATHEMATICAL FUNCTIONS
Numbers: [-5, 10, 3, -8, 15, 0]
Max: 15
Min: -8
Sum: 15
Absolute of -5: 5
Round 3.14159 to 2 decimals: 3.14
2. TYPE CONVERSION
int('123'): 123
float('45.67'): 45.67
str(100): 100
list('abc'): ['a', 'b', 'c']
tuple([1, 2, 3]): (1, 2, 3)
bool(0): False
bool('hello'): True
3. SEQUENCE FUNCTIONS
Text: 'Python Programming'
Length: 18
Sorted: [' ', 'P', 'P', 'a', 'g', 'h', 'i', 'm', 'm', 'n', 'o', 'o', 'r', 't', 'y', 'y']
Reversed: [4, 3, 2, 1]
4. STRING METHODS
Original: ' Hello World! '
Strip: 'Hello World!'
Upper: ' HELLO WORLD! '
Lower: ' hello world! '
Replace: ' Hello Python! '
5. FUNCTIONAL TOOLS
Numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Evens: [2, 4, 6, 8, 10]
All even? False
Any even? True
Inbuilt functions practice complete!
You've Got It!
You now understand Python's built-in functions โ mathematical, type conversion, sequence, string, utility, and functional programming tools. These are essential for everyday Python programming.
Quick Quiz
Test what you've learned:
Frequently Asked Questions
What is the difference between built-in functions and methods?
How many built-in functions does Python have?
dir(__builtins__) in the Python interpreter.
What is the difference between sorted() and list.sort()?
sorted() returns a new sorted list and works on any iterable. list.sort() sorts the list in place and returns None. sorted() is more flexible as it works on tuples, strings, etc.
What is the difference between map() and filter()?
map() applies a function to every element and returns the results. filter() keeps only elements that satisfy a condition. Both return iterators.
What's a common interview question about built-in functions?
Can I create my own built-in functions?
Where to Go From Here
Now that you know Python's built-in functions, check out these related topics:
User-Defined Functions
Learn why and how to create your own functions.
Learn More โFunction Arguments
Master different types of function arguments.
Learn More โLambda Functions
Learn about anonymous functions and their use cases.
Learn More โ