Command Palette

Search for a command to run...

Introduction to Number Data Types

In Python programming, numbers are one of the most fundamental data types you'll use every day. Think of numbers as basic ingredients in cooking. Without good basic ingredients, the dish you create won't be perfect. Similarly with programming, without understanding numbers well, the programs you create will be difficult to develop.

Python is very flexible in handling numbers. When you type numbers in the Python shell, Python immediately recognizes and displays them back. Let's see how Python works with numbers.

Pythonbasic_numbers.py
>>> 44>>> 4.4.0>>> 0.300.3>>> 0.00009999.99e-05>>> 2. + 1j(2+1j)

From the example above, you can see Python automatically recognizes various number formats. When you type 44, Python knows it's an integer. When you type 4.4. with a dot at the end, Python converts it to a decimal number 4.04.0.

Types of Number Data

Python has three main categories for numbers, each with special characteristics you need to understand.

Integer

Integer is a whole number without decimal points. This includes positive, negative, and zero numbers. In daily life, you use integers to count things that can't be broken down, like the number of students in a class or page numbers in a book.

Valid integer examples

2,1,0,1,2-2, -1, 0, 1, 2
+1,+2,+3+1, +2, +3

Special features of integers in Python

Python allows you to use underscores to make large numbers easier to read. For example, 123_456_789123\_456\_789 is actually the same as 123456789123456789. This is like using commas in number writing in Indonesian language.

Float

Float is a number that has decimal points. You'll often use floats for calculations that require precision, such as measuring distance, weight, or percentages.

Common float examples

1.2,3.85,+7.01-1.2, 3.85, +7.01
.21,.01,2.,+5.-.21, .01, 2., +5.

Scientific notation for very small or large numbers

Python uses scientific notation for very small or very large numbers. For example:

3.2e5=3.2×105-3.2e5 = -3.2 \times 10^5
2e2=2×1022e-2 = 2 \times 10^{-2}
+2.e4=+2.0×104+2.e4 = +2.0 \times 10^4

The notation ee or EE in Python is the same as the scientific notation you learned in mathematics. The number 1E11E1 equals 1×101=10.01 \times 10^1 = 10.0, and 3.1E73.1E-7 equals 3.1×1073.1 \times 10^{-7}.

Complex

Complex numbers consist of real and imaginary parts. In Python, the letter jj is used to indicate the imaginary part. This is often used in advanced mathematics, signal processing, and machine learning.

Complex number examples

2+3j2 + 3j
4.52.1j4.5 - 2.1j

Imagine complex numbers like coordinates on a map. The real part is the horizontal position, and the imaginary part is the vertical position.

The type Function for Checking Data Types

When you work with complex data, sometimes you need to ensure the data type you're handling. Python provides the type() function for this purpose.

Pythoncheck_types.py
>>> type(5)<class 'int'>>>> type(.1)<class 'float'>>>> type(2j)<class 'complex'>

The type() function is useful when you're debugging programs or want to ensure the received data matches expectations. It's like checking labels on food packaging to make sure you get the right product.

Constructors for Creating Number Objects

Python provides constructors to convert or create number objects from other types. This is very useful when you need to convert data from one format to another.

Constructor float

The float() constructor converts other numbers into decimal numbers.

Pythonfloat_constructor.py
>>> float(2)2.0>>> float()0.0

When you call float(2), Python converts the integer 22 to float 2.02.0. If you call float() without arguments, Python gives the default value 0.00.0.

Constructor int

The int() constructor converts other numbers to integers by discarding the decimal part.

Pythonint_constructor.py
>>> int(2.1)2>>> int(2.9)2

Note that int() doesn't round numbers, but truncates the decimal part. Both int(2.1) and int(2.9) result in 22. This is like cutting off the tail of decimal numbers, not rounding them.

Constructor complex

The complex() constructor creates complex numbers from real numbers.

Pythoncomplex_constructor.py
>>> complex(2.)(2+0j)>>> complex(0., 2.)2j

complex(2.) creates a complex number with real part 22 and imaginary part 00. Meanwhile, complex(0., 2.) creates a pure imaginary number 2j2j.

Number Data Types Summary Table

CategoryTypeExamplesDescription
Integerint2,1,0,1,2-2, -1, 0, 1, 2 and +1,+2,+3+1, +2, +3Whole numbers, can use underscore for clarity
Floating-Pointfloat1.2,3.85,+7.01-1.2, 3.85, +7.01 and .21,.01,2.-.21, .01, 2.Decimal numbers, supports scientific notation with ee or EE
Complexcomplex2+3j2 + 3j and 4.52.1j4.5 - 2.1jComplex numbers with jj as imaginary unit