-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_module.py
57 lines (39 loc) · 1.56 KB
/
python_module.py
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
# Hello World program in Python
print("Example of Python Module")
# Consider a module to be the same as a code library.
# A file containing a set of functions you want to include in your application.
# Create a Module---
# To create a module just save the code you want in a file with the file extension .py:
# Save this code in a file named mymodule.py
# (((((-> def greeting(name):
# print("Hello, " + name) <-))))))
# Use a Module----
# Now we can use the module we just created, by using the import statement:
import mymodule
mymodule.greeting("Alex")
# Variables in Module----
# The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc):
# Import the module named mymodule, and access the employee dictionary:
import mymodule
a = mymodule.employee["age"]
print(a)
# You can create an alias when you import a module, by using the as keyword:
import mymodule as mx
a = mx.employee["age"]
print(a)
# Built-in Modules
# There are several built-in modules in Python, which you can import whenever you like.
# Import and use the platform module:
import platform
x = platform.system()
print(x)
print("Using the dir() Function")
# There is a built-in function to list all the function names (or variable names) in a module. The dir() function:
import platform
x = dir(platform)
print(x)
print("Import From Module")
# You can choose to import only parts from a module, by using the from keyword.
# The module named mymodule has one function and one dictionary:
from mymodule import employee
print(employee["age"])