TOPIC 4: Functions & Code Documentation
4. Built-in Functions in Python
Python has many built-in functions that are available for use without needing to define them. These functions perform common tasks and are essential for Python programming.
Common Built-in Functions:
print(): Outputs data to the console.
print("Hello, World!")
len(): Returns the length of an object (e.g., string, list).
my_list = [1, 2, 3]
print(len(my_list)) # Output: 3
type(): Returns the type of an object.
x = 5
print(type(x)) # Output: <class 'int'>
str(): Converts an object to a string.
x = 100
print(str(x)) # Output: '100'
int(): Converts an object to an integer.
x = "10"
print(int(x)) # Output: 10
sum(): Returns the sum of elements in an iterable (like a list).
numbers = [1, 2, 3]
print(sum(numbers)) # Output: 6
1.
max(): Returns the largest item in an iterable.
numbers = [1, 2, 3, 4]
print(max(numbers)) # Output: 4
min(): Returns the smallest item in an iterable.
numbers = [1, 2, 3, 4]
print(min(numbers)) # Output: 1