We'll dive deep into the world of built-in functions in Python. Built-in functions are predefined functions that are readily available for use in Python without the need for additional imports. We'll explore a wide range of built-in functions, from basic ones like print() and len() to more advanced functions for data manipulation, iteration, and more.
In this section, we’ll cover the basics of built-in functions in Python and understand their significance in programming.
Built-in functions are functions that are pre-defined in Python and are always available for use without the need for explicit importing. These functions serve various purposes, from performing basic operations to complex data manipulations.
Built-in functions save time and effort by providing a set of commonly used functionalities out of the box. They are optimized for performance and are well-tested, making them reliable tools for developers.
While built-in functions are convenient for quick tasks, user-defined functions offer flexibility and customization. Understanding the differences between the two helps in choosing the right approach for different scenarios.
In this section, we’ll explore some of the most commonly used basic built-in functions in Python.
print()
FunctionThe print()
function is used to display output on the console.
print("Hello, World!")
print()
function is used to display output on the console.
# Output
Hello, World!
len()
FunctionThe len()
function returns the length of an object, such as a string, list, tuple, or dictionary.
my_list = [1, 2, 3, 4, 5]
print(len(my_list)) # Output: 5
len()
function returns the length of an object, such as a list, tuple, string, or dictionary.my_list
, which is 5.
# Output
5
input()
FunctionThe input()
function is used to take user input from the console.
name = input("Enter your name: ")
print("Hello,", name)
input()
function is used to take user input from the console.name
, which is then printed along with the greeting message.
# Output
Enter your name: John
Hello, John
In this section, we’ll explore advanced built-in functions that offer powerful functionalities for data manipulation, iteration, and more.
map()
FunctionThe map()
function applies a given function to each item of an iterable (e.g., list) and returns a new iterable with the results.
# Doubling each element in a list
numbers = [1, 2, 3, 4, 5]
doubled_numbers = list(map(lambda x: x * 2, numbers))
print(doubled_numbers) # Output: [2, 4, 6, 8, 10]
map()
function applies a given function (in this case, a lambda function that doubles the input) to each item of an iterable (in this case, the list numbers
).list()
function is used to convert the iterable returned by map()
into a list, which is then printed.filter()
FunctionThe filter()
function filters elements from an iterable based on a given function.
# Filtering even numbers from a list
numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # Output: [2, 4]
filter()
function filters elements from an iterable (in this case, the list numbers
) based on a given function (in this case, a lambda function that checks if the number is even).True
.list()
function is used to convert the iterable returned by filter()
into a list, which is then printed.In this section, we’ll explore more commonly used built-in functions that are frequently encountered in Python programming.
sorted()
FunctionThe sorted()
function returns a new sorted list from the elements of any iterable.
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # Output: [1, 1, 2, 3, 4, 5, 5, 6, 9]
sorted()
function returns a new sorted list from the elements of any iterable (in this case, the list numbers
).max()
and min()
FunctionsThe max()
and min()
functions return the maximum and minimum values from an iterable, respectively.
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5]
print(max(numbers)) # Output: 9
print(min(numbers)) # Output: 1
max()
function returns the maximum value from an iterable (in this case, the list numbers
).min()
function returns the minimum value from the same iterable.sum()
FunctionThe sum()
function returns the sum of all elements in an iterable.
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total) # Output: 15
sum()
function returns the sum of all elements in an iterable (in this case, the list numbers
).In this section, we’ll explore built-in functions that are specific to certain data types in Python.
str()
and format()
The str()
function converts an object into a string, while the format()
function formats a string with placeholders.
num = 42
str_num = str(num)
print(str_num) # Output: '42'
name = "John"
age = 30
message = "My name is {} and I am {} years old.".format(name, age)
print(message) # Output: My name is John and I am 30 years old.
str()
function converts an object into a string (in this case, converting the integer 42
into the string '42'
).format()
function formats a string with placeholders (in this case, {}
) and replaces them with the provided values (name
and age
).list()
and append()
The list()
function creates a new list, while the append()
method adds an element to an existing list.
new_list = list(range(5))
print(new_list) # Output: [0, 1, 2, 3, 4]
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
list()
function creates a new list from an iterable (in this case, the result of range(5)
, which generates numbers from 0 to 4).append()
method adds an element to the end of an existing list (in this case, adding the number 4
to the list my_list
).Built-in functions are fundamental tools in Python programming, offering a wide range of functionalities for various tasks. By understanding and mastering built-in functions, you can write more efficient and concise code. Whether you're a beginner or an experienced developer, harnessing the power of built-in functions can greatly enhance your Python programming skills. Happy Coding!❤️