# Nakafa Learning Content

> For AI agents: use [llms.txt](https://nakafa.com/llms.txt) for the site index. Markdown versions are available by appending `.md` to content URLs or sending `Accept: text/markdown`.

URL: https://nakafa.com/en/subjects/ai-ds/ai-programming/numbers
Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/material/lesson/ai-ds/ai-programming/numbers/en.mdx

Python number types such as integers, floats, and complex numbers, including type checking, constructors, and scientific notation.

---

## 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.

File: basic_numbers.py
```python
>>> 4
4

>>> 4.
4.0

>>> 0.30
0.3

>>> 0.0000999
9.99e-05

>>> 2. + 1j
(2+1j)
```

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

Visible text: 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 one room or page numbers in a book.

**Valid integer examples**

Component: MathContainer
Children:

```math
-2, -1, 0, 1, 2
```

```math
+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\_789$$ is actually the same as $$123456789$$. This is like using commas in number writing in Indonesian language.

Visible text: 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**

Component: MathContainer
Children:

```math
-1.2, 3.85, +7.01
```

```math
-.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:

Component: MathContainer
Children:

```math
-3.2e5 = -3.2 \times 10^5
```

```math
2e-2 = 2 \times 10^{-2}
```

```math
+2.e4 = +2.0 \times 10^4
```

The notation $$e$$ or $$E$$ in Python is the same as the scientific notation you learned in mathematics. The number $$1E1$$ equals $$1 \times 10^1 = 10.0$$, and $$3.1E-7$$ equals $$3.1 \times 10^{-7}$$.

Visible text: 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 $$j$$ is used to indicate the imaginary part. This is often used in advanced mathematics, signal processing, and machine learning.

Visible text: 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**

Component: MathContainer
Children:

```math
2 + 3j
```

```math
4.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.

File: check_types.py
```python
>>> 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.

File: float_constructor.py
```python
>>> float(2)
2.0

>>> float()
0.0
```

When you call `float(2)`, Python converts the integer $$2$$ to float $$2.0$$. If you call `float()` without arguments, Python gives the default value $$0.0$$.

Visible text: 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.

File: int_constructor.py
```python
>>> 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 $$2$$. This is like cutting off the tail of decimal numbers, not rounding them.

Visible text: 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.

File: complex_constructor.py
```python
>>> complex(2.)
(2+0j)

>>> complex(0., 2.)
2j
```

`complex(2.)` creates a complex number with real part $$2$$ and imaginary part $$0$$. Meanwhile, `complex(0., 2.)` creates a pure imaginary number $$2j$$.

Visible text: `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` | $$-2, -1, 0, 1, 2$$ and $$+1, +2, +3$$ | Whole numbers, can use underscore for clarity |
| **Floating-Point** | `float` | $$-1.2, 3.85, +7.01$$ and $$-.21, .01, 2.$$ | Decimal numbers, supports scientific notation with $$e$$ or $$E$$ |
| **Complex** | `complex` | $$2 + 3j$$ and $$4.5 - 2.1j$$ | Complex numbers with $$j$$ as imaginary unit |

Visible text: | 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 |