Custom Search

PYTHON: Numbers

Python numbers are a data type that stores numeric values.

Numbers are an immutable data type – which is a type of object whose data cannot be changed after its creation.

An immutable type sets the property or state of the object as read only because it cannot be modified after it is assigned during initialization.

Immutable types are designed for efficient memory management and better speed, which makes them suitable for objects with synchronization requirements. Immutability provides better code readability by making changes in program state visibility and isolating the operations that change state from those that do not. Immutable types provides higher security than mutable types.

Number types

The class of number type is returned by the function: type()

 

There are three types of number:

1 - the 'int' Type

This is a 'whole number' an integer, it can be positive or negative, but it is without decimals of no limit to its length.

For example:

number = 23

print(type(number))

the output is <class 'int'>

2 - the 'float' Type

A float (or floating-point) number is a number containing one or more decimals - it can positive or negative.

Float numbers can also be scientific numbers with an "e" to indicate the power of 10.

3 - the 'complex' Type (Imaginary numbers)

Complex numbers are written with a “j” as the imaginary part, rather than using the 'i' which would cause confusion.

In python, you can put ‘j’ or ‘J’ after a number to make it imaginary, so you can write complex numbers easily: