Python - Numbers

Python

Share
python logo

In this tutorial we will learn about Numbers in Python.

We use number data type to hold numerical values.

A variable of number type is immutable i.e. when we reassign a new value to the same variable then a new object is created.

Number types

We can group numbers into four types listed below.

Integer Number

These represents positive and negative whole numbers without decimal point.

Example of integer: -100, 10, 0, -1, 1000

Integers are often referred as int and if you come from other programming language like C or Java then you know int data type.

In the following Python code we are creating a variable and assigning an integer value.

# variable
x = 10

# detail
print("Value of x:", x, " Type of x:", type(x))

This will give us the following output.

Value of x: 10  Type of x: <class 'int'>

We use the type() function to find the type of a variable.

Long a.k.a Long Integer

Long represents positive and negative whole numbers without decimal point and can hold larger integer values.

Long values are ended with uppercase L or lowercase l.

Example of long: 123456789012345678901234567890L

You will generally see uppercase L with long integer values as lowercase letter l can be confused with number 1.

Long are also referred as long integer.

long and int were unified in Python. So, now we use int to also hold very large integer values. Check out this documentation.

Floating Point Number

Float or Floating Point numbers represent real numbers with decimal point.

Example of float: -123.456, 0, 678.9

In the following Python code we are creating a variable and assigning a floating point value.

# variable
x = 3.14

# detail
print("Value of x:", x, " Type of x:", type(x))

This will give us the following output.

Value of x: 3.14  Type of x: <class 'float'>

Complex Number

Complex data type is used to represent complex number.

In Python complex numbers are represented as x + yj where, x represent the real part and y represent the imaginary part.

Example of complex numbers: 10+4j, -1+1j

In the following Python code we are creating a variable and assigning a complex value.

# variable
x = 3 + 14j

# detail
print("Value of x:", x, " Type of x:", type(x))

This will give us the following output.

Value of x: (3+14j)  Type of x: <class 'complex'>

The del statement

We use the del statement to delete the reference of a number object.

In the following example we are deleting a single number objects.

del x

We can also delete multiple number objects by separating them with comma.

del x, y, z
Share