Difference between revisions of "Python:Functions"

From wiki
Jump to navigation Jump to search
Line 37: Line 37:
 
:Read and return input from standard input. Display 'prompt' first when provided.
 
:Read and return input from standard input. Display 'prompt' first when provided.
  
;int(str[,base=x)])
+
;int(string[,base=x)])
 
:Convert str to an integer. Base = 2 converts bit-string presentation ('010101').
 
:Convert str to an integer. Base = 2 converts bit-string presentation ('010101').
 +
 +
;str(object)
 +
;repr(object)
 +
:Return a string representation of an object. str for the informal representation (just make it printable), repr for the formal representation (more formating??)
  
 
=Self defined functions=
 
=Self defined functions=

Revision as of 21:33, 27 January 2020


You can pass a function-name as paramter

def main():
    outcome = function1('function2',par2)
    function(par2 = <value>, par1 = <value>)
    return

def function1 (functiontocall, inpar2 = <defaultvalue>):
    result = functiontocall(inpar2)
    return result

def function2 (inpar1, inpar2 = <defaultvalue>):
    codeblock
    return result

main()


Build in function

List of all build in functions [1]

len(obj1)
Return the number of elements in obj1 (string, list, tuple, set)
max(obj1)
Return the largest item in obj1
min(obj1)

Return the smallest item in obj1

input([prompt])
Read and return input from standard input. Display 'prompt' first when provided.
int(string[,base=x)])
Convert str to an integer. Base = 2 converts bit-string presentation ('010101').
str(object)
repr(object)
Return a string representation of an object. str for the informal representation (just make it printable), repr for the formal representation (more formating??)

Self defined functions

Arguments are positional but can be passed by keyword too.

def main():
    outcome = function(par1,par2)
    function(par2 = <value>, par1 = <value>)
    return

def function (inpar1, inpar2 = <defaultvalue>):
    codeblock
    return result

main()


def function (*args,**kwargs)
Take any number of positional arguments and put them in the tuple args
Take any number of keyword arguments and put them in the dict kwargs