Python Intermediate Functions
Python Intermediate Functions
This page covers the intermediate functions of Python programming.
For Python basic functions here.
It continues the previous basic tutorials in more details. Intermediate functions encompass:
- keyword arguments
- default parameters
- “pass” statement
- “return” function
- “lambda” function
Keyword arguments
Through the use of parameters and arguments, functions can circulate a stream of information. Note that the number as well as order of parameters when creating the function must match the number of arguments when calling the function.
Keyword arguments can avoid the order issue (see below).
def person(name, surname): print("My name is " + name) print("And my surname is " + surname) person(surname = "Smith", name = "John")
# Output: My name is John And my surname is Smith
Even though the surname is in the first position, keyword arguments help the function associate the right order.
Default parameters
The function default parameters set the standard value setting. This means that in the case of a missing argument value, the function will produce the default value.
The following is an example of a default parameter.
def person(name, surname = "Smith"): print("My name is " + name) print("And my surname is " + surname) person("John")
# Output: My name is John And my surname is Smith
The default parameter sets the setting in the case of a missing argument value. But different values can pass even with a default parameter.
See below.
def person(name, surname = "Smith"): print("My name is " + name) print("And my surname is " + surname) person("John", surname = "Williams")
# Output: My name is John And my surname is Williams
Note that default parameters always go at the end of the list. Therefore, non-default parameters must always be declared first, otherwise Python produces an error.
def person(surname = "Smith", name): print("My name is " + name) print("And my surname is " + surname) # Output: SyntaxError: non-default argument follows default argument
“Pass” statement
Functions cannot operate without any statements, as the program produces an error. The pass statement allows Python to process empty functions without issues.
The following is an example of an empty function.
def person(): # Output: IndentationError
The following is an example of a function with the “pass” statement.
def person(): pass # Output:
Return statement
The “return” statement inside a function produces (returns) a value. It is commonly used with mathematical expressions.
Below is an example of the function “return” statement.
def num(a, b): return 6 * a + (b / 2) num(2, 6) # Output: 15.0
Lambda function
In Python, “lambda” functions are small (one line) methods. They can have only one output expression, but allow single or multiple arguments.
The keyword is “lambda“, followed by the argument(s), colon, and the expression.
# one argument lambda argument : expression # two arguments lambda argument, argument : expression
The following is an example of one argument lambda.
num = lambda a: 5 * a print(num(2)) # Output: 10
The following is an example of 3 arguments lambda.
num = lambda a, b, c: (a + b + c) / 2 print(num(4, 10, 8)) # Output: 11.0