-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31_modules_tutorial.py
More file actions
141 lines (108 loc) · 2.99 KB
/
31_modules_tutorial.py
File metadata and controls
141 lines (108 loc) · 2.99 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Modules and Packages in Python
# ========== IMPORTING MODULES ==========
# 1. Importing entire module
import math
print(f"Pi: {math.pi}")
print(f"Square root: {math.sqrt(16)}")
# 2. Import with alias
import math as m
print(f"Cosine: {m.cos(0)}")
# 3. Import specific functions/objects
from math import pi, sqrt
print(f"Pi: {pi}")
print(f"Square root of 25: {sqrt(25)}")
# 4. Import everything (not recommended!)
# from math import *
# 5. Import with alias
from math import sqrt as square_root
print(square_root(9))
# ========== STANDARD MODULES ==========
# 6. os - operating system
import os
print(f"Current directory: {os.getcwd()}")
# 7. sys - system parameters
import sys
print(f"Python version: {sys.version}")
print(f"Platform: {sys.platform}")
# 8. random - random numbers
import random
print(f"Random number: {random.randint(1, 10)}")
print(f"Random choice: {random.choice(['a', 'b', 'c'])}")
# 9. datetime - working with dates
from datetime import datetime, timedelta
now = datetime.now()
print(f"Now: {now}")
tomorrow = now + timedelta(days=1)
print(f"Tomorrow: {tomorrow}")
# 10. collections - special collections
from collections import Counter, defaultdict, namedtuple
# Counter
words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
count = Counter(words)
print(f"Counter: {count}")
# defaultdict
d = defaultdict(int)
d['key'] += 1
print(f"defaultdict: {d}")
# namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
print(f"Point: {p.x}, {p.y}")
# ========== CREATING YOUR OWN MODULES ==========
# 11. Create a file my_module.py:
# def greet(name):
# return f"Hello, {name}!"
#
# PI = 3.14159
# Import it:
# import my_module
# print(my_module.greet("Pavel"))
# print(my_module.PI)
# ========== PACKAGES ==========
# 12. Package structure:
# my_package/
# __init__.py
# module1.py
# module2.py
# subpackage/
# __init__.py
# module3.py
# Import from package:
# from my_package import module1
# from my_package.subpackage import module3
# ========== __name__ and __main__ ==========
# 13. Check if module is run directly
if __name__ == "__main__":
print("Module is run directly")
else:
print("Module is imported")
# ========== INSTALLING PACKAGES ==========
# 14. pip - package manager
# pip install requests
# pip install numpy
# pip list
# pip uninstall package_name
# ========== VIRTUAL ENVIRONMENTS ==========
# 15. Creating a virtual environment
# python -m venv myenv
# Activation:
# Windows: myenv\\Scripts\\activate
# Linux/Mac: source myenv/bin/activate
# Deactivation:
# deactivate
# ========== USEFUL MODULES ==========
# json - working with JSON
import json
data = {"name": "Pavel", "age": 25}
json_string = json.dumps(data)
print(f"JSON: {json_string}")
# re - regular expressions
import re
pattern = r'\d+'
text = "I have 5 apples and 10 bananas"
numbers = re.findall(pattern, text)
print(f"Numbers: {numbers}")
# pathlib - working with paths
from pathlib import Path
path = Path('..')
print(f"Files: {list(path.glob('*.py'))}")