Skip to Content
Course content

Python provides a wide range of built-in functions that allow you to perform common tasks efficiently without needing to write custom code. These functions are part of Python’s standard library and are available for use in any Python program.

Built-in functions can help you with tasks like type conversion, mathematical operations, handling strings, and more.

1. Common Built-in Functions

Here are some of the most commonly used built-in functions in Python:

1.1 print()

  • Used to output data to the console.

Example:

print("Hello, World!")

1.2 len()

  • Returns the length (the number of items) of an object (e.g., string, list, tuple).

Example:

my_list = [1, 2, 3, 4]
print(len(my_list))  # Output: 4

1.3 type()

  • Returns the type of an object.

Example:

x = 5
print(type(x))  # Output: <class 'int'>

1.4 max() and min()

  • max() returns the largest item in an iterable or the largest of two or more arguments.
  • min() returns the smallest item in an iterable or the smallest of two or more arguments.

Example:

numbers = [1, 2, 3, 4, 5]
print(max(numbers))  # Output: 5
print(min(numbers))  # Output: 1

1.5 sum()

  • Returns the sum of all elements in an iterable (e.g., list or tuple).

Example:

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

1.6 abs()

  • Returns the absolute value of a number.

Example:

num = -5
print(abs(num))  # Output: 5

1.7 round()

  • Rounds a floating-point number to a specified number of decimal places.

Example:

pi = 3.14159
print(round(pi, 2))  # Output: 3.14

2. String Functions

2.1 str()

  • Converts an object to a string.

Example:

x = 123
print(str(x))  # Output: '123'

2.2 upper() and lower()

  • upper() converts a string to uppercase.
  • lower() converts a string to lowercase.

Example:

text = "hello"
print(text.upper())  # Output: 'HELLO'
print(text.lower())  # Output: 'hello'

3. Type Conversion Functions

3.1 int()

  • Converts a value to an integer.

Example:

x = "42"
print(int(x))  # Output: 42

3.2 float()

  • Converts a value to a floating-point number.

Example:

x = "3.14"
print(float(x))  # Output: 3.14

3.3 str()

  • Converts a value to a string.

Example:

x = 42
print(str(x))  # Output: '42'

4. Collection-Related Functions

4.1 list()

  • Converts an iterable into a list.

Example:

x = (1, 2, 3)
print(list(x))  # Output: [1, 2, 3]

4.2 tuple()

  • Converts an iterable into a tuple.

Example:

x = [1, 2, 3]
print(tuple(x))  # Output: (1, 2, 3)

4.3 set()

  • Converts an iterable into a set (removes duplicates).

Example:

x = [1, 2, 2, 3]
print(set(x))  # Output: {1, 2, 3}

5. File-Related Functions

5.1 open()

  • Opens a file and returns a file object. You can use this object to read or write to the file.

Example:

file = open("example.txt", "w")
file.write("Hello, file!")
file.close()

5.2 read()

  • Reads the contents of a file.

Example:

file = open("example.txt", "r")
content = file.read()
print(content)  # Output: 'Hello, file!'
file.close()

6. Utility Functions

6.1 id()

  • Returns the unique identifier for an object.

Example:

x = 42
print(id(x))  # Output: Unique ID (depends on the system)

6.2 isinstance()

  • Checks if an object is an instance of a specified class or type.

Example:

x = 42
print(isinstance(x, int))  # Output: True

6.3 all() and any()

  • all() returns True if all elements of the iterable are true.
  • any() returns True if any element of the iterable is true.

Example:

nums = [1, 2, 3]
print(all(nums))  # Output: True (since all elements are non-zero)
print(any(nums))  # Output: True (since there are non-zero elements)

7. Lambda Functions and Higher-Order Functions

7.1 map()

  • Applies a function to every item in an iterable (like a list or tuple) and returns a map object (which is an iterator).

Example:

numbers = [1, 2, 3]
squared = map(lambda x: x ** 2, numbers)
print(list(squared))  # Output: [1, 4, 9]

7.2 filter()

  • Filters elements in an iterable based on a function that returns True or False.

Example:

numbers = [1, 2, 3, 4, 5]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers))  # Output: [2, 4]

Summary of Built-in Functions

Python’s built-in functions provide a versatile and efficient way to handle various tasks in your programs. Whether you're performing basic arithmetic, manipulating strings, or working with collections, Python’s built-in functions can save time and effort by handling common operations. Some built-in functions even allow you to work with files, process user input, and handle type conversion. Understanding and utilizing these functions will help you write clean, efficient, and effective Python code.

Commenting is not enabled on this course.