1. What is a function in Python?

In Python, a function is a group of structured statements whose purpose is to perform a specific task when called. A function is used to call a single code from multiple places in a program. It is also called method or procedure. Python provides many built-in functions like print(), input(), compile(), exec(), etc. but it also gives the possibility to create your own functions.
The notion of function is very important in computer science. They allow a problem to be broken down into sub-problems, which can themselves be broken down into sub-tasks. This design of the programs allows better readability of the code. In addition, the functions are reusable in several places of the program or even in several different programs provided that we have taken the trouble to gather them within modules.
We will first see the user defined  functions and then we will deal with the original functions, i.e. those that you will develop yourself in order to deal with the particular problem that you have to solve.
You can choose any name for the function you are creating, with the exception of reserved language keywords, and on the condition that you do not use any special or accented characters (the underscore “_” character is allowed) . As is the case for variable names, we use lowercase by convention, especially at the beginning of the name.

There are 3 types of functions in python:

  1. built-in functions in python
  2. user-defined functions
  3. lumbda functions

2. User defined function

The syntax of a user-defined function is declared using the keyword def followed by the name of the function

def function_name (parameters):

Example: function that returns the product of two numbers

def product(x,y):
return x*y
print(product(5,7))# display '35'

Example (function without parameters)

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.
The Python interpreter has various predefined functions that are easily usable. We don't need to define these functions to use them; we can call them directly. These functions are called built-in functions.
In previous tutorials in this Python course, we have used various built-in functions. 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

  1. Python abs(): It returns the absolute value of a number and the returned value is always positive.
  2. 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.
  3. 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.
  4. Python ascii(): This method returns a string containing a printable representation.
  5. Python bin(): This Python built-in function is used to convert an integer to a binary string.
  6. Python bool(): This Python built-in function is used to convert a value to boolean.
  7. Python bytearray(): This built-in python function returns an array of the given byte size.
  8. Python bytes(): This Python built-in function returns an immutable bytes object.
  9. Python callable(): This Python built-in function is used to check if the object is callable.
  10. Python chr(): This built-in python function returns a character (a string): from an integer.
  11. Python classmethod(): This built-in python function returns the class method for a given function.
  12. Python compile(): This built-in python function returns a Python code object.
  13. Python complex(): This Python built-in function is used to create a complex number.
  14. Python delattr(): This Python built-in function is used to remove an attribute from an object.
  15. Python dict(): This built-in function is used to create a Python dictionary.
  16. Python dir(): This built-in python function tries to return attributes of an object.
  17. Python divmod(): This built-in python function returns a tuple of quotient and remainder.
  18. Python enumerate(): This built-in python function returns an enumerate object.
  19. Python eval(): This built-in python function executes Python code in a program
  20. Python exec(): This built-in python function is used to execute a dynamically created program.
  21. Python filter(): This Python built-in function is used to construct an iterator from the elements that are true.
  22. Python float(): This Python built-in function is used to return the floating point number from a number or string.
  23. Python format(): This built-in python function returns the formatted representation of a value.
  24. Python frozenset(): This built-in python function returns an immutable frozenset object.
  25. Python getattr(): This built-in python function returns the value of an object's named attribute.
  26. Python globals(): This built-in python function returns the dictionary of a current global symbol table.
  27. Python hasattr(): This built-in python function returns a value that indicates whether an object has a named attribute.
  28. Python hash(): This built-in python function returns the hash value of an object.
  29. Python help(): This built-in python function Invokes the built-in help system
  30. Python hex(): This Python built-in function is used to convert an integer to its hexadecimal form.
  31. Python id(): This built-in python function returns the id of an object.
  32. Python input(): This built-in python function usually reads and returns a line of string.
  33. Python int(): This built-in python function returns an integer from a number or a string.
  34. Python isinstance(): This function checks if an object is an instance of a class.
  35. Python issubclass(): This checks if an object is a subclass of a class.
  36. Python iter(): This function returns an iterator for an object.
  37. Python len(): This function returns the length of an object.
  38. Python list(): This function is used to create a Python list.
  39. Python locals(): This function returns the dictionary of a current local symbol table
  40. Python map(): This function applies functions and returns a list.
  41. Python max(): This function returns the largest element.
  42. Python memoryview(): This function returns the memory view of an argument.
  43. Python min(): This function returns the smallest element.
  44. Python next(): This function retrieves the next element of an iterator.
  45. Python object(): This function creates a featureless object.
  46. Python oct(): This function converts an integer to its octal form.
  47. Python open(): This function returns a file object.
  48. Python ord(): This function returns a Unicode point for a Unicode character.
  49. Python pow(): This function evaluates and returns x to the y power.
  50. Python print(): It is used to print a given object.
  51. Python property(): This function returns a property attribute.
  52. Python range(): This function returns the sequence of integers between start and stop.
  53. Python repr(): This function returns the printable representation of an object.
  54. Python reversed(): This function returns the reversed iterator of a sequence.
  55. Python round(): This function rounds a floating point number to n digits.
  56. Python set(): This function returns a Python set.
  57. Python setattr(): This function sets the value of an attribute of an object.
  58. Python slice(): It is used to create slice object specified by range() function.
  59. Python sorted(): This function returns the sorted list of a given iterable.
  60. Python staticmethod(): This function creates a static method from a function.
  61. Python str(): This function returns an informal representation of an object.
  62. Python sum(): This function is used to add items from an Iterable.
  63. Python super(): This function allows us to refer to the parent class as 'super'.
  64. Python tuple(): This function creates a tuple in python.
  65. Python type(): This function returns the type of object.
  66. Python vars(): This function returns the __dict__ attribute of a class.
  67. Python zip(): This function returns an iterator of tuples.
  68. Python __import__(): This function is an advanced function called by 'import'.

4.Lumbda function

4.1 About lumbda function

In Python, an anonymous function is a function defined without a name. While normal functions are defined using the def keyword in Python, a lumbda function is an anonymous function and is defined using the lambda keyword.

4.2 Syntax of a Lambda function

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.

# example of using lambda functions
double = lambdax:x*2

print(double(10)) # dusplay: '20'

Younes Derfoufi

Leave a Reply