10 commonly used built-in functions

Built-in functions are pre-defined functions that are available for you to use without needing to write the code yourself. These functions are built into the Python language and perform common tasks and operations, making your coding life easier.

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

  1. print(): Displays output on the console. You can pass values or variables as arguments to the print() function.
    print("Hello, world!")  # Output: Hello, world!
  2. len(): Returns the length of a string, list, or any other iterable object.
    fruits = ["apple", "banana", "orange"]
    len(fruits)  # Output: 3
  3. input(): Gets user input from the console. It displays a prompt and waits for the user to enter a value.
    name = input("Enter your name: ")
    print("Hello, " + name + "!")  # Output: Hello, [user's name]!
  4. type(): Determines the type of an object. It returns the type as a string.
    x = 5
    type(x)  # Output: <class 'int'>
  5. max(): Returns the largest item in an iterable or the largest of two or more arguments.
    numbers = [10, 5, 8, 12]
    max(numbers)  # Output: 12
  6. min(): Returns the smallest item in an iterable or the smallest of two or more arguments.
    numbers = [10, 5, 8, 12]
    min(numbers)  # Output: 5
  7. sum(): Returns the sum of all items in an iterable.
    numbers = [1, 2, 3, 4, 5]
    sum(numbers)  # Output: 15
  8. round(): Rounds a number to a specified number of decimal places.
    x = 3.14159
    round(x, 2)  # Output: 3.14
  9. str(): Converts an object into a string.
    x = 10
    print("The value is " + str(x))  # Output: The value is 10
  10. sorted(): Returns a new sorted list from the items in an iterable (e.g. a list).
    numbers = [5, 2, 8, 1, 9]
    sorted_numbers = sorted(numbers)
    print(sorted_numbers)  # Output: [1, 2, 5, 8, 9]

These examples demonstrate the usage of various built-in functions in Python. Remember, there are many more built-in functions available, each serving a specific purpose.

Next, learn about the built-in range function to generate a sequence of numbers.