datetime Module:
Q. Why do we require datetime module?
Ans: The datetime module is very important module in python for any type of application that deals with date and time related functionality.
This library provides classes for manipulating dates and times. We can perform various operations such as date arithmetic, Formatting and parsing.
Q. What are the applications where i can use datetime module functionality?
Ans: Scheduling and Reminders application, Formatting dates for reports and user interface, Logging and Timestamps, Data analysis with time-based data.
Q. List the important classes in datetime module?
Ans: Following are the important key classes in datetime module.
- datetime: Represents date and time (year, month, day, hours, minutes, seconds, microseconds)
- date: Represents only the date (year, month, day)
- time: Represents only time (hours, minutes, seconds, microseconds)
- timedelta: Represents a duration, used for date and time arithmetic.
- tzinfo: An abstract base class for dealing with time zone.
Q. How do i display current date and time using datetime module?
Ans: first import the module in your program to use its inbuilt functionality.
from datetime import datetime
current_date_time = datetime. now ()
print ("Current date and Time", current_date_time)
Q. How to format date and time object?
# Format the datetime object
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print ("Formatted date and time:", formatted)
Q. What if you have given datetime in string format, can you parse it into date time object?
Ans:
# Parse a date string
date_string = "2024-11-25 14:30:00"
parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print ("Parsed date and time:", parsed_date)
Q. Can we add 7 days in the current date, and can we subtract 30 minutes from current time in python using datetime module?
Ans: Yes, we can do this as follows.
from datetime import timedelta
# Add 7 days to the current date
future_date = now + timedelta(days=7)
print ("Future date:", future_date)
# Subtract 30 minutes from the current time
past_time = now - timedelta(minutes=30)
print ("Past time:", past_time)
Q. Can we compare date and time using datetime module?
Ans: Yes, we can compare date and time as follows.
# Compare two dates or times
if parsed_date > now:
print ("Parsed date is in the future")
else:
print ("Parsed date is in the past")
-------------------------------------------------------------------------------
random Module:
Q. Why do we require random Module?
Ans: If we want to generate random number and wants to perform randomization task then in that case, we can use this python module to do the same. Through this module we can generate random floating point, integer number also we generate random sampling.
Q. How i can generate random floating-point number between 0.0 to 1.0.
Ans:
import random
# Generate a random float
print(random.random())
Q. How i can generate random integer number between a and b.
Ans:
# Generate a random integer between 1 and 10
print (random.randint(1, 10))
Q. How i can generate random floating-point number between a and b.
Ans:
# Generate a random float between 5 and 10
print (random.uniform(5, 10))
Q. can i randomly choose elements from the non-empty sequence like list or tuple.
Ans: Yes, we can do this by using choice () inside random module.
# Random choice from a list
day_of_marriage = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
print (random.choice(day_of_marriage))
Q. can we shuffle the elements inside list.
Ans: Yes, we can shuffle the elements inside the list as follows.
numbers = [11, 22, 33, 44, 55]
random.shuffle(numbers)
print(numbers)
Q. can we use random module for dice roll?
Ans: Yes, we can use randint () function of random module.
print ("Roll a dice:", random.randint(1, 6))
Q. can we use random module for random password generation?
Ans:
import string
characters = string.ascii_letters + string.digits + string.punctuation
password = ''. join (random.choices(characters, k=12))
print ("Generated password:", password)
Q. can we use random module Lottery unique number from specified range?
Ans: Yes, you can.
# Select 6 unique numbers from 1 to 99
print ("Lottery numbers:", random.sample(range (1, 100), 6))
No comments:
Post a Comment