Skip to content

Number Type


Python supports three numeric types to represent numbers: integers, float, and complex number. Here you will learn about each number type.

In Python, integers are zero, positive or negative whole numbers without a fractional part and having unlimited precision, e.g. 0, 100, -10. The followings are valid integer literals in Python.

#integer variables
x = 0
print(x)
x = 100
print(x)
x = -10
print(x)
x = 1234567890
print(x)
x = 5000000000000000000000000000000000000000000000000000000
print(x)

`#integer variables x = 0 print(x)

x = 100 print(x)

x = -10 print(x)

x = 1234567890 print(x)

x = 5000000000000000000000000000000000000000000000000000000 print(x)`Try it Integers can be binary, octal, and hexadecimal values.

b = 0b11011000 # binary
print(b)
o = 0o12 # octal
print(o)
h = 0x12 # hexadecimal
print(h)

`b = 0b11011000 # binary print(b)

o = 0o12 # octal print(o)

h = 0x12 # hexadecimal print(h)[Try it](/codeeditor?cid=python-3z7kemnmm) All integer literals or variables are objects of the int class. Use the type() method to get the class name, as shown below. int“type()`

print(type(100))
x=1234567890
print(type(x))
y=5000000000000000000000000000000000000000000000000000000
print(type(y))

`print(type(100))

x=1234567890 print(type(x))

y=5000000000000000000000000000000000000000000000000000000 print(type(y))`Try it Note: Leading zeros in non-zero integers are not allowed in Python, e.g. 000123 is invalid number and 0000 becomes 0.

x=001234567890 #syntaxError: invalid token

x=001234567890 #syntaxError: invalid token Python does not allow comma as number delimiter. Use underscore _ as a delimiter instead. _

x = 1_234_567_890
print(x) #output: 1234567890

x = 1_234_567_890 print(x) #output: 1234567890Try it Note that integers must be without a fractional part (decimal point). It it includes a fractional then it becomes a float.

x=5
print(type(x)) #output: <class 'int'>
x=5.0
print(type(x)) #output: <class 'float'>

`x=5 print(type(x)) #output: <class ‘int’>

x=5.0 print(type(x)) #output: <class ‘float’>[Try it](/codeeditor?cid=python-3z7kfc6nu ) The int() function converts a string or float to int. int()`

x = int('100')
print(x) #output: 100
y = int('-10')
print(y) #output: -10
z = int(5.5)
print(z) #output: 5
n = int('100', 2)
print(n) #output: 4

`x = int(‘100’) print(x) #output: 100

y = int(‘-10’) print(y) #output: -10

z = int(5.5) print(z) #output: 5

n = int(‘100’, 2) print(n) #output: 4`Try it

A number having 0b with eight digits in the combination of 0 and 1 represent the binary numbers in Python. For example, 0b11011000 is a binary number equivalent to integer 216.

x = 0b11011000
print(x)
x = 0b_1101_1000
print(x)
print(type(x))

`x = 0b11011000 print(x)

x = 0b_1101_1000 print(x) print(type(x))`Try it

A number having 0o or 0O as prefix represents an octal number. For example, 0O12 is equivalent to integer 10.

x = 0o12
print(x)
print(type(x))

`x = 0o12

print(x) print(type(x))`Try it

A number with 0x or 0X as prefix represents hexadecimal number. For example, 0x12 is equivalent to integer 18.

x = 0x12
print(x)
print(type(x))

`x = 0x12

print(x) print(type(x))`Try it

In Python, floating point numbers (float) are positive and negative real numbers with a fractional part denoted by the decimal symbol . or the scientific notation E or e, e.g. 1234.56, 3.142, -1.55, 0.23. .``E``e

f = 1.2
print(f) #output: 1.2
print(type(f)) #output: <class 'float'>
f=123_42.222_013 #output: 12342.222013
print(f)
f=2e400
print(f) #output: inf

`f = 1.2 print(f) #output: 1.2 print(type(f)) #output: <class ‘float’>

f=123_42.222_013 #output: 12342.222013 print(f)

f=2e400 print(f) #output: inf[Try it](/codeeditor?cid=python-3z7khfdhd ) As you can see, a floating point number can be separated by the underscore _. The maximum size of a float is depend on your system. The float beyond its maximum size referred as inf, Inf, INFINITY, or infinity. For example, a float number 2e400 will be considered as infinity for most systems. _“2e400` Scientific notation is used as a short representation to express floats having many digits. For example: 345.56789 is represented as 3.4556789e2 or 3.4556789E2

f = 1e3
print(f) #output: 1000.0
f = 1e5
print(f) #output:100000.0
f = 3.4556789e2
print(f) #output:
print(type(f)) #output:345.56789

`f = 1e3 print(f) #output: 1000.0

f = 1e5 print(f) #output:100000.0

f = 3.4556789e2 print(f) #output: print(type(f)) #output:345.56789[Try it](/codeeditor?cid=python-3z7nmbj4w ) Use the float() function to convert string, int to float. float()`

f=float('5.5')
print(f) #output: 5.5
f=float('5')
print(f) #output: 5.0
f=float(' -5')
print(f) #output: -5.0
f=float('1e3')
print(f) #output: 1000.0
f=float('-Infinity')
print(f) #output: -inf
f=float('inf')
print(f) #output: inf
print(type(f)) #output:<class 'float'>

`f=float(‘5.5’) print(f) #output: 5.5

f=float(‘5’) print(f) #output: 5.0

f=float(’ -5’) print(f) #output: -5.0

f=float(‘1e3’) print(f) #output: 1000.0

f=float(‘-Infinity’) print(f) #output: -inf

f=float(‘inf’) print(f) #output: inf print(type(f)) #output:<class ‘float’>`Try it

A complex number is a number with real and imaginary components. For example, 5 + 6j is a complex number where 5 is the real component and 6 multiplied by j is an imaginary component. 5 + 6j

a = 5+2j
print(a)
print(type(a))

a = 5+2j print(a) print(type(a))Try it You must use j or J as imaginary component. Using other character will throw syntax error.

a=5+2k
a=5+j
a=5i+2j

a=5+2k a=5+j a=5i+2jTry it

The following table list arithmetic operators on integer values: Try it

Addition and subtraction of complex numbers is straightforward. Real and imaginary parts are added/subtracted to get the result.

a=6+4j
b=3+2j
print("a+2=",a+2)
print("a*2=",a*2)
print("a/2=",a/2)
print("a**2=",a**2)
print("a+b=",a+b)
print("a-b=",a-b)

`a=6+4j b=3+2j

print(“a+2=“,a+2) print(“a2=“,a2) print(“a/2=“,a/2) print(“a2=“,a2) print(“a+b=“,a+b) print(“a-b=“,a-b)`Try it As you can see in the above example, the arithmetic operators can also be used with two complex numbers.

The process of multiplying these two complex numbers is very similar to multiplying two binomials. Multiply each term in the first number by each term in the second number.

a=6+4j
b=3+2j
c=a*b
print(c)
c=(6+4j)*(3+2j)
print(c)
c=(18+12j+14j+8*1)
print(c)

`a=6+4j
b=3+2j

c=a*b
print(c)

c=(6+4j)*(3+2j)
print(c)

c=(18+12j+14j+8*1) print(c)`Try it

A numeric object of one type can be converted in another type using the following functions: intfloatcomplexhexoctpowabsround