pow(2,3) # 2 raised to 3 = 8
NB: 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 argument
round()
Round
Rounding up or down its argument (to closest whole number).
round(2.8) # rounds up to 3.0
3
round(1.1) # rounds down to 1.0
1
Math library functions
See the Python docs on the math library.
import math
math.sqrt()
Square root
math.sqrt?
Signature: math.sqrt(x, /)
Docstring: Return the square root of x.
Type: builtin_function_or_method
# sqrt(intOne)
12) # using the square-root function from the math library math.sqrt(
3.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?
100, 10) math.log(
256, 2) math.log(
The Random library
See random — Generate pseudo-random numbers for more info.
import random
random.random()
random.random?
print(random.random()) # using random() function in random library
# will return a number between 0 and 1
random.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