pow(2,3) # 2 raised to 3 = 8NB: Numbers
Built-in Functions
These are built-in mathematical functions for numbers.
pow() Power
abs() Absolute value
abs(-2) # returns 2, the absolute value of its argumentround() Round
Rounding up or down its argument (to closest whole number).
round(2.8) # rounds up to 3.03
round(1.1) # rounds down to 1.01
Math library functions
See the Python docs on the math library.
import mathmath.sqrt() Square root
math.sqrt?Signature: math.sqrt(x, /)
Docstring: Return the square root of x.
Type: builtin_function_or_method
# sqrt(intOne)math.sqrt(12) # using the square-root function from the math library3.4641016151377544
print(math.floor(2.5)) # returns largest whole number less than the argument
print(math.floor(2.9))
print(math.floor(2.1))2
2
2
math.log()
math.log?math.log(100, 10)math.log(256, 2)The Random library
See random — Generate pseudo-random numbers for more info.
import randomrandom.random()
random.random?print(random.random()) # using random() function in random library
# will return a number between 0 and 1random.randint()
random.randint?print(random.randint(1,100)) # specify a range in the parenthesis
# this will return a random integer in the range 1-100