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.
>>> 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 , Python knows it's an integer. When you type with a dot at the end, Python converts it to a decimal number .
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
Special features of integers in Python
Python allows you to use underscores to make large numbers easier to read. For example, is actually the same as . 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
Scientific notation for very small or large numbers
Python uses scientific notation for very small or very large numbers. For example:
The notation or in Python is the same as the scientific notation you learned in mathematics. The number equals , and equals .
Complex
Complex numbers consist of real and imaginary parts. In Python, the letter is used to indicate the imaginary part. This is often used in advanced mathematics, signal processing, and machine learning.
Complex number examples
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.
>>> 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.
>>> float(2)2.0>>> float()0.0
When you call float(2)
, Python converts the integer to float . If you call float()
without arguments, Python gives the default value .
Constructor int
The int()
constructor converts other numbers to integers by discarding the decimal part.
>>> 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 . This is like cutting off the tail of decimal numbers, not rounding them.
Constructor complex
The complex()
constructor creates complex numbers from real numbers.
>>> complex(2.)(2+0j)>>> complex(0., 2.)2j
complex(2.)
creates a complex number with real part and imaginary part . Meanwhile, complex(0., 2.)
creates a pure imaginary number .
Number Data Types Summary Table
Category | Type | Examples | Description |
---|---|---|---|
Integer | int | and | Whole numbers, can use underscore for clarity |
Floating-Point | float | and | Decimal numbers, supports scientific notation with or |
Complex | complex | and | Complex numbers with as imaginary unit |