1. What is a function in Python?
A Python function is a named block of code that performs a specific task or a set of instructions. It allows you to encapsulate a piece of code that can be reused multiple times throughout a program. Functions are defined using the def keyword followed by the function name, parentheses, and a colon. The code block of the function is indented and is executed when the function is called.
There are 3 types of functions in python:
- built-in functions in python
- user-defined functions
- lumbda functions
2. User defined function
A user-defined function in Python is a function that is created by the programmer to perform a specific task or a set of instructions. Unlike built-in functions, which are already provided by the Python language, user-defined functions are defined by the user to meet their specific requirements.
To create a user-defined function in Python, you use the def keyword followed by the function name, parentheses, and a colon. The code block of the function is indented and contains the set of instructions or operations that the function performs. You can also specify parameters within the parentheses if the function needs to accept inputs.
1 2 |
def function_name (parameters): Instructions... |
Example: function that returns the product of two numbers
1 2 3 |
def product(x,y): return x*y print(product(5,7))# display '35' |
Example (function without parameters)
1 2 3 4 |
def hello(): print("Hello World") # call the function hello() # displays: "Hello World" |
3. Built-in functions in Python
3.1 About built-in functions
We know as mentioned above that there are three types of functions, namely user-defined functions in Python, lambda functions in Python, and built-in functions in Python. In this paragraph, we are going to learn everything there is to know about built-in functions in Python.
Built-in functions in Python are functions that are pre-defined in the Python language and are readily available for use without the need to import any additional modules or libraries. These functions are part of the Python standard library and cover a wide range of operations and tasks.
The very basic built-in function that we have used in almost every tutorial is the print() function which is used to display a given object.
3.2 List of the main built-in functions in Python
- Python abs(): It returns the absolute value of a number and the returned value is always positive.
- Python all(): It returns true when all items in an iterable evaluate to true or there are no items in an iterable. It returns false if an item evaluates to false.
- Python any(): It checks if an element of an iterable is true. Unlike all():, it returns false if there are no items in the iterable.
- Python ascii(): This method returns a string containing a printable representation.
- Python bin(): This Python built-in function is used to convert an integer to a binary string.
- Python bool(): This Python built-in function is used to convert a value to boolean.
- Python bytearray(): This built-in python function returns an array of the given byte size.
- Python bytes(): This Python built-in function returns an immutable bytes object.
- Python callable(): This Python built-in function is used to check if the object is callable.
- Python chr(): This built-in python function returns a character (a string): from an integer.
- Python classmethod(): This built-in python function returns the class method for a given function.
- Python compile(): This built-in python function returns a Python code object.
- Python complex(): This Python built-in function is used to create a complex number.
- Python delattr(): This Python built-in function is used to remove an attribute from an object.
- Python dict(): This built-in function is used to create a Python dictionary.
- Python dir(): This built-in python function tries to return attributes of an object.
- Python divmod(): This built-in python function returns a tuple of quotient and remainder.
- Python enumerate(): This built-in python function returns an enumerate object.
- Python eval(): This built-in python function executes Python code in a program
- Python exec(): This built-in python function is used to execute a dynamically created program.
- Python filter(): This Python built-in function is used to construct an iterator from the elements that are true.
- Python float(): This Python built-in function is used to return the floating point number from a number or string.
- Python format(): This built-in python function returns the formatted representation of a value.
- Python frozenset(): This built-in python function returns an immutable frozenset object.
- Python getattr(): This built-in python function returns the value of an object's named attribute.
- Python globals(): This built-in python function returns the dictionary of a current global symbol table.
- Python hasattr(): This built-in python function returns a value that indicates whether an object has a named attribute.
- Python hash(): This built-in python function returns the hash value of an object.
- Python help(): This built-in python function Invokes the built-in help system
- Python hex(): This Python built-in function is used to convert an integer to its hexadecimal form.
- Python id(): This built-in python function returns the id of an object.
- Python input(): This built-in python function usually reads and returns a line of string.
- Python int(): This built-in python function returns an integer from a number or a string.
- Python isinstance(): This function checks if an object is an instance of a class.
- Python issubclass(): This checks if an object is a subclass of a class.
- Python iter(): This function returns an iterator for an object.
- Python len(): This function returns the length of an object.
- Python list(): This function is used to create a Python list.
- Python locals(): This function returns the dictionary of a current local symbol table
- Python map(): This function applies functions and returns a list.
- Python max(): This function returns the largest element.
- Python memoryview(): This function returns the memory view of an argument.
- Python min(): This function returns the smallest element.
- Python next(): This function retrieves the next element of an iterator.
- Python object(): This function creates a featureless object.
- Python oct(): This function converts an integer to its octal form.
- Python open(): This function returns a file object.
- Python ord(): This function returns a Unicode point for a Unicode character.
- Python pow(): This function evaluates and returns x to the y power.
- Python print(): It is used to print a given object.
- Python property(): This function returns a property attribute.
- Python range(): This function returns the sequence of integers between start and stop.
- Python repr(): This function returns the printable representation of an object.
- Python reversed(): This function returns the reversed iterator of a sequence.
- Python round(): This function rounds a floating point number to n digits.
- Python set(): This function returns a Python set.
- Python setattr(): This function sets the value of an attribute of an object.
- Python slice(): It is used to create slice object specified by range() function.
- Python sorted(): This function returns the sorted list of a given iterable.
- Python staticmethod(): This function creates a static method from a function.
- Python str(): This function returns an informal representation of an object.
- Python sum(): This function is used to add items from an Iterable.
- Python super(): This function allows us to refer to the parent class as 'super'.
- Python tuple(): This function creates a tuple in python.
- Python type(): This function returns the type of object.
- Python vars(): This function returns the __dict__ attribute of a class.
- Python zip(): This function returns an iterator of tuples.
- Python __import__(): This function is an advanced function called by 'import'.
4.Lumbda function
4.1 About lumbda function
A lambda function, also known as an anonymous function or a lambda expression, is a way to define small, one-line functions in Python without explicitly using the def keyword. Lambda functions are typically used when you need a simple, throwaway function that doesn't require a full function definition.
4.2 Syntax of a Lambda function
1 |
lambda arguments: expression |
Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used anywhere function objects are required.
Example Lambda function in python
Here is an example of a lambda function that doubles the input value.
1 2 3 4 |
# example of using lambda functions double = lambdax:x*2 print(double(10)) # dusplay: '20' |
Younes Derfoufi
CRMEF OUJDA