Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Numbers are used everywhere in programming.
You may use numbers to:
Python provides three important built-in numeric types:
| Type | Name | Example | Common Use |
|---|---|---|---|
int | Integer | 25 | Whole numbers |
float | Floating-point | 25.5 | Decimal values |
complex | Complex number | 3 + 4j | Scientific and engineering calculations |
Let's understand each one step by step.
int)An integer is a whole number without a decimal part.
Examples:
All of these values are integers.
Output:
Integers can be positive, negative, or zero.
Imagine counting physical objects.
If you have 10 books:
You can have:
But normally, you don't say:
That's why counting objects commonly uses integers.
One useful feature of Python is that integers can grow much larger than the typical fixed-size integers used in many other programming languages.
For example:
Output:
Python can handle this large integer without the usual fixed 32-bit integer limit.
You can also check how many digits it has:
Output:
Large numbers can be difficult to read.
Compare:
with:
Both represent exactly the same number.
Output:
Python simply ignores the underscores when reading numeric literals.
For large numbers, use underscores:
This is easier to read than:
Python also allows integers to be written in different number systems.
Prefix: 0b
Output:
Prefix: 0o
Output:
Prefix: 0x
Output:
These are especially useful when working with computers, networking, colors, and low-level programming.
float)A float is a number that can contain a decimal part.
Examples:
Check the type:
Output:
Suppose you are measuring the weight of gold:
The value contains a decimal part, so a float is useful.
Other examples:
Python also supports scientific notation using e or E.
For example:
This means:
So:
Output:
You can also represent very small numbers:
Output:
Scientific notation is useful for very large or very small numbers.
You may notice something surprising in Python:
Output:
You may expect:
Why does this happen?
Computers store floating-point numbers using binary representation.
Some decimal values, such as 0.1 and 0.2, cannot be represented exactly in binary floating-point format.
So Python stores a very close approximation.
This is not a Python bug.
It is a normal limitation of binary floating-point arithmetic.
Because of this behavior, avoid relying on exact equality when comparing calculated floating-point values.
For example:
Output:
Instead, Python provides math.isclose() for checking whether two numbers are sufficiently close.
Output:
math.isclose() allows a small difference caused by floating-point representation.
round() vs math.isclose()These two functions solve different problems.
round()Use round() when you want to control the displayed or stored rounded value:
Output:
math.isclose()Use math.isclose() when you want to compare two calculated values:
Output:
round() is useful for rounding.
math.isclose() is useful for comparing floating-point results.For many simple examples, float is fine.
However, financial applications may need exact decimal arithmetic.
For example:
Python provides the decimal.Decimal type for such situations.
Example:
Output:
For now:
int for whole numbers.float for normal decimal calculations.Decimal later when you work with precise financial calculations.complex)A complex number contains two parts:
In mathematics, complex numbers are commonly written as:
Python uses j instead of i.
For example:
Here:
j?Python uses j or J to represent the imaginary part.
Correct:
Also correct:
Incorrect:
Python does not use i as the imaginary-number suffix.
You can create a complex number directly:
Output:
You can also use the complex() function:
Output:
Python provides .real and .imag.
Output:
Notice that these values are returned as floats.
Python supports normal arithmetic with complex numbers.
Output:
The conjugate changes the sign of the imaginary part.
For example:
becomes:
Python:
Output:
The magnitude of:
is:
which equals:
Python can calculate it using abs():
Output:
This is similar to finding the length of the hypotenuse of a right triangle.
Complex numbers are commonly used in areas such as:
For example, electrical engineers can represent impedance as:
where:
R = resistanceX = reactancej = imaginary unitPython's built-in complex type makes these calculations easier.
| Situation | Recommended Type | Example |
|---|---|---|
| Number of students | int | 50 |
| Age | int | 21 |
| Product quantity | int | 5 |
| Temperature | float | 36.5 |
| Product price | float | 99.99 |
| Scientific measurement | float | 1.5e8 |
| Electrical impedance | complex | 40 + 5j |
| Precise currency calculation | Decimal | Decimal("99.99") |
Let's combine what we learned:
Possible output:
| Situation | Don't | Do |
|---|---|---|
| Large numbers | 15000000 | 15_000_000 |
| Float comparison | total == 0.3 | math.isclose(total, 0.3) |
| Complex numbers | 3 + 4i | 3 + 4j |
| Imaginary value | 5 + j | 5 + 1j |
| Precise financial calculations | Rely blindly on float | Consider Decimal |
math.isclose() when appropriate for comparisonsj.real gets the real part.imag gets the imaginary part.conjugate() returns the conjugateabs() returns the magnitude0.1 + 0.2 produce 0.30000000000000004?Create a small program that demonstrates all three Python numeric types.
Your program should:
math.isclose() to compare a calculated value.After running the program, try changing:
to another integer.
Then change:
to another decimal value.
Finally, experiment with:
For example:
Observe how the real part, imaginary part, and magnitude change.
In this lesson, you learned:
int represents whole numbersfloat represents decimal valuesmath.isclose() helps compare floating-point valuesDecimal may be useful for financial calculationsj.real and .imagIn the next lesson, we will continue your Python learning journey with Mathematical Operations (4: Numbers).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Checking Data Types (isinstance) | Mathematical Operations |
Test your understanding of this lesson with 6 questions. Each question has one correct answer.