Python Built-in Functions

Python provides a rich library of built-in functions that can be used directly in any Python program without importing any modules. They provide convenient methods for performing common tasks. Familiarity with these functions can greatly improve your coding efficiency.

Below are some of the most commonly used and useful built-in functions:

I/O and Printing

  • print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) Prints objects to a text stream, typically the console.

    print("Hello", "World", sep="-") # Output: Hello-World
  • input([prompt]) Reads a line of input from the user and returns it as a string.

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

Types and Type Conversion

  • type(object): Returns the type of an object.
  • int(x), float(x), str(x): Converts x to an integer, float, or string.
  • list(iterable), tuple(iterable), set(iterable): Converts an iterable to a list, tuple, or set.
  • dict(): Creates a new dictionary.
print(type(123)) # <class 'int'>
num_str = "100"
num_int = int(num_str)
print(num_int + 5) # 105

Mathematical Operations

  • abs(x): Returns the absolute value of a number.
  • round(number[, ndigits]): Rounds a number to the specified number of decimal places.
  • sum(iterable[, start]): Sums all items in an iterable.
  • max(iterable), min(iterable): Returns the maximum or minimum item in an iterable.
print(sum([1, 2, 3, 4, 5])) # 15
print(max(10, 20, 5))      # 20

Sequence and Collection Operations

  • len(s): Returns the length (number of items) of an object.
  • sorted(iterable, *, key=None, reverse=False): Returns a new sorted list of items from an iterable.
  • reversed(seq): Returns a reversed iterator.
  • range(start, stop[, step]): Generates a sequence of numbers.
my_list = [3, 1, 4, 1, 5, 9]
print(sorted(my_list, reverse=True)) # [9, 5, 4, 3, 1, 1]

for i in range(5):
    print(i) # 0, 1, 2, 3, 4

Iteration and Loops

  • enumerate(iterable, start=0): Returns an enumerate object. When iterating, it yields a tuple containing a count value (starting from start) and the value obtained from the iterable.
  • zip(*iterables): Creates an iterator that aggregates elements from multiple iterables together.
  • map(function, iterable, ...): Applies function to every element of iterable, returning an iterator.
  • filter(function, iterable): Uses function to filter elements of iterable, returning an iterator containing those elements for which the function returns True.
names = ['Alice', 'Bob', 'Charlie']
for index, name in enumerate(names):
    print(f"{index}: {name}")

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
zipped = zip(numbers, letters)
print(list(zipped)) # [(1, 'a'), (2, 'b'), (3, 'c')]

Logical Judgment

  • any(iterable): Returns True if at least one element in the iterable is true.
  • all(iterable): Returns True if all elements in the iterable are true.
print(any([False, False, True])) # True
print(all([True, True, False]))  # False

This is only a small part of Python's built-in functions. A complete list can be found in the Python official documentation. Making good use of these built-in functions is a key step in writing idiomatic, efficient Python code.