Understanding the most basic built-in functions in Python is essential for anyone beginning their journey in programming with this versatile and powerful language. Python is renowned for its simplicity and readability, and a significant part of this ease of use comes from its extensive standard library, which includes a multitude of built-in functions. These functions provide fundamental capabilities that allow programmers to perform common tasks without the need to write custom code for every operation. Below, let’s detail some of the most basic and frequently used built-in functions in Python, providing explanations and examples to illustrate their use.
1. `print()`
The `print()` function is one of the most fundamental functions in Python. It outputs data to the standard output device, usually the console. This function is indispensable for debugging and displaying information to the user.
Example:
{{EJS40}}2. `len()`
The `len()` function returns the length (the number of items) of an object. The argument may be a sequence (such as a string, list, or tuple) or a collection (such as a dictionary).
Example:
{{EJS41}}3. `type()`
The `type()` function returns the type of an object. This can be useful for debugging and ensuring that variables are of the expected type.
Example:
{{EJS42}}4. `int()`, `float()`, `str()`
These functions are used to convert values to an integer, float, or string, respectively. They are essential for type conversion, which is a common requirement in many programming tasks.
Example:
{{EJS43}}5. `input()`
The `input()` function reads a line from input, converts it to a string, and returns it. This function is important for interactive programs that require user input.
Example:
{{EJS44}}6. `sum()`
The `sum()` function takes an iterable (like a list or tuple) and returns the sum of its elements. This function is particularly useful for numerical computations.
Example:
{{EJS45}}7. `min()` and `max()`
The `min()` and `max()` functions return the smallest and largest item in an iterable, respectively. They can also take multiple arguments and return the smallest or largest among them.
Example:
{{EJS46}}8. `sorted()`
The `sorted()` function returns a new sorted list from the elements of any iterable. It is a highly versatile function that can sort data in ascending or descending order.
Example:
{{EJS47}}9. `range()`
The `range()` function generates a sequence of numbers, which is often used in for-loops. It can take one, two, or three arguments.
Example:
{{EJS48}}10. `enumerate()`
The `enumerate()` function adds a counter to an iterable and returns it as an enumerate object. This is particularly useful for obtaining an index along with the value when iterating over a list.
Example:
{{EJS49}}11. `zip()`
The `zip()` function takes iterables (can be zero or more), aggregates them in a tuple, and returns it. This is useful for iterating over multiple sequences in parallel.
Example:
{{EJS50}}12. `map()`
The `map()` function applies a given function to all items in an input list (or any iterable) and returns a list of the results. It is a powerful tool for functional programming.
Example:
{{EJS51}}13. `filter()`
The `filter()` function constructs an iterator from elements of an iterable for which a function returns true. This is useful for extracting elements that meet certain criteria.
Example:
{{EJS52}}14. `reduce()`
The `reduce()` function, which is part of the `functools` module, applies a rolling computation to sequential pairs of values in a list. This is useful for cumulative operations like summing or multiplying all elements.
Example:
{{EJS53}}15. `any()` and `all()`
The `any()` function returns `True` if any element of the iterable is true. If the iterable is empty, it returns `False`. Conversely, the `all()` function returns `True` if all elements of the iterable are true (or if the iterable is empty).
Example:
{{EJS54}}16. `chr()` and `ord()`
The `chr()` function returns the string representing a character whose Unicode code point is the integer passed as an argument. The `ord()` function returns an integer representing the Unicode code point of a given Unicode character.
Example:
{{EJS55}}17. `abs()`
The `abs()` function returns the absolute value of a number. This is a fundamental mathematical function used in various computations.
Example:
{{EJS56}}18. `round()`
The `round()` function returns a floating-point number rounded to the specified number of decimals. If no number of decimals is specified, it rounds to the nearest integer.
Example:
{{EJS57}}19. `divmod()`
The `divmod()` function takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder.
Example:
{{EJS58}}20. `pow()`
The `pow()` function returns the value of x raised to the power of y. If a third argument is provided, it returns x raised to the power of y, modulo z.
Example:
{{EJS59}}21. `isinstance()`
The `isinstance()` function checks if an object is an instance or subclass of a class or a tuple of classes.
Example:
{{EJS60}}22. `issubclass()`
The `issubclass()` function checks if a class is a subclass of another class or a tuple of classes.
Example:
{{EJS61}}23. `help()`
The `help()` function invokes the built-in help system. It is highly useful for obtaining documentation on modules, classes, functions, and keywords.
Example:
{{EJS62}}24. `dir()`
The `dir()` function attempts to return a list of valid attributes of the object. If the object is a module, it returns the list of names defined in the module.
Example:
{{EJS63}}25. `eval()`
The `eval()` function parses the expression passed to it and runs Python expression (code) within the program.
Example:
{{EJS64}}26. `exec()`
The `exec()` function executes the dynamically created program, which is either a string or a code object.
Example:
{{EJS65}}27. `globals()` and `locals()`
The `globals()` function returns a dictionary representing the current global symbol table. The `locals()` function returns a dictionary representing the current local symbol table.
Example:
{{EJS66}}28. `id()`
The `id()` function returns the identity of an object. This identity is unique and constant for this object during its lifetime.
Example:
{{EJS67}}29. `hash()`
The `hash()` function returns the hash value of an object (if it has one). Hash values are integers used to quickly compare dictionary keys during a dictionary lookup.
Example:
{{EJS68}}30. `memoryview()`
The `memoryview()` function returns a memory view object of the given argument. A memory view object exposes the buffer interface to Python objects.
Example:
{{EJS69}}31. `open()`
The `open()` function opens a file and returns a corresponding file object. It is essential for file handling in Python.
Example:
{{EJS70}}32. `repr()`
The `repr()` function returns a string representation of an object that can be used to recreate the object using `eval()`.
Example:
{{EJS71}}33. `set()`
The `set()` function returns a new set object, optionally with elements taken from an iterable.
Example:
{{EJS72}}34. `frozenset()`
The `frozenset()` function returns a new frozenset object, optionally with elements taken from an iterable. A frozenset is an immutable version of a set.
Example:
{{EJS73}}35. `slice()`
The `slice()` function returns a slice object representing the set of indices specified by range(start, stop, step).
Example:
{{EJS74}}36. `staticmethod()`
The `staticmethod()` function returns a static method for a function. It is used inside a class to define methods that do not operate on an instance or class.
Example:
{{EJS75}}37. `property()`
The `property()` function returns a property attribute. It is used to manage the access to instance attributes.
Example:
{{EJS76}}38. `super()`
The `super()` function returns a proxy object that delegates method calls to a parent or sibling class of type. It is used to call a method from the parent class.
Example:
{{EJS77}}39. `vars()`
The `vars()` function returns the `__dict__` attribute of the given object. If no argument is provided, it acts like `locals()`.
Example:
{{EJS78}}40. `__import__()`
The `__import__()` function is called by the `import` statement. It can be used to import a module programmatically.
Example:
python
math = __import__('math')
print(math.sqrt(16)) # Output: 4.0
These built-in functions provide a solid foundation for working with Python. They cover a wide range of functionalities, from basic input/output to more advanced operations like type conversion, mathematical computations, and object introspection. Familiarity with these functions will greatly enhance one's ability to write efficient and effective Python code.
Other recent questions and answers regarding Built-in functions:
- Why is it important to understand slices when analyzing game boards in Python?
- What is the advantage of using indexing in Python?
- What is the symbol used to add comments in Python code?
- How can we iterate over a list of lists using a "for" loop in Python?
- What is the purpose of using the "enumerate" function in Python?
More questions and answers:
- Field: Computer Programming
- Programme: EITC/CP/PPF Python Programming Fundamentals (go to the certification programme)
- Lesson: Functions (go to related lesson)
- Topic: Built-in functions (go to related topic)

