Python Builtin Functions
$count++; if($count == 1) { include "../mobilemenu.php"; } ?> if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
Python comes with numerous built-in functions that allow us to perform essential tasks without needing to write custom code for them. These functions help in data manipulation, type conversion, iteration, math operations, and more. Here, we will discuss some of the most commonly used built-in functions in Python, along with examples and their real outputs.
1. `print()`
The `print()` function displays the specified message or variable to the console.# Using print to display a message
print("Hello, world!")
# Printing multiple values
print("Name:", "Alice", "Age:", 25)
Output:
Hello, world!
Name: Alice Age: 25
2. `type()`
The `type()` function returns the type of an object. This is useful for debugging and understanding the types of variables in your code.# Check the type of different objects
print(type(5))
print(type(3.14))
print(type("Hello"))
print(type([1, 2, 3]))
Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
3. `len()`
The `len()` function returns the number of items in an object like a string, list, tuple, or dictionary.# Get the length of different objects
print(len("Hello"))
print(len([1, 2, 3, 4, 5]))
print(len({"name": "Alice", "age": 25}))
Output:
5
5
2
4. `abs()`
The `abs()` function returns the absolute value of a number. If the input is negative, it returns the positive equivalent.# Get absolute values
print(abs(-10))
print(abs(3.5))
Output:
10
3.5
5. `max()` and `min()`
The `max()` and `min()` functions return the largest and smallest values from a sequence or multiple values.# Find the maximum and minimum values
print(max(3, 7, 1, 5))
print(min([10, 20, 5, 15]))
Output:
7
5
6. `sum()`
The `sum()` function returns the total of a list or iterable containing numeric values.
# Calculate the sum of a list
print(sum([1, 2, 3, 4, 5]))
Output:
15
7. `round()`
The `round()` function rounds a number to the specified number of decimal places.# Round numbers to different decimal places
print(round(3.14159, 2))
print(round(5.678, 0))
Output:
3.14
6.0
8. `sorted()`
The `sorted()` function returns a sorted list of the specified iterable without modifying the original iterable.# Sort a list of numbers
numbers = [4, 1, 3, 2, 5]
sorted_numbers = sorted(numbers)
print("Sorted:", sorted_numbers)
Output:
Sorted: [1, 2, 3, 4, 5]
9. `input()`
The `input()` function allows the user to input data as a string. It can be used to get input from the user at runtime.# Prompting the user for input
name = input("Enter your name: ")
print("Hello,", name)
Output:
Enter your name: Alice
Hello, Alice
10. `range()`
The `range()` function generates a sequence of numbers. It’s often used with loops.# Generate numbers from 0 to 4
for i in range(5):
print(i)
Output:
0
1
2
3
4
11. `map()`
The `map()` function applies a specified function to each item in an iterable.# Square each number in the list
numbers = [1, 2, 3, 4]
squared_numbers = list(map(lambda x: x * x, numbers))
print(squared_numbers)
Output:
[1, 4, 9, 16]
12. `filter()`
The `filter()` function filters items in an iterable based on a specified condition.# Filter out numbers greater than 2
numbers = [1, 2, 3, 4]
filtered_numbers = list(filter(lambda x: x > 2, numbers))
print(filtered_numbers)
Output:
[3, 4]
13. `zip()`
The `zip()` function combines elements from multiple iterables into tuples, pairing items by their positions.# Zip two lists together
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
combined = list(zip(names, ages))
print(combined)
Output:
[('Alice', 25), ('Bob', 30), ('Charlie', 35)]
14. `enumerate()`
The `enumerate()` function adds a counter to an iterable, returning a sequence of tuples containing the index and value.# Enumerate over a list
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(index, fruit)
Output:
0 apple
1 banana
2 cherry
15. `isinstance()`
The `isinstance()` function checks if an object is an instance of a specified class or type.# Check if the object is of a specific type
print(isinstance(5, int))
print(isinstance("hello", str))
print(isinstance([1, 2, 3], list))
Output:
True
True
True
Summary
Python’s built-in functions provide powerful and efficient tools for common tasks, improving code readability and reducing the need for writing custom implementations. From input/output operations to sequence manipulation and type-checking, built-in functions are a core part of Python’s functionality and help create cleaner, more concise code.