In Python, there are both built-in functions that are provided by the language itself and user-defined functions that are created by the users. Here are some examples of both:
- Built-in Functions:
print()
: Used to output text or variables to the console.len()
: Returns the length of a string, list, or other iterable.type()
: Returns the type of an object.input()
: Takes input from the user through the console.range()
: Generates a sequence of numbers.sum()
: Calculates the sum of a sequence of numbers.
- User-defined Functions:
def greet(name):
print("Hello, " + name + "!")
def add_numbers(a, b):
return a + b
def multiply_list(numbers):
result = 1
for num in numbers:
result *= num
return result
Code language: Python (python)
- In the examples above:
- The
greet()
function takes a name as an argument and prints a greeting message. - The
add_numbers()
function takes two numbers as arguments and returns their sum. - The
multiply_list()
function takes a list of numbers as an argument and returns their product.
- The
These are just a few examples, and both built-in and user-defined functions can be used in various ways to perform different tasks in Python.
What are the most commonly used built-in functions in Python?
Python provides a wide range of built-in functions that are commonly used in everyday programming tasks. Here are some of the most commonly used built-in functions in Python:
print()
: Used to output text or variables to the console.input()
: Takes input from the user through the console.len()
: Returns the length of a string, list, tuple, or other iterable.type()
: Returns the type of an object.range()
: Generates a sequence of numbers.sum()
: Calculates the sum of a sequence of numbers.max()
: Returns the maximum value from a sequence.min()
: Returns the minimum value from a sequence.abs()
: Returns the absolute value of a number.round()
: Rounds a number to a specified number of decimal places.sorted()
: Returns a new sorted list from an iterable.str()
: Converts an object into a string representation.int()
: Converts a string or a number into an integer.float()
: Converts a string or a number into a floating-point number.list()
: Converts an iterable into a list.dict()
: Creates a new dictionary.set()
: Creates a new set.enumerate()
: Returns an iterator that yields pairs of elements and their indices.zip()
: Combines multiple iterables into a single iterable of tuples.help()
: Displays the documentation and help information for objects, modules, or keywords.
These are just a few examples of the commonly used built-in functions in Python. There are many more functions available in the Python standard library that can be used for a wide range of tasks.
Read More;
- What is TQDM in for loop Python?
- What is an example of a syntax error in Python?
- What is the dash function in Python?
- How to test a class with unittest Python?
- What is an example of a runtime error?
- How to run Python script from GitLab?
- What is an example of a string in Python?
- What is RegEx in Python with example?
- What is an example of a Boolean in Python?
- What Is Variable Length Argument In Python With Example?
- What is NumPy in Python with example?
- Python Example For Arithmetic Operations On Lists