Python में सामान्य गणितीय कार्यों के लिए +, -, *, /, % जैसे Operators पर्याप्त होते हैं। लेकिन जब हमें Square Root, Factorial, Trigonometric Functions, Logarithm, GCD, LCM या अधिक जटिल Mathematical Calculations करनी हों, तब Python का math Module बहुत उपयोगी होता है।
math Python की Standard Library का हिस्सा है, इसलिए इसे अलग से Install करने की आवश्यकता नहीं होती।
Python का math Module विभिन्न Mathematical Operations और Functions प्रदान करता है।
इसका उपयोग करने के लिए पहले इसे Import करना होता है।
import math
इसके बाद इसके Functions को math. के साथ उपयोग किया जाता है।
print(math.sqrt(25))
5.0
math Module Import करने के तरीके
import math
print(math.sqrt(16))
तरीका 2 – Specific Function Import
from math import sqrt
print(sqrt(16))
import math as m
print(m.sqrt(16))
math.sqrt() किसी संख्या का Square Root निकालता है।
import math
print(math.sqrt(25))
5.0
एक और उदाहरण:
print(math.sqrt(64))
Output:
8.0
math.pow() किसी संख्या की Power निकालने के लिए उपयोग किया जाता है।
math.pow(x, y)
import math
print(math.pow(2, 3))
8.0
यहाँ:
2³ = 8
math.pow() का परिणाम सामान्यतः float होता है।
किसी Positive Integer का Factorial निकालने के लिए factorial() का उपयोग किया जाता है।
import math
print(math.factorial(5))
120
क्योंकि:
5! = 5 × 4 × 3 × 2 × 1
= 120
ceil() किसी संख्या को ऊपर की ओर निकटतम Integer तक Round करता है।
import math
print(math.ceil(4.2))
print(math.ceil(4.9))
5
5
floor() किसी संख्या को नीचे की ओर निकटतम Integer तक Round करता है।
print(math.floor(4.2))
print(math.floor(4.9))
4
4
|
ceil() |
floor() |
|
ऊपर की ओर Round करता है |
नीचे की ओर Round करता है |
|
4.2 → 5 |
4.2 → 4 |
|
4.9 → 5 |
4.9 → 4 |
fabs() किसी Number का Absolute Value देता है और परिणाम float में देता है।
import math
print(math.fabs(-25))
25.0
gcd() दो या अधिक Integers का Greatest Common Divisor निकालता है।
import math
print(math.gcd(12, 18))
6
क्योंकि 12 और 18 का सबसे बड़ा Common Divisor 6 है।
lcm() दो या अधिक Integers का Least Common Multiple निकालता है।
import math
print(math.lcm(4, 6))
12
trunc() Decimal Part को हटाकर Integer Part देता है।
import math
print(math.trunc(5.89))
print(math.trunc(-5.89))
Output:
5
-5
math.pi Circle के π का Approximate Value देता है।
import math
print(math.pi)
Output लगभग:
3.141592653589793
math.e Mathematical Constant e का मान देता है।
import math
print(math.e)
Formula:
Area = π × r²
Python Program:
import math
radius = 7
area = math.pi * radius ** 2
print("Area =", area)
Formula:
Circumference = 2 × π × r
import math
radius = 7
circumference = 2 * math.pi * radius
print(circumference)
log() Natural Logarithm निकालने के लिए उपयोग किया जाता है।
import math
print(math.log(10))
print(math.log(8, 2))
3.0
क्योंकि:
2³ = 8
Base-10 Logarithm के लिए:
import math
print(math.log10(100))
2.0
exp(x) का अर्थ है:
eˣ
उदाहरण:
import math
print(math.exp(2))
Python का math Module विभिन्न Trigonometric Functions भी प्रदान करता है।
मुख्य Functions:
- sin()
- cos()
- tan()
इन Functions में Angle Radians में दिया जाता है।
import math
print(math.sin(math.pi / 2))
1.0
print(math.cos(0))
1.0
print(math.tan(0))
0.0
यदि हमारे पास Angle Degree में है, तो radians() का उपयोग कर सकते हैं।
import math
angle = math.radians(90)
print(math.sin(angle))
Output:
1.0
import math
angle = math.degrees(math.pi)
print(angle)
Output:
180.0
hypot() Right-Angled Triangle में Hypotenuse निकालने के लिए उपयोग किया जा सकता है।
import math
a = 3
b = 4
print(math.hypot(a, b))
Output:
5.0
क्योंकि:
√(3² + 4²) = 5
isqrt() किसी Non-negative Integer का Integer Square Root देता है।
import math
print(math.isqrt(25))
print(math.isqrt(20))
Output:
5
4
isqrt(20) का परिणाम 4 है क्योंकि Integer Square Root में Decimal भाग नहीं लिया जाता।
comb(n, k) Combination की संख्या निकालता है।
import math
print(math.comb(5, 2))
Output:
10
perm(n, k) Permutation की संख्या निकालता है।
import math
print(math.perm(5, 2))
Output:
20
|
Function |
कार्य |
|
sqrt() |
Square Root |
|
pow() |
Power |
|
factorial() |
Factorial |
|
ceil() |
ऊपर की ओर Round |
|
floor() |
नीचे की ओर Round |
|
fabs() |
Absolute Float Value |
|
gcd() |
Greatest Common Divisor |
|
lcm() |
Least Common Multiple |
|
trunc() |
Decimal हटाना |
|
log() |
Logarithm |
|
log10() |
Base-10 Logarithm |
|
exp() |
e की Power |
|
sin() |
Sine |
|
cos() |
Cosine |
|
tan() |
Tangent |
|
radians() |
Degree → Radian |
|
degrees() |
Radian → Degree |
|
hypot() |
Hypotenuse |
|
isqrt() |
Integer Square Root |
|
comb() |
Combination |
|
perm() |
Permutation |
import math
number = int(input("Enter number: "))
print("Square Root:", math.sqrt(number))
import math
number = int(input("Enter number: "))
print("Factorial:", math.factorial(number))
import math
radius = float(input("Enter radius: "))
area = math.pi * radius ** 2
print("Area:", area)
import math
a = 12
b = 18
print("GCD:", math.gcd(a, b))
print("LCM:", math.lcm(a, b))
GCD: 6
LCM: 36
import math
degree = 90
radian = math.radians(degree)
print("Sin:", math.sin(radian))
print("Cos:", math.cos(radian))
print("Tan:", math.tan(radian))
import math
print(math.factorial(-5))
यह Error देगा क्योंकि Factorial के लिए Integer Non-negative होना चाहिए।
import math
print(math.sqrt(-25))
Real-number math context में यह ValueError देगा।
- Mathematical Calculations के लिए math Module का उपयोग करें।
- math.pi का उपयोग π के लिए करें।
- Trigonometric Functions में Angle Radians में देने की बात याद रखें।
- Integer Factorial के लिए math.factorial() उपयोग करें।
- केवल जरूरत के Functions Import करके Code को साफ रखा जा सकता है।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- math एक Python Standard Library Module है।
- sqrt() Square Root निकालता है।
- factorial() Factorial निकालता है।
- ceil() ऊपर की ओर Round करता है।
- floor() नीचे की ओर Round करता है।
- gcd() Greatest Common Divisor निकालता है।
- lcm() Least Common Multiple निकालता है।
- math.pi π का मान देता है।
- sin(), cos(), tan() में Angle Radians में होता है।
- radians() Degree को Radian में बदलता है।
- degrees() Radian को Degree में बदलता है।
Frequently Asked Questions (FAQs)
प्रश्न 1. Python में Math Module कैसे Import करते हैं?
import math
प्रश्न 2. Square Root के लिए कौन-सा Function है?
math.sqrt()
प्रश्न 3. Factorial के लिए कौन-सा Function है?
math.factorial()
प्रश्न 4. GCD निकालने के लिए कौन-सा Function है?
math.gcd()
प्रश्न 5. LCM निकालने के लिए कौन-सा Function है?
math.lcm()
प्रश्न 6. math.pi का उपयोग क्यों किया जाता है?
Circle से संबंधित गणनाओं में π का मान प्राप्त करने के लिए।
- math → Mathematical Operations
- sqrt() → Square Root
- pow() → Power
- factorial() → Factorial
- ceil() → ऊपर Round
- floor() → नीचे Round
- gcd() → GCD
- lcm() → LCM
- pi → π
- sin(), cos(), tan() → Trigonometry
- radians() → Degree से Radian
- degrees() → Radian से Degree
- comb() → Combination
- perm() → Permutation
Python Random Module in Hindi – random(), randint(), randrange(), choice(), choices(), sample(), shuffle(), uniform(), seed() और Practical Programs।