Python Functions

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:

  1. built-in functions in python
  2. user-defined functions
  3. 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.

Example: function that returns the product of two numbers

Example (function without parameters)

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

  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

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

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.


Younes Derfoufi
CRMEF OUJDA

Leave a Reply