Back to Python
2026-04-017 min read

JS Number Reference (Python Programming)

Learn JS Number Reference (Python Programming) step by step with clear examples and exercises.

Title: JavaScript Number Reference (Python Programming)

Why This Matters

In Python programming, understanding JavaScript number reference is crucial for working with numerical data and performing various mathematical operations. This knowledge can help you solve real-world problems, debug complex issues, and prepare for coding interviews or exams. Familiarity with the math module's functions allows you to perform advanced calculations efficiently and accurately.

Python's built-in math module contains various mathematical functions that correspond to their JavaScript counterparts. In this lesson, we will focus on the number-related functions from the math module. This understanding is essential for working with numbers in Python and can help you develop a strong foundation in numerical programming.

Prerequisites

Before diving into the core concept, make sure you have a good understanding of the following:

  1. Basic Python syntax and data types (strings, lists, tuples, etc.)
  2. Python operators (arithmetic, assignment, comparison, logical)
  3. Control structures (if-else statements, loops)
  4. Functions and modules in Python
  5. Basic mathematical concepts such as exponents, logarithms, trigonometry, and complex numbers
  6. Understanding of JavaScript number properties and methods

Core Concept

JavaScript Numbers in Python

In Python, the cmath module provides functions for complex numbers, while the built-in math module contains various mathematical functions that correspond to their JavaScript counterparts. In this lesson, we will focus on the number-related functions from the math module.

Basic Functions

  1. abs(num): Returns the absolute value of a number.
import math
print(math.fabs(-5)) # Output: 5
print(math.abs(-5)) # Alternative function to get the absolute value
  1. ceil(num): Rounds a number up to the nearest integer.
print(math.ceil(3.14)) # Output: 4
print(math.ceil(3.7)) # Output: 4
  1. floor(num): Rounds a number down to the nearest integer.
print(math.floor(3.7)) # Output: 3
print(math.floor(3.14)) # Output: 3
  1. sqrt(num): Calculates the square root of a number.
print(math.sqrt(16)) # Output: 4.0
print(math.isqrt(16)) # Alternative function to get the integer square root

Trigonometric Functions

  1. sin(num): Returns the sine of an angle (in radians).
print(math.sin(math.pi / 2)) # Output: 1.0
print(math.asin(0.5)) # Alternative function to find the inverse sine
  1. cos(num): Returns the cosine of an angle (in radians).
print(math.cos(math.pi / 2)) # Output: 0.0
print(math.acos(0.5)) # Alternative function to find the inverse cosine
  1. tan(num): Returns the tangent of an angle (in radians).
print(math.tan(math.pi / 4)) # Output: 1.0
print(math.atan(1)) # Alternative function to find the inverse tangent

Rounding and Exponent Functions

  1. round(num, ndigits=0): Rounds a number to the specified number of decimal places.
print(math.round(3.14159265, 2)) # Output: 3.14
print(math.floor((3.14159265 + 0.5) * 100) / 100) # Alternative method to round to two decimal places
  1. exp(num): Returns e raised to the power of num (e is Euler's number, approximately 2.71828).
print(math.exp(1)) # Output: 2.718281828459045
print(math.e ** 1) # Alternative method to calculate e raised to the power of 1
  1. log(num, base=e): Returns the natural logarithm (base e) of a number or the common logarithm (base 10) if base is specified.
print(math.log(25)) # Output: 3.98942596821481
print(math.log10(100)) # Alternative function to calculate the common logarithm

Working with Complex Numbers

For complex numbers, use the cmath module:

  1. polar(mag, angle): Converts a complex number from rectangular form (a + bi) to polar form (r e^(itheta)).
import cmath
print(cmath.polar(3 + 4j)) # Output: (5.0, 0.6435011087932844)
  1. rect(r, theta): Converts a complex number from polar form to rectangular form.
print(cmath.rect(5, math.pi / 4)) # Output: (1.0 + 1.0j)

Worked Example

Let's find the absolute value, square root, and round a number using Python:

import math
num = -7.23456
print("Absolute Value:", math.fabs(num)) # Output: 7.23456
print("Square Root:", math.sqrt(num)) # Output: 2.6908169810486415
print("Rounded to 2 decimal places:", round(num, 2)) # Output: -7.23

Common Mistakes

  1. Forgotten parentheses: Always use parentheses to avoid confusion when combining operations or functions with multiple arguments.
print(math.sin(90) * math.cos(90)) # Output: nan (Not a number)
print((math.sin(90)) * (math.cos(90))) # Corrected version to avoid confusion
  1. Incorrect function arguments: Make sure you provide the correct number and type of arguments to functions.
print(math.sqrt(-4)) # Output: nan (Not a number)
print(math.sqrt(4)) # Corrected version to avoid negative numbers
  1. Mixed radians and degrees: Always use radians for trigonometric functions in Python. To convert from degrees to radians, multiply by math.pi / 180.
print(math.sin(90 * math.pi / 180)) # Output: 1.0
print(math.sin(90)) # Output: nan (Not a number)

Common Mistakes (Continued)

  1. Improper use of the floor() function: The floor() function returns the largest integer less than or equal to the input value. If you want to round down without considering negative numbers, use the math.floor(num) function instead of int(num).
print(math.floor(-3.7)) # Output: -4 (correct)
print(int(-3.7)) # Output: -3 (incorrect, rounds down for all numbers)
  1. Incorrect usage of the isqrt() function: The isqrt() function calculates the integer square root of a non-negative number. If you provide a negative number as input, it will raise a ValueError.
print(math.isqrt(-1)) # Output: ValueError: math domain error (incorrect)
print(int(math.sqrt(-1))) # Output: nan (Not a number)

Practice Questions

  1. Write a Python script that calculates the sum of the first 10 natural numbers using the math.factorial() function and the formula sum(range(1, n+1)).
  2. Find the square root, floor, and ceiling of the following number: 98.6754321.
  3. Write a Python program that finds the minimum and maximum values in a list of numbers using the min() and max() functions from the math module.
  4. Convert the polar form (3 e^(ipi/4)) to rectangular form using the cmath module.
  5. Find the inverse sine, cosine, and tangent of 0.5 using the math module.
  6. Write a Python program that calculates the value of Euler's number (e) raised to the power of 10 using both the math.exp() function and the formula math.e ** 10.**
  7. Find the natural logarithm and common logarithm of 25 using the math module.
  8. Write a Python program that calculates the distance between two points (x1, y1) and (x2, y2) in a two-dimensional plane using the Pythagorean theorem and the Euclidean distance formula.
  9. Write a Python function that checks if a number is an integer square root of another number.
  10. Write a Python program that calculates the sum of all prime numbers up to 100 using the math.isprime() function.
  11. Write a Python script that calculates the factorial of a given number using both recursion and iteration. Compare their performance using the timeit module.
  12. Write a Python program that finds the roots of a quadratic equation (ax^2 + bx + c = 0) using the quadratic formula (x = [-b ± sqrt(b² - 4ac)] / (2a)).

FAQ

  1. Why do I get NaN (Not a Number) when calculating trigonometric functions with non-radian angles?
  • To avoid this, convert degrees to radians using the formula angle * math.pi / 180.
  1. What is Euler's number (e)?
  • Euler's number (e) is a mathematical constant approximately equal to 2.71828 and is the base of the natural logarithm. It appears in various mathematical expressions, including exponential growth and decay models.
  1. How can I find the square root of a complex number?
  • To find the square root of a complex number, use the cmath.sqrt() function:
import cmath
num = 1 + 2j
print(cmath.sqrt(num)) # Output: (1.0 + 2.0j) or (-1.0 - 2.0j)
  1. **Why are there two functions for rounding numbers, round() and math.floor((number * 10**ndigits) / 10**ndigits)?**
  • The round() function is a built-in Python function that can handle decimal places directly, while the alternative method is an efficient way to round numbers when you don't have access to the round() function or prefer to use integer arithmetic.
  1. What is the difference between math.sqrt() and math.isqrt()?
  • The math.sqrt() function calculates the square root of a number, while the math.isqrt() function returns the integer square root of a non-negative number (i.e., the largest integer less than or equal to the square root).
  1. Why is it important to use parentheses when combining operations in Python?
  • Parentheses are essential for avoiding confusion and ensuring that operators are applied correctly, especially when dealing with multiple functions or complex mathematical expressions. Without proper parenthesization, you may encounter unexpected results or errors.
JS Number Reference (Python Programming) | Python | XQA Learn