Matematične funkcije v Pythonu | 1. niz (številske funkcije)
V pythonu je mogoče z lahkoto izvesti številne matematične operacije z uvozom modula z imenom 'math', ki definira različne funkcije, kar olajša naše naloge. 1. ceil() :- Ta funkcija vrne najmanjša integralna vrednost večja od števila . Če je število že celo število, se vrne isto število. 2. nadstropje() :- Ta funkcija vrne največja integralna vrednost manjša od števila . Če je število že celo število, se vrne isto število.
Python # Python code to demonstrate the working of # ceil() and floor() # importing 'math' for mathematical operations import math a = 2.3 # returning the ceil of 2.3 print ( 'The ceil of 2.3 is : ' end = '' ) print ( math . ceil ( a )) # returning the floor of 2.3 print ( 'The floor of 2.3 is : ' end = '' ) print ( math . floor ( a ))
Izhod:
The ceil of 2.3 is : 3 The floor of 2.3 is : 2
Časovna zapletenost: O(1)
Pomožni prostor: O(1)
3. fabs() :- Ta funkcija vrne absolutna vrednost števila. 4. faktorial() :- Ta funkcija vrne faktorial števila. Če število ni integral, se prikaže sporočilo o napaki.
# Python code to demonstrate the working of # fabs() and factorial() # importing 'math' for mathematical operations import math a = - 10 b = 5 # returning the absolute value. print ( 'The absolute value of -10 is : ' end = '' ) print ( math . fabs ( a )) # returning the factorial of 5 print ( 'The factorial of 5 is : ' end = '' ) print ( math . factorial ( b ))
Izhod:
The absolute value of -10 is : 10.0 The factorial of 5 is : 120
Časovna zapletenost: O(b)
Pomožni prostor: O(1)
5. kopija (a b) :- Ta funkcija vrne številko z vrednost 'a', vendar s predznakom 'b' . Vrnjena vrednost je plavajoči tip. 6. gcd() :- Ta funkcija se uporablja za izračun največji skupni delitelj 2 števil omenjeno v svojih argumentih. Ta funkcija deluje v pythonu 3.5 in novejšem.
# Python code to demonstrate the working of # copysign() and gcd() # importing 'math' for mathematical operations import math a = - 10 b = 5.5 c = 15 d = 5 # returning the copysigned value. print ( 'The copysigned value of -10 and 5.5 is : ' end = '' ) print ( math . copysign ( 5.5 - 10 )) # returning the gcd of 15 and 5 print ( 'The gcd of 5 and 15 is : ' end = '' ) print ( math . gcd ( 5 15 ))
Izhod:
The copysigned value of -10 and 5.5 is : -5.5 The gcd of 5 and 15 is : 5
Časovna zapletenost: O(min(cd))
Pomožni prostor: O(1)