Fundamental Data Types: Numbers (Integers, Floating-Point)
Following our exploration of Variables and Assignment, this article dives into one of the most fundamental data types in Python: numbers. We'll cover integers and floating-point numbers, and the common operations you can perform on them.
📚 Prerequisites
A basic understanding of Python variables and assignment.
🎯 Article Outline: What You'll Master
In this article, you will learn:
- ✅ What integers and floating-point numbers are.
- ✅ How to create and use integers and floats in Python.
- ✅ Common arithmetic operations.
- ✅ Type conversion between numbers.
🧠 Section 1: Integers (int)
Integers are whole numbers, both positive and negative, without any decimal points. In Python, integers can be of any size, limited only by your computer's memory.
# Integer examples
my_integer = 10
negative_integer = -5
large_integer = 1_000_000 # You can use underscores for readability
💻 Section 2: Floating-Point Numbers (float)
Floating-point numbers, or floats, are numbers that have a decimal point. They are used to represent real numbers.
# Float examples
my_float = 3.14
negative_float = -0.5
scientific_notation = 2.5e2 # This is 2.5 * 10^2, or 250.0
A quick note on float precision: Due to how computers store floating-point numbers, you might sometimes see small rounding errors. For most applications, this isn't a problem, but it's something to be aware of.
🛠️ Section 3: Arithmetic Operations
Python supports all the standard arithmetic operations you'd expect:
| Operator | Name | Example |
|---|---|---|
+ | Addition | 10 + 5 |
- | Subtraction | 10 - 5 |
* | Multiplication | 10 * 5 |
/ | Division | 10 / 5 (always results in a float) |
// | Floor Division | 10 // 3 (discards the remainder) |
% | Modulus | 10 % 3 (returns the remainder) |
** | Exponentiation | 10 ** 2 (10 to the power of 2) |
🔬 Section 4: Type Conversion
You can easily convert between integers and floats using the int() and float() functions.
my_float = 3.14
my_integer = int(my_float) # my_integer is now 3 (the decimal part is truncated)
print(my_integer)
my_integer = 10
my_float = float(my_integer) # my_float is now 10.0
print(my_float)
💡 Conclusion & Key Takeaways
You've now learned about the fundamental number types in Python and how to perform common operations on them.
Let's summarize the key takeaways:
- Integers (
int): Whole numbers. - Floats (
float): Numbers with a decimal point. - Arithmetic Operators: Python supports all the standard arithmetic operators.
- Type Conversion: You can convert between integers and floats using
int()andfloat().
Challenge Yourself:
Write a script that calculates the area of a circle with a radius of 5. (Hint: the formula is pi * r^2).
➡️ Next Steps
In the next article, we'll continue our exploration of fundamental data types with "Fundamental Data Types: Strings (Part 1)".
Happy coding!
Glossary (Python Terms)
- Integer: A whole number.
- Float: A number with a decimal point.
- Operator: A symbol that performs an operation on one or more operands.