-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path48_Import_Module.py
More file actions
51 lines (36 loc) · 1.15 KB
/
48_Import_Module.py
File metadata and controls
51 lines (36 loc) · 1.15 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
"""Import module in Python
In Python, modules help organize code into reusable files.
They allow you to import and use functions, classes and variables from other scripts."""
# Importing built-in Module
import math
pie = math.pi
print("Value of pi:", pie)
"""Importing External Modules
To use external modules, we need to install them first, we can easily install any external module using pip command in the terminal."""
# Example
"""pip install pandas"""
import pandas
# Create a simple DataFrame
data = {
"Name": ["Elon", "Trevor", "Swastik"],
"Age": [25, 30, 35]
}
df = pandas.DataFrame(data)
print(df)
# Importing Specific Functions
from math import pi
print(pi)
# Importing Modules with Aliases
import math as m
result = m.sqrt(25)
print("Square root of 25:", result)
# Importing Everything from a Module (*)
from math import *
print(pi) # Accessing the constant 'pi'
print(factorial(6)) # Using the factorial function
# Handling Import Errors in Python
try:
import mathematics # Incorrect module name
print(mathematics.pi)
except ImportError:
print("Module not found! Please check the module name or install it if necessary.")