Python Packages _ Datetime Functions & Modules - Notes
Python Packages _ Datetime Functions & Modules - Notes
Modules - Notes
Topics Covered:
● Python Packages
● Python Modules
● Datetime Functions in Python
Python Packages
Syntax:-
import <package_name>
Syntax:-
import <package_name>.<module_name>
1
Install New Package
Syntax :
Python Modules
2
Python import with Renaming
Example:-
Output:-
Here, We have renamed the math module as m. This can save us typing
time in some cases.
Note:- The name math is not recognized in our scope. Hence, math.pi is
invalid, and m.pi is the correct implementation.
Example:-
Output:-
3
Import all names
In Python, we can import all names(definitions) from a module using
the following construct:
Example:-
Output:-
Here, we have imported all the definitions from the math module. This
includes all names visible in our scope except those beginning with an
underscore(private definitions).
Python provides the math module to deal with calculations that include
financial or scientific operations. It includes basic as well as advance
operations such as addition(+), subtraction(-), multiplication(*),
division(/) and trigonometric, logarithmic, exponential functions.
Constants Provided:
● Euler’s Number: math.e
● Pi: math.pi
● Tau: math.tau
● Infinity: math.inf
● Not a Number (NaN): math.nan
4
Math methods:
Random
5
Random Methods:
Collections Library
We have already studied the four collection data types- list, tuples,
sets and dictionary. Other than them, Python also has a builtin
module called as Collection. It has different types of containers which
are objects used to store different objects and provide a way to
access them and iterate over them.
6
Example:-
Output:-
Example:-
Output:-
7
Example:-
Output:-
Example:-
8
Output:-
Python Itertools
Combinatoric Iterators
Example:-
9
Output:-
Example:-
Output:-
10
Combinations(): It prints all the possible combinations (without
replacement).
Example:-
Output:-
11
Combinations_with_replacement(): It returns a subsequence of length
n from the elements of the iterable where n is the argument that
determines the length of the subsequences generated by it. Individual
elements may repeat itself in combinations_with_replacement
function.
Example:-
Output:-
12
Datetime Functions in Python
Syntax:-
import datetime as dt
13
To get current time:
Example:-
Output:-
14
To compare two dates:
Example:-
Output:-
Example:-
15
Addition and Subtraction of date and time
What is the date and time 5 days, 3 hours, 45 minutes and 45 sec
ahead from now?
Example:-
Output:-
What is the date and time 1 week and 3 days before 2019 June 1, 9 AM?
Example:-
16
Output:-
Example:-
Output:-
17
strftime() Method
The method strftime(), takes one parameter, format, to specify the
format of the returned string
Example:-
Output:-
strptime() Method
This function takes two arguments, a string in which some time is given
and a format code, to change the string into, the string is changed to
the DateTime object
Example:-
Output:-
Reference Notebooks
18