Introduction to Functions and Modules
In Python, functions are blocks of code that perform a specific task and can be reused throughout a program. They allow for better organization and reusability of code. Modules, on the other hand, are files that contain Python code, which define functions, classes, and variables. They help in organizing code into logical units that can be easily imported and used in other parts of the program.
Functions in Python
Functions in Python are defined using the def
keyword followed by the function name and parentheses containing any parameters. The body of the function is indented and can contain any number of statements. Functions can return values using the return
statement. Here is an example of a simple function that returns the sum of two numbers:
1
2
def add_numbers(a, b):
return a + b
Modules in Python
Modules are used to group related functions, classes, and variables together. Modules are created by saving Python code in a file with a .py
extension. Functions and classes defined in a module can be accessed by using the import
statement followed by the module name. Here is an example of a module named math_operations.py
containing a function to calculate the square of a number:
1
2
3
# math_operations.py
def square(num):
return num ** 2
Example of Using Functions and Modules
You can use functions and modules together to create a program that performs various math operations. Here is an example of how to import the math_operations
module and use the square
function to calculate the square of a number:
1
2
3
4
import math_operations
result = math_operations.square(5)
print(result) # Output: 25
Conclusion
Functions and modules are essential building blocks in Python programming that help in creating organized and reusable code. By understanding how to define functions and create modules, you can improve the efficiency and readability of your Python programs. Start practicing by creating your own functions and modules to enhance your programming skills.