Sunday, May 24, 2026

Python Material - Part - 24 - Calendar_time & date_Modules

 __author__ = "Narendra Boyina"

# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
# -----------------------------------------------------------------------------
"""
Topics to be covered in today's class:
--> calendar module
--> isleap
--> leapdays
--> month
--> setfirstweekday

"""

import calendar
# print(calendar.calendar(1991))
# from calendar import calendar
# print(calendar(2024))
# print(calendar(2024, w=3, l=1, c=4))
''' w is the width of each week w=2 mean Mo (or) W=3 means Mon (or) W=6 means Monday
# (Width=2 is always preferable)
# I is the number of lines for each week.(I=2 is always preferable)
# where c is space b/w each month.'''

#################################################################################

# print(calendar.isleap(2024))
# #Returns True if year is a leap year; otherwise, False.
# ################################################################################
# print(calendar.leapdays(1990, 2022)) #2020 is not considered because of range
#Returns the total number of leap days in the years within range(y1,y2)

# print(calendar.month(2024, 7))
# syntax: calendar.month(year,month,w=2,l=1)
# print(calendar.month(2022, 12, w=3, l=1))

##########################################################################################
# calendar.setfirstweekday(6)
# print(calendar.calendar(2024))
# print(calendar.month(2022, 12))
# '''Sets the first day of each week to weekday code weekday.
# # Weekday codes are 0 (Monday) to 6 (Sunday).'''

==================================================================================
                                time_datetime_math

==================================================================================

__author__ = "Narendra Boyina"
# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
# -----------------------------------------------------------------------------
"""
Topics to be covered in today's class:
--> How to import time module
--> time(), asctime(), split(), localtime(), sleep()
--> How to import datetime module

"""
############ time module ######################
import time # internal/ built-in module
# print(time.time()) # gives time frame (machine readable format)

# print(time.asctime()) # # gives time (Human readable format)

'''time.ctime([secs]) is Like asctime(localtime(secs)) and without arguments is like asctime( )'''
# print(time.ctime())

x = "Wed Jul 9 06:55:35 2025"
# y = x.split()
# print(y)
# z = y[-2]
# print(z)
# t = z.split(":")
# print(t)
# print(t[1])

x = time.localtime()

# print(x,"\n",type(x))
# #
# print(x.tm_hour)
# print(x.tm_min)
# print(x.tm_sec)
# print(x.tm_mon)
# print(x.tm_year)
# print(x.tm_hour,":",x.tm_min) # print only hours & minutes
# print(x.tm_hour,":",x.tm_min,":",x.tm_sec ) # print hours, minutes and seconds


'''The method sleep() suspends execution for the given number of seconds.
The argument may be a floating point number to indicate a more precise sleep time.
Syntax: time.sleep(seconds)
'''

#
# print("Narendra your time Starts now at %s " %time.ctime())
# time.sleep(180) # will hold execution for 20 seconds
# print("Narendra your time Ends now at : %s" % time.ctime())


def task():
time.sleep(3.5) # actually I have to write 150 lines code, instead of that I wrote sleep()
print("Subbu")

# tc1_start_time= time.time() # 1st test case starting time
# task()
# print("time taken for task_1: ", time.time()-tc1_start_time)



"""############ datetime module ######################"""

from datetime import datetime

# now = datetime.now() # current date and time
# print(now)
# year = now.strftime("%Y")
# print("year:", year)
# # #
# month = now.strftime("%m")
# print("month:", month)
# # # #
# day = now.strftime("%d")
# print("day:", day)
# # #
# time = now.strftime("%H:%M:%S")
# print("time:", time)
# #
# date_time = now.strftime("%d-%m-%y %H:%M:%S")
# print("date and time:",date_time)
# #
# date_time = now.strftime("%d/%m/%y %H:%M:%S")
# print("date and time:",date_time)
#
# date_time = now.strftime("%d/%m/%y, %H:%M:%S %p") # %p equivalent of either AM or PM.
# print("date and time:",date_time)
#
# date_time = now.strftime("%m/%d/%y, %H:%M %p") # %p equivalent of either AM or PM.
# print("date and time:",date_time)
"""========================="""
# from datetime import datetime

date_string = "2018 July 26"
print(datetime.strptime(date_string, "%Y %B %d"))

""" Commonly used format codes:

%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
"""

""" ############ math module ###################### """

import math
from math import *

print(math.factorial(7))
print(math.sqrt(64)) # square root always provide float value
print(math.log(100))
print(math.exp(5))
print(math.pi) # 22/7 =3.14
print(math.pow(3,4))
print(factorial(5))
print(sqrt(16)) # Bydefault you will get output as a float value
print(int(sqrt(16))) # you will get output as an integer value


from math import factorial
#
f1 = [(factorial(x)) for x in range(10)] # list comprehension
print(f1) # [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
# f2 = [len(str((factorial(x)))) for x in range(10)]
# print(f2) # output: len of factorial of numbers till 10 [1, 1, 1, 1, 2, 3, 3, 4, 5, 6]


# f1 = {(factorial(x)) for x in range(10)} # set comprehension
# print(f1) # {40320, 1, 2, 362880, 6, 720, 5040, 24, 120}
# f2 = {len(str((factorial(x)))) for x in range(10)}
# print(f2) # output: len of factorial of numbers till 10 {1, 2, 3, 4, 5, 6}

Author: Boyina Narendra

Supporting Author: M. Meera Sindhu

Request: If you find this information useful, please provide your valuable comments

No comments:

Post a Comment