Functions in Python:
Functions are the primary method of code organization and reuse in Python.Functions are groups of code that have a name, and can be called using parentheses. We've seen functions before. For example, ``type`` is a function:
Here 'type' is the function name, and 'abc' is the function's *argument*. Functions become even more useful when we begin to define our own, organizing functionality to be used in multiple places.
The built-in sequence functions of Python are as follows:
Now we have a function named 'fibonacci' which takes a single argument 'N', does something with this argument, and 'return's' a value; in this case, a list of the first 'N' Fibonacci numbers:
Function overloading is not permitted
A function should always have a “return” value.
If “return” is not defined, then it returns “None.”
Functions are the primary method of code organization and reuse in Python.Functions are groups of code that have a name, and can be called using parentheses. We've seen functions before. For example, ``type`` is a function:
Here 'type' is the function name, and 'abc' is the function's *argument*. Functions become even more useful when we begin to define our own, organizing functionality to be used in multiple places.
The built-in sequence functions of Python are as follows:
- Enumerate:- Indexes data to keep track of indices and corresponding data mapping
- Sorted:- Returns the new sorted list for the given sequence
- Reversed:-Iterates the data in reverse order
- Zip:- Creates lists of tuples by pairing up elements of lists, tuples, or other sequence
Now we have a function named 'fibonacci' which takes a single argument 'N', does something with this argument, and 'return's' a value; in this case, a list of the first 'N' Fibonacci numbers:
Function overloading is not permitted
A function should always have a “return” value.
If “return” is not defined, then it returns “None.”
Default Argument Values
Often when defining a function, there are certain values that we want the function to use most of the time, but we'd also like to give the user some flexibility. In this case, we can use default values for arguments. Consider the
fibonacci
function from before. What if we would like the user to be able to play with the starting values? We could do that as follows:
*args and **kwargs: Flexible Arguments
Sometimes you might wish to write a function in which you don't initially know how many arguments the user will pass. In this case, you can use the special form *args and **kwargs to catch all arguments that are passed. Here is an example:
Anonymous (lambda) Functions
Earlier we quickly covered the most common way of defining functions, the def statement. You'll likely come across another way of defining short, one-off functions with the lambda statement. It looks something like this:
As an example of this, suppose we have some data stored in a list of dictionaries,dictionaries are not orderable: we need a way to tell the function how to sort our data. We can do this by specifying the
As an example of this, suppose we have some data stored in a list of dictionaries,dictionaries are not orderable: we need a way to tell the function how to sort our data. We can do this by specifying the
key
function, a function which given an item returns the sorting key for that item:
No comments:
Post a Comment
Note: only a member of this blog may post a comment.