Python Modules Tutorial for Beginners
1. Importing a module
import math
2. Using a function from a module
import math print(math.sqrt(16)) # Output: 4.0
3. Using a constant from a module
import math print(math.pi) # Output: 3.141592653589793
4. Importing only a specific item from a module
from math import pi print(pi) # Output: 3.141592653589793
5. Importing multiple items from a module
from math import pi, sqrt print(pi) # Output: 3.141592653589793 print(sqrt(16)) # Output: 4.0
6. Renaming imported items
from math import sqrt as square_root print(square_root(16)) # Output: 4.0
7. Importing all items from a module
from math import * print(pi) # Output: 3.141592653589793 print(sqrt(16)) # Output: 4.0
8. Checking the items defined in a module
import math print(dir(math))
9. Creating a custom module
def greet(name): return f"Hello, {name}!"
10. Importing a custom module
import mymodule print(mymodule.greet("Alice")) # Output: Hello, Alice!