python libraries.
- Details
- Category: Uncategorised
- Published: Tuesday, 06 July 2021 06:34
- Written by Super User
- Hits: 730
What is a Module?
A module is same as a code library. A file containing a set of functions you want to include in your application. you can also define variables, objects and dictionaries in them. you can save a module with .py extension. a module can be used in your program using import keyword. there are several ways to use import. Let us see example below.
Example
Save this code in a file named mymoduleexample.py
def hello(name):
print("Hello : " + name)
Now use the module we just created, by using the import
statement
import mymoduleexample
mymoduleexample.hello("Vijay Singh") # syntax to acess module methods: module_name.function_name.
The module can also contain variables of all types (lists, dictionaries, objects etc) see below example
Save below code in the file mymoduleexample.py
employee1 = {
"name": "vijay",
"salary": 36000,
"country": "india"
}
now you can Import the module named mymoduleexample, and access the employee1 dictionary:
import mymoduleexample
place = mymodule.employee1["country"]
print(place)
You can create an alias of module when you import a module, by using the as keyword
Example
Create an alias for mymoduleexample
called my_ex
:
import mymoduleexample as my_ex
a = my_ex.employee1["name"]
print(a)