Skip to main content

Two types of numbers

Python has two ways to store numbers: Integers - Whole numbers without decimals
Floats - Numbers with decimal points
Python uses a dot (.) for decimals, not a comma. Writing 3,14 creates a tuple, not the number 3.14!

Why two types?

Python separates integers and floats for efficiency - computers can work with whole numbers faster and more precisely. But here’s the reassuring part: in practice, you rarely need to think about this. Python handles the conversion automatically when needed, and most of the time you’ll just use whatever makes sense - whole numbers for counting things, decimals for measurements and calculations.

Basic math operations

Numbers work just like a calculator:

Integer vs float division

Division has two forms:
These are just the basics! Python can solve complex math problems too. When you need square roots, trigonometry, or advanced calculations, ask AI to help with the specific syntax - no need to memorize every math symbol right now.

Memory & Floating-Point Precision

Python has unique ways of handling numbers under the hood. Let’s look at three important concepts:

1. Everything is an Object

In Python, even simple numbers like integers and floats are objects. This means they have built-in methods you can call.

2. Integer Caching (Object Sharing)

To save memory, Python pre-loads and caches small integers (typically from -5 to 256). When you assign these values, Python points both variables to the same object in memory instead of creating a new one. We can inspect memory locations using the id() function or the is comparison operator:

3. Floating-Point Precision Issues

Floating-point numbers (decimals) are represented in binary (base-2) fractions. Since computers cannot represent fractions like 0.1 or 0.2 exactly in binary (they become infinite repeating fractions, just like 1/3 is 0.3333... in decimal), small rounding errors can occur.

Common mistakes

What’s next?

Now let’s learn about working with text in Python!

Strings

Text and characters