Lesson-10: Python time module and Sleep function

Python time module and Sleep function

In the Python Time module and Sleep function lesson, we will talk about the Time module that comes ready with python. In addition, we will include the sleep function, which is often used in this module. A module that contains information and operations related to time is the time module in Python programming.

First, we need to import this module into our program. import time we can import the time module with the code line. Now let’s look at the functions contained in this module.

time.time() Function:

the time function is used to calculate the time from the beginning of time to the moment we are in. as a start, the Year 1 January 1970 is usually based on. Its usage is as follows.

import time

print (time.time())

screen shot:
1606773212.152462

Process finished with exit code 0

time.gmtime( ) Function:

We use the value calculated by the time function above to make it meaningful. Below is the method of use.

import time

zaman=time.time()
print(time.gmtime(zaman))

screen shot
time.struct_time(tm_year=2020, tm_mon=11, tm_mday=30, tm_hour=21, tm_min=57, tm_sec=20, tm_wday=0, tm_yday=335, tm_isdst=0)

Process finished with exit code 0

As can be seen, the gmtime function was divided into years, months, days, hours, minutes, seconds with time information.

time.localtime( ) Function:

It gives us local time by addressing the time difference between countries. Its usage is as follows.

import time

zaman=time.localtime()
print (zaman)

screen shot:
time.struct_time(tm_year=2020, tm_mon=12, tm_mday=1, tm_hour=1, tm_min=0, tm_sec=3, tm_wday=1, tm_yday=336, tm_isdst=0)

Process finished with exit code 0

time.asctime( ) Function:

This function gives us time information in a slightly more regular structure. Below is its use.

import time

zaman=time.asctime()
print (zaman)

screen shot:
Tue Dec  1 01:02:21 2020

Process finished with exit code 0

If you look at the screen output, the date and time information has been output a little more regularly.

time.strftime ( ) Function:

With this function, it can be divided into the above structure parts. With the parameters we will write in it, we can access the information of the day, month, year, hour, minute, second separately. Below these parameters are given in detail. In the video at the beginning of the course, this narration was made.

import time

zaman=time.strftime('%A')
print (zaman)

EKRAN ÇIKTISI:
Tuesday

As can be seen, when “A” is entered as a parameter, it gives us information about the day. Below these parameters are described in detail. You can use these parameters by trying.

% a shortened week day
% A week full day name
% b abbreviated name of the month
% B full month name
% c Full date, time and time information
% d Day numerically
%j is the day of the year
% m numerically month
%U what week is
%y last two figures of the year
% Y full four-digit state of the year
% X full date information
% X full time information

time.sleep( ) Function:

It is one of the most commonly used functions in the Time module. It helps to keep the code functioning for a certain period of time. See the following example. In the video narration, the sleep function is also included in detail.

import time

for i in range (10,0):
    print (i)
    time.sleep(2)

print ("Process Finished...")

In the above code structure, there is a loop that starts from 10 and rotates until 0, and each time it prints the variable i on the screen and waits 2 seconds. Hold is made with Sleep function.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *