Python Lambda
A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only
have one expression.
Syntax
lambda arguments : expression
The expression is executed and the result is returned:
Example
x = lambda a : a + 10
print(x(5))
Lambda functions can take any number of arguments:
x = lambda a, b : a * b
print(x(5, 6))
Summarize argument a, b, and c and return the result:
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
Why Use Lambda Functions?
The power of lambda is better shown when you use them as an
anonymous function inside another function.
Say you have a function definition that takes one argument, and that
argument will be multiplied with an unknown number:
def myfunc(n):
return lambda a : a * n
Python Scope
A variable is only available from inside the region it is created. This is
called scope.
Local Scope
A variable created inside a function belongs to the local scope of that
function, and can only be used inside that function.
Example
A variable created inside a function is available inside that function:
def myfunc():
x = 300
print(x)
myfunc()
Function Inside Function
As explained in the example above, the variable x is not available
outside the function, but it is available for any function inside the
function:
Example
The local variable can be accessed from a function within the
function:
def myfunc():
x = 300
def myinnerfunc():
y=900
print(x)
def myinnerfunc():
print(x)
myinnerfunc()
myfunc()
Global Scope
A variable created in the main body of the Python code is a global
variable and belongs to the global scope.
Global variables are available from within any scope, global and local.
Example
A variable created outside of a function is global and can be used by
anyone:
x = 300
def myfunc():
print(x)
myfunc()
print(x)
Example:
x=900
def myfunc():
x=100
print(x)
def myfunc1():
print(x)
myfunc()
myfunc1()
Naming Variables
If you operate with the same variable name inside and outside of a
function, Python will treat them as two separate variables, one
available in the global scope (outside the function) and one available
in the local scope (inside the function):
Example
The function will print the local x, and then the code will print the
global x:
x = 300
def myfunc():
x = 200
print(x)
myfunc()
print(x)
Python Modules
What is a Module?
Consider a module to be the same as a code library.
A file containing a set of functions you want to include in your
application.
Create a Module
To create a module just save the code you want in a file with the file
extension .py:
Example
Save this code in a file named mymodule.py
def greeting(name):
print("Hello, " + name)
Use a Module
Now we can use the module we just created, by using
the import statement:
Example
Import the module named mymodule, and call the greeting function:
import mymodule
mymodule.greeting("Jonathan")
Variables in Module
The module can contain functions, as already described, but also
variables of all types (arrays, dictionaries, objects etc):
Example
Save this code in the file mymodule.py
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
Import the module named mymodule, and access the person1
dictionary:
import mymodule
a = mymodule.person1["age"]
print(a)
Naming a Module
You can name the module file whatever you like, but it must have the
file extension .py
Re-naming a Module
You can create an alias when you import a module, by using
the as keyword:
Example
Create an alias for mymodule called mx:
import mymodule as mx
a = mx.person1["age"]
print(a)
Built-in Modules
There are several built-in modules in Python, which you can import
whenever you like.
Example
Import and use the platform module:
import platform
x = platform.system()
print(x)
Using the dir() Function
There is a built-in function to list all the function names (or variable
names) in a module. The dir() function:
Example
List all the defined names belonging to the platform module:
import platform
x = dir(platform)
print(x)
Import From Module
You can choose to import only parts from a module, by using
the from keyword.
Example
The module named mymodule has one function and one dictionary:
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
Import only the person1 dictionary from the module:
from mymodule import person1
print (person1["age"])