Chapter 9 : Other Important Topics
Python range( ) function returns a sequence of integer numbers. In order to traverse through the number we need for
loop.
range ( start, stop )
or
range ( start, stop , step)
Please note:
The default value of start
is 0.
Example:
Code explanation:
Output
9.2: How to get date, time and time zone in Python
In order to work with date
and time
Python provides us with datetime
and time
modules.
But among the two modules, I prefer using time
module because we
can not only get the date and time but we can also access other information like time zone.
Example:
In the above piece of code, we get the current local time using
localtime(
)
function and then format the time using
strftime(
)
function
What is
localtime( )
function in Python?
The
localtime( )
function is used to convert a time ( in seconds ) since the
epoch
to a
struct_time.
time.localtime( t ) ,
where t is the time in seconds.
If t is not provided, then the
localtime( )
function returns the current date and time.
In Python, time is measured from 1
st
Jan 12.00 AM, 1970 and this very time is termed as
epoch
.
struct_time
is a
tuple
containing 8 elements and they can be accessed by their index value and attribute names.
The elements are as follows:
|
|
|
|
|
|
|
|
|
|
|
Day of month – from 1 to 31
|
|
|
|
|
|
|
|
|
|
|
|
Day of the week – from 0 to 6 – Monday 0, Tuesday 1 and so on
|
|
|
Day of the year – from 1 to 366
|
|
|
daylight saving flag (0, 1 or -1). A value of 1 indicates that the daylight savings is in effect, 0 if daylight savings is not in effect and -1 if the information is not available.
|
Along with these 8 elements, there is a newly added attribute named
tm_zone
.
What is
strftime( )
in Python?
strftime( )
is used to convert a
struct_time
to a
string
as specified by the
format
argument
.
time.strftime(“
format_code
”, t),
w
here t is the time to be
formatted.
Commonly used format codes are:
Format Code
|
Value
|
%A
|
returns weekday. Example: Monday
|
%d
|
returns day of the month in number. From 0 to 31
|
%B
|
returns month. Example: December
|
%m
|
returns month in number. Example: from 0 to 12
|
%Y
|
returns year. Example: 2020
|
%H
|
returns hours from 00 - 23. Example: from 00 to 23
|
%M
|
returns minutes from 00-59. Example:30
|
%S
|
returns seconds from 00-59. Example: 20
|
%T
|
returns current time, equal to %H:%M:%S
|
%Z
|
returns timezone.
|
Now let's run the above piece of code,
The date, time and timezone shows perfectly.
It is very important to add logs at places that will aid with understanding the program flow and the point of failure in various environments (as in dev, staging, prod, etc)
By default, Python skips logging information from severity level DEBUG
and INFO
and prints only from severity level WARNING
and above.
In order to make Python print all the log messages from all severity level, we need to configure the logging by using
basicConfig( )
method
.
Some of the commonly used parameters
for
basicConfig( )
method are as follows:
-
level
: This specifies the root severity level from which the log messages will start displaying.
-
filename
: This specifies the log file.
-
filemode
: This specifies the mode in which the log file will open.
w
means write mode The default is
a
, which means
append
.
-
format
: This specifies the format of the log message which will appear on the log file.
Example:
Code explanation:
Now let’s run the above piece of code.
The log file generated successfully.
9.4: What does the line if__name__ == __main__ mean in Python?
if
__name__
== “__main__”
, mean that if a Python file is running directly as the main
python
file and not imported then set the value of python special variable
__name__
equal to __main__
.
main( )
function is executed when the class
is executed as an independent unit.
Let us consider the example below,
Code explanation:
(To understand Python class, please refer to Chapter 9 )
Now let’s run the above piece of code
In this file, we imported the
numbers
module created above.
Now let’s run the
letters.py
file.
After running the
letters.py
, we got three outputs:
Output 1:
The first output is generated from the
numbers
module. Since
numbers
module is running from
letters
module and not running directly, then the variable
__name__
of
numbers
module is not equal to
“__main__”,
hence we get the output “
Numbers module is imported
” .
(Please refer to code present in numbers.py)
Output 2:
The second output is generated from
letters
module. Since the
letters
module is running directly as the
main
python file so the variable
__name__
of
letters
module becomes equal to
“__main__”,
hence we get the output “
Letters module is not imported
”