Sunday, January 28, 2024

Math Library Functions in Python

 Math Library Functions in Python



Function

Description

Example Usage

math.sqrt(x)

Square root of x

math.sqrt(25) returns 5.0

math.factorial(x)

Factorial of x

math.factorial(5) returns 120

math.sin(x)

Sine of x (x is in radians)

math.sin(math.pi/2) returns 1.0

math.cos(x)

Cosine of x (x is in radians)

math.cos(0) returns 1.0

math.tan(x)

Tangent of x (x is in radians)

math.tan(math.pi/4) returns 1.0

math.log(x)

Natural logarithm (base e) of x

math.log(math.e) returns 1.0

math.log10(x)

Base-10 logarithm of x

math.log10(100) returns 2.0

math.exp(x)

Exponential function of x

math.exp(1) returns 2.718281828459045

math.pow(x, y)

x raised to the power y

math.pow(2, 3) returns 8.0

math.radians(x)

Converts angle x from degrees to radians

math.radians(90) returns 1.5707963267948966

math.degrees(x)

Converts angle x from radians to degrees

math.degrees(math.pi/2) returns 90.0

math.pi

Mathematical constant pi (π)

math.pi returns 3.141592653589793

math.e

Mathematical constant e (base of natural logarithms)

math.e returns 2.718281828459045

math.ceil(x)

Rounds x up to the nearest integer

math.ceil(3.2) returns 4

math.floor(x)

Rounds x down to the nearest integer

math.floor(3.7) returns 3

math.trunc(x)

Truncates x to the nearest integer

math.trunc(3.7) returns 3

math.fabs(x)

Absolute value of x

math.fabs(-4.5) returns 4.5

math.isnan(x)

Checks if x is NaN (Not a Number)

math.isnan(float('nan')) returns True

math.isinf(x)

Checks if x is positive or negative infinity

math.isinf(float('inf')) returns True

2 comments:

Input Text and Display in tkinter

  import tkinter as tk def display_text():     text = entry.get()     label.config(text="You entered: " + text) root = tk.Tk() roo...