Numbers in python
Int
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
x = 11
y = 20200108
z = -9874666
print(x)
print(type(x))
print(y)
print(type(y))
print(z)
print(type(z))
Float
Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
x = 11.0
y = 20200108.01
z = -35.59
print(x)
print(type(x))
print(y)
print(type(y))
print(z)
print(type(z))
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
Note: "e" to indicate the power of 10.
Complex
Complex numbers are written with a "i" as the imaginary part:
x = 3+5i
y = 5i
z = -5i
print(type(x))
print(type(y))
print(type(z))
Random Number
Python does not have a random()
function to make a random number, but Python has a built-in module called random
that can be used to make random numbers:
import random
print(random.randrange(1,10))#prints 8
Hexadecimal
Using the function hex()
we can convert numbers into a hexadecimal format:
a=246
print(hex(a))#prints '0xf6'
Binary
Using the function bin()
you can convert numbers into their binary format.
b=1234
print(bin(b))#prints '0b10011010010'
Exponentials
The function pow()
takes two arguments, equivalent to x^y
. With three arguments it is equivalent to (x^y)%z
, but may be more efficient for long integers.
print(pow(3,4))# prints 81
print(pow(3,4,5))# prints 1
Absolute Value
The function abs()
returns the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.
print(abs(-3.14))#prints 3.14
print(abs(3))#prints 3
Round
The function round()
will round a number to a given precision in decimal digits (default 0 digits). It does not convert integers to floats.
print(round(3,2))#prints 3
print(round(395,-2))#prints 400
print(round(3.1415926535,2))#prints 3.14