0% found this document useful (0 votes)
10 views18 pages

Python Time, Random, and Datetime Modules

This document provides an overview of the time, random, and datetime modules in Python, detailing their functionalities and key functions. It explains how to work with time-related tasks, generate random numbers, and manipulate date objects. Examples are provided for each function to illustrate their usage in Python programming.

Uploaded by

ssuresh19747745
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

Python Time, Random, and Datetime Modules

This document provides an overview of the time, random, and datetime modules in Python, detailing their functionalities and key functions. It explains how to work with time-related tasks, generate random numbers, and manipulate date objects. Examples are provided for each function to illustrate their usage in Python programming.

Uploaded by

ssuresh19747745
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Python

Programming
Introduction to time,
random, datetime in
Python
Learning
outcomes:
• What is a time module?
• Functions provided by the time module
• random module
• Functions provided by the random module
• datetime module
• Functions provided by the datetime
module
time Module:
In Python, the time, random, and datetime (for
date) modules are part of the standard library and
provide functionalities related to time, random
numbers, and date/time operations, respectively.
Here's a brief overview of each:
time Module:
The time module provides functions that deal with
time-related tasks.
It allows you to work with time values,
sleep or pause the execution of your
program, and convert between different
representations of time.
time Module:
Here are some key functions provided by the time
module:
time() Function:
Returns the current time in seconds since the
epoch (January 1, 1970).
Example:
import time

current_time = [Link]()
print(current_time)
time Module:
ctime() Function:
Converts a time in seconds since the epoch to a
string representing the local time.
Example:
import time
current_time = [Link]()
local_time_string = [Link](current_time)
print(local_time_string)
time Module:
sleep() Function:

Suspends the execution of the current thread for a


specified number of seconds.
Example:
import time
print("This is printed immediately.")
[Link](5)
print("This is printed after 5 seconds.")
time Module:
gmtime() and localtime() Functions:

Convert a time in seconds since the epoch to a


struct_time in UTC or local time.
Example:
import time
current_time = [Link]()
utc_time = [Link](current_time)
local_time = [Link](current_time)
print("UTC Time:", utc_time)
print("Local Time:", local_time)
time Module:
strftime() Function:
Format a struct_time or time in seconds since the epoch as
a string.
Example:
import time
current_time = [Link]()
formatted_time = [Link]("%Y-%m-%d %H:%M:%S",
[Link](current_time))
print("Formatted Time:", formatted_time)
These are just a few examples of the functionalities
provided by the time module. It's a versatile module that
can be useful in various scenarios, especially when dealing
with time-related operations in your Python programs.
random Module:
The random module in Python provides functions for
generating pseudo-random numbers. These functions
are useful in a variety of applications, such as
simulations, games, and statistical sampling. Here are
some key functions provided by the random module:
random() Function:
Returns a random floating-point number in the range
[0.0, 1.0].
Example:
import random
random_number = [Link]()
print(random_number)
random Module:
randint(a, b) Function:
Returns a random integer in the range [a, b](inclusive).
Example:
import random
random_integer = [Link](1, 10)
print(random_integer)
choice(seq) Function:
Returns a random element from the given sequence
(list, tuple, or string). Example:
import random
fruits = ['apple', 'banana', 'orange', 'grape']
random_fruit = [Link](fruits)
print(random_fruit)
random Module:
shuffle(seq) Function:
Randomly shuffles the elements of the given sequence in
place.
Example: import random
numbers = [1, 2, 3, 4, 5]
[Link](numbers)
print(numbers)
sample(population, k) Function:
Returns a k-length list of unique elements chosen randomly
from the given population.
Example: import random
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random_samples = [Link](numbers, 3)
print(random_samples)
random Module:
uniform(a, b) Function:
Returns a random floating-point number in the range [a, b)
or (a, b] depending on rounding.
Example:
import random
random_float = [Link](1.0, 2.0)
print(random_float)
These functions can be useful when you need to introduce
randomness into your programs or simulate scenarios
where unpredictable values are needed. It's important to
note that the numbers generated by these functions are
pseudorandom, meaning they are generated by algorithms
and not truly random.
datetime Module:
In Python, the date module is part of the datetime module,
which provides classes for working with dates and times.
The date class specifically represents dates (year, month,
and day) and allows various operations related to dates.
Here are some key aspects of the date module:
Importing the Module:
To use the date module, you need to import the datetime
module, as it is part of it.
from datetime import date
Creating Date Objects:
You can create a date object representing a specific date
using the date class constructor. The syntax is as follows:
my_date = date(year, month, day)
datetime Module:
Example:
from datetime import date

# Creating a date object for December 25, 2022


christmas = date(2022, 12, 25)
print(christmas)

Current Date:
You can obtain the current date using the today() method.
from datetime import date
current_date = [Link]()
print(current_date)
datetime Module:
Date Attributes:
Once you have a date object, you can access its
attributes such as year, month, and day
from datetime import date
christmas = date(2022, 12, 25)
print([Link])
print([Link])
print([Link])
datetime Module:
Date Operations:
You can perform various operations on date
objects, such as finding the difference between
two dates.
from datetime import date
christmas = date(2023, 12, 25)
print([Link])
print([Link])
print([Link])

These are some basic functionalities provided


by the datetime module for working with dates
in Python. The datetime module also includes
classes for working with times (time class) and
Thank you

You might also like