Mathematical Functions in Python in Hindi – Mathematical Functions क्या होते हैं?
Table of Contents
- 1. Introduction to Mathematical Functions in Python in Hindi
- 2. Commonly Used Functions (random(), ceil(), floor(), sqrt())
- 3. Difference Between ceil() and floor() in Hindi
- 4. Importing and Using the math Module in Python in Hindi
1. Introduction to Mathematical Functions in Python in Hindi
Mathematical Functions वो built-in functions होते हैं जो Python में गणितीय (mathematical) calculations करने के लिए पहले से मौजूद होते हैं। इनकी मदद से हमें खुद से logic लिखने की जरूरत नहीं पड़ती, बस function को call करना होता है और वो हमारा काम कर देता है।
Python में कुछ functions जैसे abs(), round() और pow() directly बिना किसी module के use हो जाते हैं, क्योंकि ये built-in functions होते हैं। लेकिन कुछ advanced functions जैसे sqrt(), ceil() और floor() को use करने के लिए हमें Python के math module को import करना पड़ता है।
print(abs(-15)) # Output: 15
print(round(7.6)) # Output: 8
print(pow(2, 3)) # Output: 8
ऊपर दिए गए example में देखिए कि इन functions को use करने के लिए कोई extra module import नहीं करना पड़ा, क्योंकि ये directly Python में built-in हैं। लेकिन जैसे ही हमें square root या rounding से जुड़े advanced calculations करने होते हैं, वहाँ हमें math module की जरूरत पड़ती है, जिसे हम आगे के sections में विस्तार से समझेंगे।
यह SVG दिखा रहा है कि Python के कुछ mathematical functions बिना किसी module के directly use हो जाते हैं, जबकि कुछ functions के लिए math module import करना जरूरी होता है।
2. Commonly Used Functions (random(), ceil(), floor(), sqrt())
Python में कुछ Mathematical Functions ऐसे हैं जो programs में सबसे ज्यादा use किए जाते हैं। आइए इन्हें एक-एक करके समझते हैं।
1. random() Function
यह function 0 से लेकर 1 के बीच का एक random decimal number generate करता है। इसे use करने के लिए हमें Python के random module को import करना पड़ता है।
import random
value = random.random()
print(value) # हर बार अलग decimal number आएगा, जैसे 0.7345
2. ceil() Function
यह function किसी भी decimal number को उसके नज़दीकी बड़े integer (upper value) में round कर देता है।
import math
print(math.ceil(4.1)) # Output: 5
print(math.ceil(4.9)) # Output: 5
3. floor() Function
यह function किसी भी decimal number को उसके नज़दीकी छोटे integer (lower value) में round कर देता है।
import math
print(math.floor(4.1)) # Output: 4
print(math.floor(4.9)) # Output: 4
4. sqrt() Function
यह function किसी भी number का square root निकालने के काम आता है।
import math
print(math.sqrt(25)) # Output: 5.0
print(math.sqrt(2)) # Output: 1.4142135623730951
ये चारों functions daily programming में बहुत काम आते हैं, चाहे वो कोई game बनाना हो जिसमें random values चाहिए हों, या फिर कोई scientific calculation जिसमें rounding और square root की जरूरत पड़े।
3. Difference Between ceil() and floor() in Hindi
बहुत सारे students ceil() और floor() को लेकर confuse हो जाते हैं, इसलिए इनका फर्क अच्छे से समझना जरूरी है।
ceil() Function:
यह number को हमेशा ऊपर की तरफ round करता है, यानी उसके नज़दीकी बड़े integer में बदल देता है। चाहे decimal value कितनी भी छोटी क्यों न हो, ceil() हमेशा उसे अगले बड़े number में convert कर देगा।floor() Function:
यह number को हमेशा नीचे की तरफ round करता है, यानी उसके नज़दीकी छोटे integer में बदल देता है। चाहे decimal value कितनी भी बड़ी क्यों न हो, floor() हमेशा उसे पहले वाले छोटे number में convert कर देगा।
import math
number = 6.3
print("ceil value:", math.ceil(number)) # Output: 7
print("floor value:", math.floor(number)) # Output: 6
यह SVG दिखा रहा है कि 6.3 number के लिए floor() कैसे नीचे की तरफ 6 देता है, जबकि ceil() ऊपर की तरफ 7 देता है।
4. Importing and Using the math Module in Python in Hindi
Python का math module एक built-in module है जिसमें बहुत सारे mathematical functions पहले से मौजूद होते हैं, जैसे sqrt(), ceil(), floor(), pow(), log() आदि। इसे use करने से पहले इसे import करना जरूरी होता है, वरना Python error दे देगा।
import math
print(math.sqrt(49)) # Output: 7.0
print(math.ceil(3.2)) # Output: 4
print(math.floor(3.8)) # Output: 3
print(math.pi) # Output: 3.141592653589793
अगर आपको math module के सिर्फ कुछ specific functions ही चाहिए, तो आप पूरे module को import करने की बजाय सिर्फ जरूरी function को भी import कर सकते हैं।
from math import sqrt, ceil
print(sqrt(64)) # Output: 8.0
print(ceil(2.3)) # Output: 3
इस तरीके से import करने पर हमें function के नाम के आगे math. लगाने की जरूरत नहीं पड़ती, सीधे function का नाम लिखकर ही उसे call किया जा सकता है। यह तरीका तब useful होता है जब हमें module के सिर्फ एक या दो functions ही चाहिए होते हैं और पूरा module load करना जरूरी नहीं होता।