0% found this document useful (0 votes)
2 views

DateTime_Math

The document provides an overview of Python's datetime and math modules, detailing their functionalities and usage. It includes examples of how to get the current date and time, as well as a list of functions available in the math module for performing various mathematical operations. The document emphasizes that the math module is built-in and does not require separate installation.

Uploaded by

rathodevv6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DateTime_Math

The document provides an overview of Python's datetime and math modules, detailing their functionalities and usage. It includes examples of how to get the current date and time, as well as a list of functions available in the math module for performing various mathematical operations. The document emphasizes that the math module is built-in and does not require separate installation.

Uploaded by

rathodevv6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MMN Python

Modules – datetime, math

Datetime:
 Python has a module named datetime to work with dates and times.
 It provides a variety of classes for representing and manipulating dates and
times, as well as formatting and parsing dates and times in a variety of formats.

Example 1: Get Current Date and Time


import datetime

# get the current date and time


now = datetime.datetime.now()

print(now)

Output

2023-03-08 09:31:26.137671

 Here, we have imported the datetime module using the import


datetime statement.
 One of the classes defined in the datetime module is the datetime class.
 We then used the now() method to create a datetime object containing the
current local date and time.

Example 2: Get Current Date


import datetime

# get current date


current_date = datetime.date.today()

print(current_date)

Output

2023-03-08

Mr. D.S.Kiwde Page 1


MMN Python

In the above example, we have used the today() method defined in


the date class to get a date object containing the current local date.

Example: Print today's year, month and day

We can get year, month, day, day of the week etc. from the date object easily.
For example,
from datetime import date

# date object of today's date


today = date.today()

print("Current year:", today.year)


print("Current month:", today.month)
print("Current day:", today.day)

Output

Current year: 2023


Current month: 3
Current day: 8

Math Module:
 Python has a built-in math module.
 It is a standard module, so we don't need to install it separately.
 We only have to import it into the program we want to use.
 We can import the module, like any other module of Python, using import math
to implement the functions to perform mathematical operations.
 This module does not support complex datatypes.

It gives access to the underlying C library functions. For example,

# Square root calculation

import math
math.sqrt(4)

Mr. D.S.Kiwde Page 2


MMN Python

Functions in Python Math Module

Here is the list of some of the functions and attributes defined in math module
with a brief explanation of what they do.

List of Functions in Python Math Module

Function Description

ceil(x) Returns the smallest integer greater than or equal to x.

fabs(x) Returns the absolute value of x

factorial(x) Returns the factorial of x

floor(x) Returns the largest integer less than or equal to x

isinf(x) Returns True if x is a positive or negative infinity

modf(x) Returns the fractional and integer parts of x

trunc(x) Returns the truncated integer value of x

exp(x) Returns e**x

pow(x, y) Returns x raised to the power y

sqrt(x) Returns the square root of x

acos(x) Returns the arc cosine of x

asin(x) Returns the arc sine of x

atan(x) Returns the arc tangent of x

cos(x) Returns the cosine of x

sin(x) Returns the sine of x

tan(x) Returns the tangent of x

Mr. D.S.Kiwde Page 3


MMN Python

degrees(x) Converts angle x from radians to degrees

radians(x) Converts angle x from degrees to radians

Mathematical constant, the ratio of circumference of a circle to it's diameter


pi
(3.14159...)

e mathematical constant e (2.71828...)

Mr. D.S.Kiwde Page 4

You might also like