0% found this document useful (0 votes)
6 views16 pages

python_Lec-4

The document provides an overview of handling dates and times in Python using the datetime module, along with built-in math functions and the math module for performing mathematical operations. It includes examples of functions like min(), max(), abs(), pow(), and methods for calculating square roots, ceilings, and floors. Additionally, it presents solutions to mathematical problems involving compound interest, vector angles, perfect squares, and distances in 3D space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views16 pages

python_Lec-4

The document provides an overview of handling dates and times in Python using the datetime module, along with built-in math functions and the math module for performing mathematical operations. It includes examples of functions like min(), max(), abs(), pow(), and methods for calculating square roots, ceilings, and floors. Additionally, it presents solutions to mathematical problems involving compound interest, vector angles, perfect squares, and distances in 3D space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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 = datetime.datetime.now()
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 math.sqrt() method for example,
x = math.sqrt(64)
returns the square root of a number
print(x)
Python: Math Module
import math
The math.ceil() method rounds a
number upwards to its nearest x = math.ceil(1.4)
integer, and the math.floor() method y = math.floor(1.4)
rounds a number downwards to its
nearest integer print(x)
print(y)

import math
The math.pi constant, returns the
value of PI x = math.pi

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 * math.pow(1 + r / n, n * t)

print(f"The compound interest amount is: {A:.2f}")


2 Solutions!!
import math

x1 = float(input("Enter x1: ")) The math.acos() 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 = math.sqrt(x1**2 + y1**2)
magnitude_2 = math.sqrt(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 = math.acos(cos_theta)
theta_deg = math.degrees(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 math.isqrt() function
returns the integer square
root = math.isqrt(num) root of a non-negative
if root * root == num: integer
print(f"{num} is a perfect square.")
import math
else:
math.isqrt(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 = math.sqrt(math.pow(x2 - x1, 2) +


math.pow(y2 - y1, 2) +
math.pow(z2 - z1, 2))

print(f"The distance between the points is: {distance:.2f}")

You might also like