Professor Dr.
Iqbal Ahmed
Department of Computer Science and Engineering
University of Chittagong
Python: Dates and Times
A date in Python is not a data type of its own, but we can import
a module named datetime to work with dates as date objects.
import datetime
x = [Link]()
print(x)
the result will be: 2025-05-04 19:28:18.861600
The date contains year, month, day, hour, minute, second, and
microsecond.
ython: Dates and Times
ython: Dates and Times
Python: Built in math function
Python has a set of built-in math functions, including an extensive math
module, that allows you to perform mathematical tasks on numbers.
x = min(5, 10, 25)
y = max(5, 10, 25)
The min() and max() functions can be used
to find the lowest or highest value
print(x)
print(y)
Python: Built in math function
The abs() function returns the x = abs(-7.25)
absolute (positive) value of the
specified number: print(x)
x = pow(4, 3)
The pow(x, y) function returns the
value of x to the power of y : xy
print(x)
Python: Math Module
Python has also a built-in module
called math, which extends the list
of mathematical functions.
To use it, you must import
the math module:
import math
The [Link]() method for example,
x = [Link](64)
returns the square root of a number
print(x)
Python: Math Module
import math
The [Link]() method rounds a
number upwards to its nearest x = [Link](1.4)
integer, and the [Link]() method y = [Link](1.4)
rounds a number downwards to its
nearest integer print(x)
print(y)
import math
The [Link] constant, returns the
value of PI x = [Link]
print(x)
Some Mathematical Problems!
Some Mathematical Problems!
Some Mathematical Problems!
Problem 3: Perfect Square Checker
Some Mathematical Problems!
Problem 4: Distance Between Two Points (3D)
1 Solutions!!
import math
P = float(input("Enter the principal amount (P): "))
r = float(input("Enter the annual interest rate (in decimal, e.g., 0.05 for 5%): "))
n = int(input("Enter the number of times interest is compounded per year (n): "))
t = float(input("Enter the number of years (t): "))
A = P * [Link](1 + r / n, n * t)
print(f"The compound interest amount is: {A:.2f}")
2 Solutions!!
import math
x1 = float(input("Enter x1: ")) The [Link]() function
y1 = float(input("Enter y1: ")) returns the arc cosine (also
x2 = float(input("Enter x2: ")) called the inverse cosine) of
y2 = float(input("Enter y2: ")) a number.
dot_product = x1 * x2 + y1 * y2
magnitude_1 = [Link](x1**2 + y1**2)
magnitude_2 = [Link](x2**2 + y2**2)
cos_theta = dot_product / (magnitude_1 * magnitude_2)
# Clamp value to handle floating-point errors (e.g., 1.00000000001)
cos_theta = min(1.0, max(-1.0, cos_theta))
theta_rad = [Link](cos_theta)
theta_deg = [Link](theta_rad)
print(f"The angle between the vectors is: {theta_deg:.2f} degrees")
3. Solutions!!
import math
num = int(input("Enter a number: "))
The [Link]() function
returns the integer square
root = [Link](num) root of a non-negative
if root * root == num: integer
print(f"{num} is a perfect square.")
import math
else:
[Link](x)
print(f"{num} is not a perfect square.")
4. Solutions!!
import math
x1 = float(input("Enter x1: "))
y1 = float(input("Enter y1: "))
z1 = float(input("Enter z1: "))
x2 = float(input("Enter x2: "))
y2 = float(input("Enter y2: "))
z2 = float(input("Enter z2: "))
distance = [Link]([Link](x2 - x1, 2) +
[Link](y2 - y1, 2) +
[Link](z2 - z1, 2))
print(f"The distance between the points is: {distance:.2f}")