Python Numbers

Number data types store numeric values. Number objects are created when you assign a value to them.

Python supports three different numerical types.

  • int (signed integers)
  • float (floating point real values)
  • complex (complex numbers)

Variables of numeric types are created when you assign a value to them.

a= 10 
b = 7.8 
c = 10j

You can verify the type of any object in Python, there is type() function that prints the type of the object.

print(type(a))  # int
print(type(b))  # float
print(type(c))  # complex

int (Signed Integers)

You can use int data type to represent whole numbers (integral values), positive or negative, without decimals, of unlimited length.In Python 2.x, there was a long data type to represent very large integral values. Whereas in Python 3.x, there is no long type explicitly and long values are represented by using int type only.

a= 10
b = 2323232323887711
c = -7777372522

print(type(a))   #int
print(type(b))   #int
print(type(c))   #int

You can represent int values in the following ways.

  • Decimal Form
  • Binary Form
  • Octal Form
  • Hexa decimal Form

Decimal Form (base-10)

It is the default number system in Python. The allowed digits are from 0 to 9.

a = 10

Binary Form (Base-2)

The allowed digits are 0 & 1. Literal value should be prefixed with 0b or 0B.

a = 0B1111
b = 0B123
c = 0b111

Octal Form(Base-8)

The allowed digits are 0 to 7. Literal value should be prefixed with 0o or 0O.

a=0O123
b=0o456

Hexa Decimal Form(Base-16)

The allowed digits are : 0 to 9, a-f (both lower and upper cases are allowed). Literal value should be prefixed with 0x or 0X.

a =0XFACE
a=0XAbcd
a =0XBeer

Note : Being a programmer, you can specify literal values in decimal, binary, octal and hexa decimal forms. But PVM will always print values in decimal form only.

a=10
b=0o10
c=0X10
d=0B10

#Here it will print output in decimal forms only.

print(a)   # 10
print(b)   # 8
print(c)   # 16
print(d)   # 2

Float (floating point real values)

You can use float data type to represent floating point values, positive or negative, containing one or more decimals.

f=1.234

print(type(f))   # float

You can also represent floating point values by using exponential form (scientific notation).

f=1.2e3

print(f)  # 1200.0

instead of ‘e‘ we can use ‘E‘. The main advantage of exponential form is we can represent big values in less memory.

Note: You can represent int values in decimal, binary, octal and hexa decimal forms. But you can represent float values only by using decimal form.

Complex (complex numbers)

A complex number contains real and imaginary part. For instance, a+bj, here a is real part and b is imaginary part. And a and b can be integers or floating point values.

3+5j
10+5.5j
0.5+0.1j

In the real part if you use int value then you can specify that either by decimal,octal,binary or hexa decimal form. But imaginary part should be specified only by using decimal form.

Random Number

Python does not contain a random() function to make a random numbers, but Python has a built-in module called random. You can use random module to make random numbers.

import random

print(random.randrange(1, 10))

It will print a random number between 1 and 10. It could be any number.

Previous Read : Python Data Types

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

1 thought on “Python Numbers”

Leave a Comment