Built-in Functions in Python

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.

Understanding Built-in Functions

In this section, we’ll cover the basics of built-in functions in Python and understand their significance in programming.

What are Built-in Functions?

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.

Why Use Built-in Functions?

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.

Built-in Functions vs. User-defined Functions

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.

Basic Built-in Functions

In this section, we’ll explore some of the most commonly used basic built-in functions in Python.

print() Function

The print() function is used to display output on the console.

				
					print("Hello, World!")
				
			

Explanation:

  • The print() function is used to display output on the console.
  • In this example, it prints the string “Hello, World!”.
				
					# Output

Hello, World!
				
			

len() Function

The 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
				
			

Explanation:

  • The len() function returns the length of an object, such as a list, tuple, string, or dictionary.
  • In this example, it returns the length of the list my_list, which is 5.
				
					# Output

5
				
			

input() Function

The input() function is used to take user input from the console.

				
					name = input("Enter your name: ")
print("Hello,", name)
				
			

Explanation:

  • The input() function is used to take user input from the console.
  • It displays the prompt “Enter your name:” and waits for the user to enter input.
  • The entered value is assigned to the variable name, which is then printed along with the greeting message.
				
					# Output

Enter your name: John
Hello, John
				
			

Advanced Built-in Functions

In this section, we’ll explore advanced built-in functions that offer powerful functionalities for data manipulation, iteration, and more.

map() Function

The 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]
				
			

Explanation:

  • The 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).
  • It returns a new iterable (in this case, a list) with the results of applying the function to each item.
  • The list() function is used to convert the iterable returned by map() into a list, which is then printed.

filter() Function

The 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]
				
			

Explanation:

  • The 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).
  • It returns a new iterable (in this case, a list) with only the elements for which the function returns True.
  • The list() function is used to convert the iterable returned by filter() into a list, which is then printed.

Commonly Used Built-in Functions

In this section, we’ll explore more commonly used built-in functions that are frequently encountered in Python programming.

sorted() Function

The 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]
				
			

Explanation:

  • The sorted() function returns a new sorted list from the elements of any iterable (in this case, the list numbers).
  • The original list remains unchanged, and the sorted list is printed.

max() and min() Functions

The 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
				
			

Explanation:

  • The max() function returns the maximum value from an iterable (in this case, the list numbers).
  • The min() function returns the minimum value from the same iterable.
  • Both functions operate on numerical values and return the maximum and minimum values, respectively.

sum() Function

The sum() function returns the sum of all elements in an iterable.

				
					numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total)  # Output: 15
				
			

Explanation:

  • The sum() function returns the sum of all elements in an iterable (in this case, the list numbers).
  • It adds up all the numerical values in the list and returns the total.

Built-in Functions for Data Types

In this section, we’ll explore built-in functions that are specific to certain data types in Python.

String Functions: 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.
				
			

Explanation:

  • The str() function converts an object into a string (in this case, converting the integer 42 into the string '42').
  • The format() function formats a string with placeholders (in this case, {}) and replaces them with the provided values (name and age).

List Functions: 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]
				
			

Explanation:

  • The list() function creates a new list from an iterable (in this case, the result of range(5), which generates numbers from 0 to 4).
  • The 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!❤️

Table of Contents