Functions and modules in Python
Functions are a way to encapsulate a block of code that can be executed repeatedly in a program. In Python, you define a function using the def
keyword, followed by the function name, and a set of parentheses that may include one or more parameters. Here is an example:
def greet(name):
print('Hello, ' + name + '!')
greet('Alice') # Output: Hello, Alice!
In this example, we define a function called greet()
that takes one parameter, name
, and uses the print()
function to output a greeting.
Default Parameter Values
You can also provide default values for parameters in a function, so that they don't have to be specified every time the function is called. Here is an example:
def greet(name='world'):
print('Hello, ' + name + '!')
greet() # Output: Hello, world!
greet('Alice') # Output: Hello, Alice!
In this example, the greet()
function has a default parameter value of 'world'
, which is used if no argument is passed to the function. If an argument is passed, it overrides the default value.
Return Values
Functions can also return a value using the return
keyword. Here is an example:
def add(a, b):
return a + b
result = add(2, 3)
print(result) # Output: 5
In this example, the add()
function takes two parameters, a
and b
, and returns their sum using the return
keyword. The returned value is then stored in the result
variable and printed to the console.
Modules
A module is a file that contains Python code, and can include functions, variables, and other objects. In Python, you can import a module using the import
keyword, followed by the name of the module. Here is an example:
import math
result = math.sqrt(16)
print(result) # Output: 4.0
In this example, we import the math
module and use its sqrt()
function to calculate the square root of 16.
Importing Specific Objects
You can also import specific objects from a module using the from
keyword. Here is an example:
from math import pi
print(pi) # Output: 3.141592653589793
In this example, we import only the pi
constant from the math
module, and print its value to the console.
Creating Your Own Modules
You can create your own modules by writing Python code in a file and saving it with a .py
extension. Here is an example of a module called my_module.py
:
def greet(name):
print('Hello, ' + name + '!')
def add(a, b):
return a + b
PI = 3.14159
In this example, we define two functions (greet()
and add()
) and a variable (PI
) in a module called my_module
. To use the functions and variable in another file, we can import them using the import
statement:
import my_module
my_module.greet('Alice') # Output: Hello, Alice!
result = my_module.add(2, 3)
print(result) # Output: 5
print(my_module.PI) # Output: 3.14159
In this example, we import the my_module
module and use its greet()
, add()
, and PI
objects in our program.
You can also import specific objects from your module using the from
keyword:
from my_module import greet, PI
greet('Alice') # Output: Hello, Alice!
print(PI) # Output: 3.14159
In this example, we import only the greet()
and PI
objects from the my_module
module.
Conclusion
Functions and modules are powerful concepts in Python that allow you to write reusable and modular code. By defining your own functions and modules, and importing them into your programs, you can write more efficient and effective code that is easier to read and maintain.