8_time, random, datetime
8_time, random, datetime
datetime in Python
Learning objective
• What is a time module?
• Functions provided by time module
• Random module
• Functions provided by random module
• Datetime module
• Functions provided by 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.
import time
current_time = time.time()
print(current_time)
import time
start_time = time.time() # Record start time
# Some code that you want to time
import time
current_time = time.time()
local_time_string = time.ctime(current_time)
print(local_time_string)
time module
• sleep() Function:
• Suspends the execution of the current thread for a specified
number of seconds.
import time
print("This is printed immediately.")
time.sleep(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.
import time
current_time = time.time()
utc_time = time.gmtime(current_time)
local_time = time.localtime(current_time)
print("UTC Time:", utc_time)
print("Local Time:", local_time)
time module
• Format a struct_time or time in seconds since the epoch as a
string.
import time
current_time = time.time()
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime(current_time))
print("Formatted Time:", formatted_time)
time module
• These are just a few examples of the functionalities provided by
the time module.
import random
random_number = random.random()
print(random_number)
random module
• randint(a, b) Function:
• Returns a random integer in the range [a, b](inclusive).
import random
random_integer = random.randint(1, 10)
print(random_integer)
random module
• choice(seq) Function:
• Returns a random element from the given sequence (list, tuple, or
string).
import random
fruits = ['apple', 'banana', 'orange', 'grape']
random_fruit = random.choice(fruits)
print(random_fruit)
random module
• shuffle(seq) Function:
• Randomly shuffles the elements of the given sequence in place.
import random
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers)
random module
• sample(population, k) Function:
• Returns a k-length list of unique elements chosen randomly from
the given population.
import random
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random_samples = random.sample(numbers, 3)
print(random_samples)
random module
• uniform(a, b) Function:
• Returns a random floating-point number in the range (a, b).
• 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.
import random
random_float = random.uniform(1.0, 2.0)
print(random_float)
datetime module
• In Python, the date module is part of the datetime module, which
provides classes for working with dates and times.
• Current Date:
• You can obtain the current date using the today() method.
from datetime import date
current_date = date.today()
print(current_date)
date attributes
Date Attributes:
Once you have a date object, you can access its attributes
such as year, month, and day
• These are some basic functionalities provided by the datetime module for working
with dates in Python.
You must have learnt:
• What is a time module?
• Functions provided by time module
• Random module
• Functions provided by random module
• Datetime module
• Functions provided by datetime module