Chapter-9[Module] (1)
Chapter-9[Module] (1)
What is a Module?
Create a Module
To create a module just save the code you want in a file with the file
extension .py:
Example
def greeting(name):
print("Hello, " + name)
Use a Module
Example
import mymodule
mymodule.greeting("Jonathan")
Run Example »
Example
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
import mymodule
a = mymodule.person1["age"]
print(a)
Run Example »
Naming a Module
You can name the module file whatever you like, but it must have
the file extension .py
Re-naming a Module
Example
import mymodule as mx
a = mx.person1["age"]
print(a)
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:
Example
import platform
x = dir(platform)
print(x)
Example
The module named mymodule has one function and one dictionary:
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
print (person1["age"])
Run Example »
Note: When importing using the from keyword, do not use the
module name when referring to elements in the module.
Example: person1["age"], not mymodule.person1["age"]
Python Dates
A date in Python is not a data type of its own, but we can import a
module named datetime to work with dates as date objects.
Example
import datetime
x = datetime.datetime.now()
print(x)
Date Output
When we execute the code from the example above the result will
be:
2024-12-25 11:14:31.016826
The date contains year, month, day, hour, minute, second, and
microsecond.
Here are a few examples, you will learn more about them later in
this chapter:
Example
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))
Try it Yourself »
Example
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
Try it Yourself »
The datetime() class also takes parameters for time and timezone
(hour, minute, second, microsecond, tzone), but they are optional,
and has a default value of 0, (None for timezone).
The datetime object has a method for formatting date objects into
readable strings.
Example
import datetime
x = datetime.datetime(2018, 6, 1)
print(x.strftime("%B"))
Try it Yourself »
Here are few examples of format codes: #it is a good to remember and can be
used in your projects..
Python Math
The min() and max() functions can be used to find the lowest or
highest value in an iterable:
Example
x = min(5, 10, 25)
y = max(5, 10, 25)
print(x)
print(y)
Example
x = abs(-7.25)
print(x)
Try it Yourself »
Example
x = pow(4, 3)
print(x)
Try it Yourself »
The Math Module
Python has also a built-in module called math, which extends the list
of mathematical functions.
import math
When you have imported the math module, you can start using
methods and constants of the module.
Example
import math
x = math.sqrt(64)
print(x)
Try it Yourself »
Example
import math
x = math.ceil(1.4)
y = math.floor(1.4)
print(x) # returns 2
print(y) # returns 1
Try it Yourself »
Example
import math
x = math.pi
print(x)
Python has a built-in module that you can use to make random
numbers.
The random module has a set of methods:
Syntax
random.random()
Example
import random
print(random.random())
Try it Yourself »
Syntax
random.randrange(start, stop, step)
Example
import random
print(random.randrange(3, 9))
Try it Yourself »
Syntax
random.randint(start, stop)
Example
import random
print(random.randint(3, 9))
Tip: Mean = add up all the given values, then divide by how many
values there are.
Example
Syntax
statistics.median(data)
Example
Syntax
statistics.mode(data)
Example