Introduction
Welcome to my 84th blog post on the Python journey. On day 84, I learned about 'time' module in Python. The 'time' module provides a set of functions that lets you work with time-related operations. Let's dive into more details and understand these concepts in Python with practical examples.
So let's get started......
'time' Module
It performs certain time-related operations, such as timekeeping, formatting, and time conversions.
To access 'time' Module functions we need to import time Module which is available in all python installation making it easier and convenient to use the 'time' Module
time.time()
It returns the current time as a floating-point number,
This time represents the number of seconds since the epoch (the point in time when the time module was initialized).
The returned value may differ based on each computer's system clock and is affected by time adjustments made by the operating system, such as daylight saving time.
Example -
import time
print(time.time())
# Output: 1602299933.233374
This function returns the current time as a floating-point number. It can be used in many applications such as measuring the duration of an operation or the elapsed time since a certain point in time.
time.sleep()
It suspends the execution of the current thread for a specified number of seconds.
It can be used to pause the program for a certain period,
This allows other parts of the program to run, or synchronize the execution of multiple threads.
Example -
import time
print("Start:", time.time())
time.sleep(2)
print("End:", time.time())
# Output:
# Start: 1602299933.233374
# End: 1602299935.233376
In the above code the program pauses for 2 seconds and then resumes.
time.strftime()
It formats a time value as a string, based on a specified format.
This function is particularly useful for formatting dates and times in a human-readable format, such as for display in a GUI, a log file, or a report.
Example -
import time
t = time.localtime()
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", t)
print(formatted_time)
# Output: 2022-11-08 08:45:33
As it can be seen from the above code that the time received from the time.localtime() is formatted using the strftime() function. The formatted string contains codes that represent different parts of the time value, such as the year, the month, the day, the hour, the minute, and the second.
Time module is very important be it writing a script, a library, or an application. This can help you perform time-related tasks with ease and efficiency.
Resources Used
You can watch the video of Day#84 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 84, I learned the time module and its various other operations. Be it any operation, the time module can help you perform time-related tasks with ease and efficiency.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #84
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.