Numeric Functions in Python
1. eval() -
Definition (English)
The eval() function evaluates a string as a Python expression and returns the result.
परिभाषा (Hindi)
Python में eval() function किसी string को Python expression के रूप में evaluate करता है और उसका result return करता है।
Syntax:
eval(expression)
Example:
result = eval("10 + 20")
print(result)
Output:
30
Explanation:
"10 + 20" को expression के रूप में evaluate किया जाता है और result 30 मिलता है।
Important: eval() का उपयोग केवल trusted input के साथ करें, क्योंकि यह arbitrary Python code execute कर सकता है।
2. max() -
Definition (English)
The max() function returns the largest item from an iterable or the largest among the given arguments.
परिभाषा (Hindi)
max() function किसी iterable के सबसे बड़े element या दिए गए arguments में से सबसे बड़ी value को return करता है।
Syntax:
max(iterable)
max(value1, value2, ...)
Example:
print(max(10, 25, 15))
print(max([5, 12, 8, 20]))
Output:
25
20
Explanation:
पहले उदाहरण में सबसे बड़ी value 25 है और दूसरे में 20 है।
3. min() -
Definition (English)
The min() function returns the smallest item from an iterable or the smallest among the given arguments.
परिभाषा (Hindi)
min() function किसी iterable के सबसे छोटे element या दिए गए arguments में से सबसे छोटी value को return करता है।
Syntax:
min(iterable)
min(value1, value2, ...)
Example:
print(min(10, 25, 15))
print(min([5, 12, 8, 20]))
Output:
10
5
Explanation:
पहले उदाहरण में सबसे छोटी value 10 है और दूसरे में 5 है।
4. pow() -
Definition (English)
The pow() function returns a number raised to the power of another number.
परिभाषा (Hindi)
pow() function किसी संख्या की दूसरी संख्या के घात (power) का परिणाम return करता है।
Syntax:
pow(base, exponent)
Example:
print(pow(2, 3))
print(pow(5, 2))
Output:
8
25
Explanation:
pow(2, 3) का अर्थ है (2^3 = 8)।
pow(5, 2) का अर्थ है (5^2 = 25)।
Note: pow(2, 3) का परिणाम 2 ** 3 के समान होता है।
5. round() -
Definition (English)
The round() function rounds a number to the nearest integer or to a specified number of decimal places.
परिभाषा (Hindi)
round() function किसी संख्या को निकटतम integer या निर्धारित decimal places तक round करता है।
Syntax:
round(number)
round(number, ndigits)
Example:
print(round(4.6))
print(round(3.14159, 2))
Output:
5
3.14
Explanation:
round(4.6) का result 5 है।
round(3.14159, 2) का result 3.14 है।
Important Note: Python में कुछ halfway values के लिए round half to even नियम लागू होता है। उदाहरण के लिए, round(2.5) का परिणाम 2 और round(3.5) का परिणाम 4 होता है।
6. int() -
Definition (English)
The int() function converts a number or a valid integer string into an integer.
परिभाषा (Hindi)
int() function किसी संख्या या valid integer string को integer में convert करता है।
Syntax:
int(value)
Example:
print(int(12.8))
print(int("25"))
Output:
12
25
Explanation:
int(12.8) decimal part को हटाकर 12 देता है।
int("25") string "25" को integer 25 में convert करता है।
Important: int() decimal part को round नहीं करता; यह zero की ओर truncate करता है। इसलिए int(-12.8) का result -12 होता है।
7. random() -
Definition (English)
The random() function returns a pseudo-random floating-point number greater than or equal to 0.0 and less than 1.0.
परिभाषा (Hindi)
random() function 0.0 से लेकर 1.0 से कम तक की एक pseudo-random floating-point value return करता है।
Important: random() built-in function नहीं है। इसका उपयोग करने के लिए random module import करना पड़ता है।
Syntax:
import random
random.random()
Example:
import random
print(random.random())
Possible Output:
0.735214
Explanation:
हर बार program चलाने पर अलग value मिल सकती है। ऊपर दिया गया output केवल उदाहरण है।
Additional Example — Random Integer:
import random
print(random.randint(1, 10))
Possible Output:
7
randint(1, 10) में 1 और 10, दोनों शामिल हो सकते हैं।
8. ceil()
Definition (English)
The ceil() function returns the smallest integer greater than or equal to the given number.
परिभाषा (Hindi)
ceil() function दी गई संख्या के बराबर या उससे बड़ी सबसे छोटी integer value return करता है।
Important: ceil() के लिए math module import करना आवश्यक है।
Syntax:
import math
math.ceil(number)
Example:
import math
print(math.ceil(4.2))
print(math.ceil(7.8))
print(math.ceil(-4.2))
Output:
5
8
-4
Explanation:
ceil() संख्या को ऊपर की ओर integer तक ले जाता है। Negative numbers में भी यह नियम लागू होता है।
9 floor() -
Definition (English)
The floor() function returns the largest integer less than or equal to the given number.
परिभाषा (Hindi)
floor() function दी गई संख्या के बराबर या उससे छोटी सबसे बड़ी integer value return करता है।
Important: floor() के लिए math module import करना आवश्यक है।
Syntax:
import math
math.floor(number)
Example:
import math
print(math.floor(4.8))
print(math.floor(7.2))
print(math.floor(-4.2))
Output:
4
7
-5
Explanation:
floor() संख्या को नीचे की ओर integer तक ले जाता है। ध्यान दें कि floor(-4.2) का परिणाम -5 होता है, -4 नहीं।
10. sqrt() -
Definition (English)
The sqrt() function returns the square root of a non-negative number.
परिभाषा (Hindi)
sqrt() function किसी non-negative संख्या का वर्गमूल (Square Root) return करता है।
Important: sqrt() के लिए math module import करना आवश्यक है।
Syntax:
import math
math.sqrt(number)
Example:
import math
print(math.sqrt(25))
print(math.sqrt(16))
print(math.sqrt(2))
Output:
5.0
4.0
1.4142135623730951
Explanation:
sqrt(25) का परिणाम 5.0 है, क्योंकि (5 \times 5 = 25)।
sqrt(16) का परिणाम 4.0 है।
sqrt(2) का परिणाम एक approximate floating-point value है।
Note: Real-number calculations में negative number का square root निकालने पर ValueError आता है।