Python math.sin(): Calculate Sine Trigonometry

The math.sin(x) function computes the trigonometric sine of an angle x measured in radians. The return value is a floating-point number strictly bounded in the range [-1.0, 1.0], corresponding to the vertical y-coordinate on a standard unit circle.

Companion Functions: For the horizontal unit coordinate, see math.cos(). To convert a sine ratio back into an angle, use the inverse arc sine math.asin().

Syntax & Parameters 🔝

The math.sin() function is part of the standard math module:

import math

sine_val = math.sin(x)
Parameter Type Required? Description
x float or int Yes The angle measured strictly in radians. Supports positive, negative, and zero values.

Return Value: Returns a floating-point number (float) bounded in the interval $[-1.0, 1.0]$.

Basic Examples in Radians 🔝

Standard angles evaluated using math.sin():

import math

print("sin(0):       ", math.sin(0))          # Output: 0.0
print("sin(pi / 2):  ", math.sin(math.pi / 2))    # Output: 1.0 (90 degrees)
print("sin(1 radian):", math.sin(1))          # Output: 0.8414709848078965
print("sin(3*pi / 2):", math.sin(3 * math.pi / 2))# Output: -1.0 (270 degrees)
Expected Output
sin(0):        0.0
sin(pi / 2):   1.0
sin(1 radian): 0.8414709848078965
sin(3*pi / 2): -1.0

Evaluating Angles in Degrees (math.radians) 🔝

A common error is passing degree values directly (such as math.sin(90)), which evaluates $90$ radians (≈ $0.894$) rather than $90^\circ$. Always convert degrees to radians first using math.radians():

import math

angles_deg = [0, 30, 45, 60, 90, 180, 270, 360]

for deg in angles_deg:
    rad = math.radians(deg)
    print(f"sin({deg:3d}°) = {math.sin(rad):6.3f}")
Expected Output
sin(  0°) =  0.000
sin( 30°) =  0.500
sin( 45°) =  0.707
sin( 60°) =  0.866
sin( 90°) =  1.000
sin(180°) =  0.000
sin(270°) = -1.000
sin(360°) =  0.000

Floating-Point Precision at Multiples of π 🔝

Mathematically, $\sin(\pi) = \sin(180^\circ) = 0$. Because $\pi$ is an irrational number approximated in 64-bit IEEE 754 floats, evaluating $\sin(\pi)$ produces a tiny near-zero residue:

import math

raw_val = math.sin(math.pi)
print("sin(pi) raw:    ", raw_val)  # 1.2246467991473532e-16
print("Is close to 0.0?", math.isclose(raw_val, 0.0, abs_tol=1e-15))  # True

Best Practice: Avoid testing if math.sin(x) == 0:. Always use math.isclose(math.sin(x), 0.0, abs_tol=1e-12) to account for floating-point imprecision.

Key Mathematical Properties & Symmetries 🔝

The sine function satisfies key fundamental mathematical identities:

  • Odd Function Symmetry: $\sin(-x) = -\sin(x)$ — negating the angle inverts the sign of the sine value.
  • Pythagorean Trigonometric Identity: $\sin^2(x) + \cos^2(x) = 1.0$ for any angle $x$.
  • Periodicity: $\sin(x + 2\pi) = \sin(x)$ — repeating indefinitely every $2\pi$ radians ($360^\circ$).
import math

theta = 1.45
print("Odd symmetry: ", math.isclose(math.sin(-theta), -math.sin(theta)))  # True
print("Pythagorean:  ", math.isclose(math.sin(theta)**2 + math.cos(theta)**2, 1.0)) # True

Plotting the Sine Wave with Matplotlib 🔝

Sine waves form the mathematical foundation of audio signal synthesis, alternating current (AC) electricity, and harmonic oscillation. Visualize a sine wave using Matplotlib:

import math
import matplotlib.pyplot as plt

x_vals = [i * 0.1 for i in range(0, 70)]  # 0 to 7 radians (~2*pi)
y_vals = [math.sin(x) for x in x_vals]

plt.plot(x_vals, y_vals, label="sin(x)", color="#0b8043")
plt.axhline(0, color="gray", linewidth=0.8, linestyle="--")
plt.title("Harmonic Sine Wave")
plt.xlabel("Radians")
plt.ylabel("sin(x)")
plt.grid(True)
plt.show()

Hands-on Exercise: Simple Harmonic Motion (SHM) with Assertions 🔝

In physics and robotics, the displacement $y(t)$ of an oscillating spring or pendulum over time $t$ with amplitude $A$ and frequency $f$ is modeled by: $$y(t) = A \cdot \sin(2\pi f t + \phi)$$ Let's build an oscillation simulator verified with unit assertions:

Python Exercise: Harmonic Motion Oscillation Calculator
import math

def calculate_shm_displacement(amplitude, frequency_hz, time_seconds, phase_radians=0.0):
    omega = 2.0 * math.pi * frequency_hz
    y = amplitude * math.sin(omega * time_seconds + phase_radians)
    return round(y, 4)

# 1 Hz oscillator with amplitude 5.0 meters (full cycle = 1.0 second)
assert calculate_shm_displacement(5.0, 1.0, 0.0) == 0.0    # t=0s -> equilibrium
assert calculate_shm_displacement(5.0, 1.0, 0.25) == 5.0   # t=0.25s (quarter cycle, 90 deg) -> max +A
assert calculate_shm_displacement(5.0, 1.0, 0.50) == 0.0   # t=0.50s (half cycle, 180 deg) -> equilibrium
assert calculate_shm_displacement(5.0, 1.0, 0.75) == -5.0  # t=0.75s (three-quarters, 270 deg) -> min -A
assert calculate_shm_displacement(5.0, 1.0, 1.00) == 0.0   # t=1.00s (full cycle, 360 deg) -> returns to 0

print("Displacement at t=0.25s:", calculate_shm_displacement(5.0, 1.0, 0.25), "m")
print("Displacement at t=0.75s:", calculate_shm_displacement(5.0, 1.0, 0.75), "m")
print("All harmonic motion displacement assertions passed successfully!")

Practice in Google Colab & GitHub 🔝

Open in Google Colab View on GitHub

Run all math.sin() examples interactively, test degree conversions, verify the Pythagorean identity, and model harmonic oscillations with unit assertions directly in Google Colab.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer