Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Python - Get Started
  • What is Python?
  • Where to use Python?
  • Python Version History
  • Install Python
  • Python - Shell/REPL
  • Python IDLE
  • Python Editors
  • Python Syntax
  • Python Keywords
  • Python Variables
  • Python Data Types
  • Number
  • String
  • List
  • Tuple
  • Set
  • Dictionary
  • Python Operators
  • Python Conditions - if, elif
  • Python While Loop
  • Python For Loop
  • User Defined Functions
  • Lambda Functions
  • Variable Scope
  • Python Modules
  • Module Attributes
  • Python Packages
  • Python PIP
  • __main__, __name__
  • Python Built-in Modules
  • OS Module
  • Sys Module
  • Math Module
  • Statistics Module
  • Collections Module
  • Random Module
  • Python Generator Function
  • Python List Comprehension
  • Python Recursion
  • Python Built-in Error Types
  • Python Exception Handling
  • Python Assert Statement
  • Define Class in Python
  • Inheritance in Python
  • Python Access Modifiers
  • Python Decorators
  • @property Decorator
  • @classmethod Decorator
  • @staticmethod Decorator
  • Python Dunder Methods
  • CRUD Operations in Python
  • Python Read, Write Files
  • Regex in Python
  • Create GUI using Tkinter
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Python - Math Module

Some of the most popular mathematical functions are defined in the math module. These include trigonometric functions, representation functions, logarithmic functions, angle conversion functions, etc. In addition, two mathematical constants are also defined in this module.

Pi is a well-known mathematical constant, which is defined as the ratio of the circumference to the diameter of a circle and its value is 3.141592653589793.

Example: Getting Pi Value
import math

print(math.pi)  #output: 3.141592653589793
Try it

Another well-known mathematical constant defined in the math module is e. It is called Euler's number and it is a base of the natural logarithm. Its value is 2.718281828459045.

Example: e Value
import math

print(math.e)  #output: 2.718281828459045
Try it

The math module contains functions for calculating various trigonometric ratios for a given angle. The functions (sin, cos, tan, etc.) need the angle in radians as an argument. We, on the other hand, are used to express the angle in degrees. The math module presents two angle conversion functions: degrees() and radians(), to convert the angle from degrees to radians and vice versa. For example, the following statements convert the angle of 30 degrees to radians and back (Note: π radians is equivalent to 180 degrees).

Example: Math Radians and Degrees
import math

print(math.radians(30))  #output: 0.5235987755982988

print(math.degrees(math.pi/6))  #output: 29.999999999999996
Try it

The following statements show sin, cos and tan ratios for the angle of 30 degrees (0.5235987755982988 radians):

Example: sin, cos, tan Calculation
import math

print(math.sin(0.5235987755982988))  #output: 0.49999999999999994

print(math.cos(0.5235987755982988))  #output: 0.8660254037844387

print(math.tan(0.5235987755982988))  #output: 0.5773502691896257
Try it

You may recall that sin(30)=0.5,cos(30)=32 (which is 0.8660254037844387) and tan(30)= 13 (which is 0.5773502691896257).

math.log()

The math.log() method returns the natural logarithm of a given number. The natural logarithm is calculated to the base e.

Example: log
import math

print(math.log(10))  #output: 2.302585092994046
Try it

math.log10()

The math.log10() method returns the base-10 logarithm of the given number. It is called the standard logarithm.

Example: log10
import math

print(math.log10(10))  #output: 1.0
Try it

math.exp()

The math.exp() method returns a float number after raising e to the power of the given number. In other words, exp(x) gives e**x.

Example: Exponent
import math

print(math.exp(10))  #output: 22026.465794806718
Try it

This can be verified by the exponent operator.

Example: Exponent Operator **
import math

print(math.e**10)  #output: 22026.465794806703
Try it

math.pow()

The math.pow() method receives two float arguments, raises the first to the second and returns the result. In other words, pow(4,4) is equivalent to 4**4.

Example: Power
import math

print(math.pow(2,4))  #output: 16.0
Try it

math.sqrt()

The math.sqrt() method returns the square root of a given number.

Example: Square Root
import math

print(math.sqrt(100))  #output: 10.0

print(math.sqrt(3))  #output: 1.7320508075688772
Try it

The following two functions are called representation functions. The ceil() function approximates the given number to the smallest integer, greater than or equal to the given floating point number. The floor() function returns the largest integer less than or equal to the given number.

Example: Ceil and Floor
import math

print(math.ceil(4.5867))  #output: 5            
print(math.floor(4.5687)) #output: 4
Try it

Learn more about math module on Python docs.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.