Day overview:
Functions
- Purpose
- The basic recipe and calling a function
- Arguments
- Variable scopes
- Returning values from a function
- Lambda (anonymous) function
I/O Input output
- Input from user/keyboard
- Reading files
- Writing files
- Closing files
Purpose
Functions - pieces of code that are written one time and reused as much as desired within the program. They:
- Are the simplest callable object in python
- Perfom single related actions that can handle repetitive tasks
- Significantly reduce code redundancy and complexity, while providing a clean structure
- Decompose complex problems into simpler pieces
Purpose
- Supose you have a protein sequence and want to find out the frequency of the "W" amino acid and all its positions in the sequence.
Purpose
- Now you may want to know the same information about, say "P". You would need to re-write your entire code again for "P"...
And 19 more times to accomodate all other amino acids!!
Purpose
- Using a function, the problem can be easily solved like this:
With only 7 lines of code, we are now able to provide the required information for all amino acids and for any input sequence.
The basic recipe
- The basic steps when defining a function:
- "def" - Functions must start with the "def" keyword.
- "name" - The name of the function must not contain special characters or whitespaces
- "()" - Parenthesis enclose input parameters or arguments
- ":" - The code block within every function starts with a colon and is indented
- Documentation [optional] - It is good practice to document your function
- "statements" - The actual code block of your function
Function calling
- After a function is defined, it represents nothing more than an idle piece of code, unless called. It is only when we call a function that the statements inside the function body are executed.
Arguments
A function can be created without arguments,
or using the following types of arguments:
- Required arguments
- Default arguments
- Variable length arguments
Arguments
Required arguments
- When calling for a function with required arguments, the exact same number of arguments must be specified, no more and no less.
Arguments
Required arguments
- It is also possible to provide previously named variables as arguments
- If you specify a different number of arguments, however
Arguments
Variable length arguments
- Placing an asterisk (*) before the variable name will store the arguments in a tuple
Arguments
Default arguments
- Arguments can also have default values, by assigning those values to the argument keyword with the assign ("=") symbol.
- The function will assume the default value if the argument keyword is not specified when calling the function.
Arguments
Using argument keywords
- When calling a function, the order of the arguments can be changed by using the argument's keyword and the assign ("=") symbol.
- Note that this is necessary if you would like to change only the second default argument, and leave the first with the default value
Arguments
Considerations when combining different argument types
- Default arguments should come after required arguments
- Variable length arguments should be used only once and be always last. There is also no point in using them with default arguments.
Namespaces or scope of variables
When writting a program, it is extremely important to know the difference between the local and global scope of the variables
Glogal variables
- Variables defined outside functions or other objects (i.e., classes) are global variables - they are accessible throughout most of the program, even by functions.
- To change the contents of a global variable in a function, we can use the global keyword
Namespaces or scope of variables
Local variables
- By default, all variables defined inside a function (including argument keywords) are local variables - they are not accessible by the whole program, only within the function where they are declared.
- Note that without the global keyword, global variables are overwritten by local variables with the same name defined in a function
Return
The return keyword is used to return values from a function, which can then be assigned to new variables that are accessible to the whole program
Return
Returning multiple values
- Functions can return multiple values
- And these values can be assigned to multiple variables
Return
Functions always return something
If a function does not contain the return keyword, it will return None
Lambda (anonymous) functions
Lambda is an anonymous (unnamed) function that is used primarily to write very short functions that are a hassle to define in the normal way. Where a regular function would do:
a lambda function:
The lambda function can be used elegantly with other functional parts of the Python language, like map(). In this example we can use it to convert a list of RNA sequences into DNA sequences:
Wrap up
So, we have covered thus far:
- How to define functions using the def keyword
- How to call a function
- The three main types of arguments a function can take: Required , variable length and arguments
- The local and global scope of variables
- The usage of the return keyword to return values from functions
- Lambda functions
←→/
#