-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path5
More file actions
101 lines (70 loc) · 2.67 KB
/
5
File metadata and controls
101 lines (70 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Day-5 | Functions & Modules
Functions and modules are fundamental concepts in Python that allow you to write reusable and organized code.
## Functions:
### Definition:
A function is a block of organized, reusable code that performs a specific task. It takes some input (parameters) and returns an output.
### Creating a Function:
```python
def greet(name):
"""
This function prints a greeting message.
"""
print(f"Hello, {name}!")
# Call the function
greet("Alice")
```
### Example with a return statement:
```python
def add(x, y):
"""
This function returns the sum of two numbers.
"""
return x + y
result = add(3, 4)
print(result) # Output: 7
```
## Modules:
### Definition:
A module is a file containing Python definitions and statements. The file name is the module name with the suffix `.py`.
### Creating a Module:
Let's say we create a file named `math_operations.py` with the following content:
```python
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y != 0:
return x / y
else:
return "Error: Division by zero"
```
### Using a Module:
In another Python file, you can import and use the functions from the module:
```python
import math_operations
result_add = math_operations.add(3, 4)
result_subtract = math_operations.subtract(7, 2)
print(result_add) # Output: 7
print(result_subtract) # Output: 5
```
### Using Specific Functions:
You can also import specific functions from a module:
```python
from math_operations import multiply, divide
result_multiply = multiply(5, 6)
result_divide = divide(8, 2)
print(result_multiply) # Output: 30
print(result_divide) # Output: 4.0
```
## Benefits of Functions and Modules:
1. **Reusability**: Functions allow you to write code once and use it multiple times.
2. **Modularity**: Modules help in organizing code into logical units, making it easier to manage.
3. **Abstraction**: Functions allow you to focus on what a piece of code does rather than how it does it.
4. **Readability**: Well-defined functions and modules make code more understandable.
5. **Testing and Debugging**: Functions and modules facilitate easier testing and debugging of code segments.
6. **Collaboration**: Modules enable multiple programmers to work on different parts of a project simultaneously.
Remember to give your functions and modules descriptive names and include comments or docstrings to explain their purpose and usage. This makes your code more understandable and maintainable.
Using functions and modules is a best practice in Python programming and contributes to writing clean, efficient, and organized code.