Social Icons

Tuesday, October 18, 2022

The Python Functions

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. Functions help break the program down into smaller pieces to practice modular programming. As the program grows, the functions make it more organized and easier to manage. In addition, it avoids repetitions and makes the code reusable. 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):
    Instructions...

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
my-courses.net

No comments:

Post a Comment

Learn Python For Beginners

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (2) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (256) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) Bootstrap CSS (1) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (15) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) Github (2) HTML (9) IT News (3) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (37) Python BeautifulSoup (1) Python For Data Science (2) Python PyQt (13) Python Reference (1) Python Source Code (10) Python-Books (6) Python-DVD-Training (1) Python-Exercises (305) Python-Framework (1) Python-IDE (1) Python-Kivy-Framework (2) Python-Modules (1) Python-pdf (2) Python-pyQt (1) python-temp (3) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (4) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates