Two types of numbers
Python has two ways to store numbers: Integers - Whole numbers without decimalsWhy 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: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 like0.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
Division always returns float
Division always returns float
Can't use commas in numbers
Can't use commas in numbers
What’s next?
Now let’s learn about working with text in Python!Strings
Text and characters