.feed-links {display:none !important;} -->

Python functions

 function (or function ) is a series of instructions that can be called with a name.

Create my first function

Let's create a function that will return us an age:

>>>  def  indicates_my_age (): 
...      return  30 ; 
...  
>>>  indicate_my_age () 
30

You cannot copy paste this code, you must enter each line by hand and press enter to return to the line. The 3 chevrons and the 3 points are displayed by the python interpreter.

First of all to indicate to the interpreter that you want to create a function , we use the keyword def followed by a name then parentheses and then a colon.

We also notice that there is a space between the 3 points and the key word "return", it is about an indentation , ie a space which not only improves the reading of the function but which indicates that we are still in office. When the requested action is no longer in the function, the text should no longer be indented. To indent text, you must press the TAB key on your keyboard - or in other cases create 4 spaces manually -.

The settings

Let's create another function:
>>>  def  increase_me ( a ): 
...      return  increase_me  +  2 
...  
>>>  increase_me ( 1 ) 
3

This function increments by 2 a value that is passed as a parameter.

It is also possible to use several parameters:

>>>  def  increase_me ( a ,  b ): 
...      return  30  +  a  +  b 
...  
>>>  increase_me ( 1 ,  2 ) 
33

If you have understood the principles of functions, you have understood 80% of what programming is.

A parameter is required

When you indicate parameters to a function, they must be filled in, otherwise an error will appear.
>>>  def  increase_me ( a ,  b ): 
...      return  30  +  a  +  b 
... 
>>>  increase_me ( 1 ) 
Traceback  ( most  recent  call  last ): 
  File  "<stdin>" ,  line  1 ,  in  < Module > 
TypeError :  augmente_moi ()  takes  exactly  two  arguments  ( 1  Given )

The splat operator

The splat: operator is very often used in python.

def  my_function ( * var ) 
def  my_function ( ** var )
my_function ( * var ) 
my_function ( ** var )

A list in parameter

We can retrieve the values ​​entered via a list:

>>>  def  increase_me ( * param ): 
...      return  param [ 0 ]  +  param [ 1 ]  +  param [ 2 ] 
...  
>>>  increase_me ( 1 ,  2 ,  3 ) 
6 
>>>  increase_me ( 10 ,  20 ,  30 ) 
60

Make mandatory only certain parameters with a list

If you want to make only certain parameters mandatory, you can use the following syntax:

>>>  def  ma_fiche ( first name ,  name ,  * rest ): 
...      return  firstName  +  ""  +  name  
...  
>>>  ma_fiche ( "olive" , "engel" ) 
olive engel '
Note that the " remainder " parameter is preceded by a star .

Use a dictionary for parameters

You can use a dictionary in parameters for this you must add a double star: **

>>>  def  my_fiche ( ** parameters ): 
...      return  parameters [ "first name" ] 
... 
>>>  my_fiche ( first name = "olive tree" ) 
'olive tree'

Using splat list at function call level

Let's take the example of the function again augmente_moi :

>>>  def  increase_me ( * param ): 
...      return  param [ 0 ]  +  param [ 1 ]  +  param [ 2 ] 
... 

We have seen that it is possible to do this:

>>>  increase_me ( 1 ,  2 ,  3 ) 
6

Using the star allows you to go through a list:

>>>  data  =  [ 1 ,  2 ,  3 ] 
>>>  increase_me ( * data ) 
6

Using dictionary splat on function calls

Let's take the example of this function:

>>>  def  test ( firstname = "" ,  lastname = "" ): 
...      return  " {} {} " . format ( firstname , lastname )   

Let's create our dictionary:

>>>  data  =  { 'firstname' : 'olivier' ,  'lastname' : 'engel' }

And send our variable with a star *

>>>  test ( * data ) 
'lastname firstname'

Then with two stars **

>>>  test ( ** data ) 
'olivier engel'

Scope of variables (global variable and local variable)

A variable declared at the root of a module is visible throughout this module. We then speak of a global variable .

>>>  x  =  "hello" 
>>>  def  test (): 
...      print  x 
...  
>>>  test () 
hello

And a variable declared in a function will only be visible in that function. We then speak of a local variable .

>>>  x  =  False 
>>>  def  test (): 
...      x  =  "hello" 
...  
>>>  test () 
>>>  x 
False

Procedure and functions

For your IT culture, know that a function is not required to return a value , in this case we will speak more of a procedure .

No comments:

Post a Comment