Numeric Functions in Python

Numeric Functions in Python

In python a number of mathematical operations can be performed with ease by importing a module named “math” which defines various functions which makes our tasks easier.

पायथन में “math” नामक एक मॉड्यूल का इंपोर्ट करके कई गणितीय कार्यों को आसानी से किया जा सकता है जो विभिन्न कार्यों को परिभाषित करता है जो हमारे कार्यों को आसान बनाते हैं।

max()

The max() function returns the item with the highest value, or the item with the highest value in an iterable. If the values are strings, an alphabetically comparison is done.

max() फंक्शन आइटम को उच्चतम मान के साथ रिटर्न करता है, या एक पुनरावृत्ति में उच्चतम मूल्य के साथ आइटम । यदि मान स्ट्रिंग्स हैं, तो वर्णानुक्रम की तुलना की जाती है।

Syntax: max (n1, n2, n3, …)
Or
max (iterable)

Example:
x = max (5, 10)
print (x)
x = max (“apple”, “boy”, “cat”)
print (x)

Output:
10
cat

min()

The min() function returns the item with the lowest value, or the item with the lowest value in an iterable. If the values are strings, an alphabetically comparison is done.

min() फंक्शन आइटम को सबसे कम वैल्यू के साथ या आइटम एक पुनरावृत्त में सबसे कम वैल्यू के साथ रिटर्न करता है। यदि वैल्यू स्ट्रिंग्स हैं, तो वर्णानुक्रम की तुलना की जाती है।

Syntax – सिंटैक्स
min (n1, n2, n3, …)
Or:
min (iterable)

Example:
x = min (5, 10)
print (x)
x = min (“apple”, “boy”, “cat”)
print (x)

Output:
5
apple

pow()

The pow() function returns the value of x to the power of y (xy). If a third parameter is present, it returns x to the power of y, modulus z.

pow() फंक्शन x का वैल्यू y(xy) के पावर पर रिटर्न करता है। यदि कोई तीसरा पैरामीटर मौजूद है, तो यह x को y. मॉड्यूल z के पॉवर में रिटर्न होता है।

Syntax: pow(x, y, z)

Parameter    Description
पैरामीटर         डिस्क्रिप्शन
x              A number, the base – एक संख्या, आधार
y              A number, the exponent – एक संख्या, एक्सपोनेन्ट
z              Optional. A number, the modulus – वैकल्पिक। एक संख्या, मॉड्यूल्स

Example:
x = pow (2, 3)
print (x)
print (pow (4,3,6) )

Output:
8
4

round()

Python provides an inbuilt function round( which rounds off to the given number of digits and returns the floating point number, if no number of digits is provided for round off, it rounds off the number to the nearest integer.

पायथन एक इनबिल्ट फंक्शन राउंड() प्रदान करता है, जो दी गई अंकों की संख्या को राउंड ऑफ करता है और फ्लोटिंग पॉइंट नंबर को रिटर्न करता है, यदि राउंड ऑफ के लिए कोई अंक की संख्या प्रदान नहीं की जाती है, तो यह नंबर को निकटतम इंटीजर तक राउंड करता है।

Syntax: round(number, digits)

Parameter   Description
पैरामीटर       डिस्क्रिप्शन
Number      Required. The number to be rounded
नम्बर          आवश्यक है। नंबर जो राउंडेड किए जायें।
Digits           Optional. The number of decimals to use when rounding the number. Default is 0
डिजिट          वैकल्पिक। संख्या को राउंड ऑफ करते समय उपयोग करने के लिए दशमलव की संख्या। * डिफॉल्ट 0 है।

Example:
print (round (3.145678))
print (round (3.145678,2))
print (round (3.145678,3))
print (round (3.145678,4))

Output:
3
3.15
3.146
3.1457

int()

The int() function converts the specified value into an integer number.

int() फंक्शन निर्दिष्ट वैल्यू को इंटीजर नम्बर में परिवर्तित करता है।

Syntax: int(value, base)

Parameter   Description
पैरामीटर         डिस्क्रिप्शन
Value            A number or a string that can be converted into an integer number
वैल्यू              एक संख्या या एक स्ट्रिंग जिसे एक इंटीजर नम्बर में परिवर्तित किया जा सकता है।
base             A number representing the number format. Default value: 10
बेस              नम्बर फारमेट का प्रतिनिधित्व करने वाला एक नंबर । डिफॉल्ट मानः 10

Example:
x = int (“12”)
print (x)

Output:
12

Random numbers – रैंडम नम्बर

Python defines a set of functions that are used to generate or manipulate random numbers. These particular types of functions are used in a lot of games, lotteries or any application requiring random number generation.

पायथन फंक्शन के एक समूह को परिभाषित करता है जो रैंडम नम्बर को उत्पन्न करने या हेरफेर करने के लिए उपयोग  किया जाता है। इन विशेष प्रकार के कार्यों का उपयोग बहुत सारे गेम, लॉटरी या किसी भी एप्लिकेशन में किया जाता है  जिसमें रैंडम नम्बर की आवश्यकता होती है।

random()

The random() method returns a random floating point number in the range [0.0, 1.0).

रैंडम() मेथड रेंज [0.0, 1.0] में एक रैंडम फ्लोटिंग पॉइंट नंबर रिटर्न करती है।

Syntax: random()

Note – This function is not accessible directly, so we need to import the random module and then we need to call this function using the random static object.

 नोट– यह फंक्शन सीधे पहुंच योग्य नहीं है, इसलिए हमें रैंडम मॉड्यूल को इंपोर्ट करने की आवश्यकता है और फिर हमें रैंडम स्थिर ऑब्जेक्ट का उपयोग करके इस फंक्शन को कॉल करने की आवश्यकता है।

Example:
import random
# First random number
print (“random() : “, random.random() )
# Second random number
print (“random() : “, random.random())

Output:
random() : 0.38784557276237663
random() : 0.5037288345926793

randrange (beg, end, step)

This function is also used to generate random number but within a range specified in its arguments. This function takes 3 arguments, beginning number (included in generation), last number (excluded in generation) and step (to skip numbers in range while selecting).

इस फंक्शन का उपयोग रैंडम उत्पन्न करने के लिए भी किया जाता है, लेकिन इसके तर्कों में निर्दिष्ट सीमा के भीतर। यह फंक्शन 3 तर्क लेता है, प्रारंभिक संख्या (जनरेशन में शामिल), अंतिम संख्या (जनरेशन में शामिल) और चरण (चयन करते समय सीमा में संख्या को छोड़ने के लिए)।

Example:

import random
# Python code to demonstrate the working of choice () and randrange () import random
# using choice () to generate a random number from a given list of numbers.
print (“A random number from list is : “, end=””)
print (random.choice ([1, 10, 19, 6, 16]))
# using randrange () to generate in range from 20 to 50. The last
# parameter 3 is step size to skip
# three numbers when selecting.
print (“A random number from range is : “, end=””)
print (random.randrange (1, 19, 3))

Output:
A random number from list is : 19
A random number from range is : 16

ceil()

The method ceil() in Python returns ceiling value of x i.e., the smallest integer not less than x.

पायथन में मेथड ceil() का  ceiling value रिटर्न करता है यानि कि, छोटा पूणांक  x से कम नहीं ।

Syntax: सिंटैक्स
import math
math.ceil(x)
x: This is a numeric expression.

Example:
import math
# prints the ceil using ceil() method
print (“math.ceil(-23.11) : “, math.ceil(-23.11))
print (“math.ceil(300.16): “, math.ceil(300.16))
print (“math.ceil (300.72) : “, math.ceil(300.72))

Output:
math.ceil(-23.11) : -23
math.ceil(300.16) : 301
math.ceil(300.72) : 301

floor ()

floor()method in Python returns floor of x i.e., the largest integer not greater than x.

पायथन में floor() मेथड x का फ्लोर देता है, यानी सबसे बड़ा इंटीजर x से अधिक नहीं है।

Syntax: सिंटैक्स
import math
math.floor (x)
x-numeric expression.

Example:
import math
# prints the ceil using ceil() method
print(“math.floor(-23.11) : “, math.floor(-23.11))
print(“math.floor (23.16) : “, math.floor (23.16))
print (“math.floor (23.72) : “, math.floor (23.72))

Output:
math. floor (-23. 11) : -24
math. floor (23.16) : 23
math. floor (23.72) : 23

sqrt()

sqrt() function is an inbuilt function in Python programming language that returns the square root of any number.

sqrt() फंक्शन पायथन प्रोग्रामिंग भाषा में एक इनबिल्ट फंक्शन है जो किसी भी संख्या के वर्गमूल को रिटर्न करता है।

Syntax: सिंटैक्स
math.sqrt (x)
Parameter:
x is any number such that x>=0

Example:
import math
print (math.sqrt (0) )
print (math.sqrt (9))
print (math. sqrt (12.3))

Output:
0.0
3.0
3.5071355833500366

Chapter wise Model Paper Link

About Me