python QB unit 3
python QB unit 3
Local variables are those that are initialized within a function and are unique to that function. It
cannot be accessed outside of the function.
Ex: def f():
# local variable
s = "Hello"
print(s)
# Driver code
f()
output:Hello
Global variables are the ones that are defined and declared outside any function and are not
specified to any function. They can be used by any part of the program.
Example:
# This function uses global variable s
def f():
print(s)
# Global scope
s = "hello"
f()
output:hello
A lambda function can take any number of arguments, but can only have one expression.
Syntax
lambda arguments : expression
Ex: x = lambda a : a + 10
print(x(5))
Output:15
Q4)Explain module.
Ans: Python Module is a file that contains built-in functions, classes,its and variables. There
are many Python modules, each with its specific work.
Create a Python Module:
To create a Python module, write the desired code and save that in a file with .py extension.
Let’s understand it better with an example:
Example:
Let’s create a simple calc.py in which we define two functions, one add and another subtract.
print(calc.add(10, 2))
Output:
12