Functions
Use def keyword to define a function:
Adding parameters to a function:
Setting default values for the parameters:
Returning Values:
Passing lists as arguments:
Passing a copy of a list as an argument:
Passing an arbitrary number of arguments:
Passing regular and arbitrary number of arguments:
Passing an arbitrary number of key-value pair as arguments:
Passing regular and arbitrary number of key-value pair as arguments:
Storing functions in modules:
Create a file file1.py that contains the definition of two functions:
Importing modules:
Create a file file2.py that imports the module file1 (file1.py):
Importing specific functions defined in a module:
Create a file file3.py that imports specific functions defined in the module file1 (file1.py):
Importing all functions in a module:
Create a file file4.py that imports all functions defined in the module file1 (file1.py):
Using the keyword as to define an alias for a module:
Create a file file5.py that imports the module file1 (file1.py) and give it an alias f1:
Using the keyword as to define an alias for a function:
Create a file file6.py that imports specific functions defined in the module file1 (file1.py) and give them aliases: