KAHIBARO Bookshelves
Discord Login Register

3.3. Numbers

Integer and Floating-Point Numbers in Python

In Python, numbers mainly come in two basic types:

  • int — whole numbers (integers)
  • float — decimal numbers (floating-point numbers)

You’ll use these constantly for calculations, counting, and measurements.

Integer numbers: `int`

An int represents whole numbers, both positive and negative, including zero.

Examples of int values:

  • 0
  • 5
  • -3
  • 100
  • 2025

Python automatically treats these as int when you write them in your code:

a = 10
b = -4
c = 0
print(a, type(a))
print(b, type(b))
print(c, type(c))

Integers:

  • Have no decimal point.
  • Can be very large (Python automatically handles big integers; you don’t need to worry about size for normal use).

Floating-point numbers: `float`

A float is a number with a decimal point, or a number written in scientific notation.

Examples of float values:

  • 3.14
  • 0.0
  • -2.5
  • 1.0
  • 5e3 (which means $5 \times 10^3 = 5000.0$)
  • 2.5e-3 (which means $2.5 \times 10^{-3} = 0.0025$)

Python treats any number with a decimal point as a float:

x = 3.14
y = -0.5
z = 2.0
print(x, type(x))
print(y, type(y))
print(z, type(z))

Scientific notation examples:

a = 5e3      # 5000.0
b = 2.5e-3   # 0.0025
print(a, type(a))
print(b, type(b))

Note: Even if a float ends with .0 (like 2.0), it is still a float, not an int.

How Python chooses `int` vs `float`

Python decides the type from how you write the number:

  • No decimal point → int
  • Has a decimal point → float
  • Scientific notation with e or E → float
a = 5      # int
b = 5.0    # float
c = 5.     # float
d = 0.5    # float
e = 5e0    # float
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))

Mixing `int` and `float` in calculations

When you combine int and float in an arithmetic operation, Python usually converts the result to float to avoid losing decimal information.

a = 5      # int
b = 2.0    # float
print(a + b)   # 7.0
print(a - b)   # 3.0
print(a * b)   # 10.0

Even if both numbers look “whole” but one is a float, the result will be float.

Division and number types

Python’s division operator / always returns a float, even if the numbers divide evenly.

print(4 / 2)     # 2.0 (float, not 2)
print(9 / 3)     # 3.0
print(5 / 2)     # 2.5

If you want integer (whole number) division that throws away the decimal part, you can use // (floor division). This keeps the type rules in mind, but the detailed explanation of operators belongs in the arithmetic operations chapter. Here’s just a quick demonstration of how it affects types:

print(5 // 2)     # 2  (int)
print(5.0 // 2)   # 2.0 (float, but no decimal fraction shown)

Notice how using a float on either side of // makes the result a float.

Converting between `int` and `float`

Sometimes you need to switch types. Python provides built-in functions for this:

From `int` to `float`

Use float():

a = 5          # int
b = float(a)   # convert to float
print(a, type(a))
print(b, type(b))

This will turn 5 into 5.0.

From `float` to `int`

Use int():

x = 3.9
y = int(x)
print(x, type(x))
print(y, type(y))

int() throws away the decimal part; it does not round.
So:

  • int(3.9) becomes 3
  • int(3.1) becomes 3
  • int(-2.7) becomes -2 (towards zero)

If you need rounding (e.g. to the nearest whole number), that will be covered with other built-in functions later; for now, remember that int() simply cuts off the decimal part.

Typical uses of `int` vs `float`

Common uses:

  • Use int for:
    • Counting things (people, items, attempts)
    • Indexes and positions in lists
    • Whole-number calculations (like pages, days, rounds)
  • Use float for:
    • Measurements (height, weight, temperature)
    • Money in simple examples (though real financial apps need special handling)
    • Percentages, averages, and anything that naturally needs decimals

Example:

age = 25             # int
number_of_items = 3  # int
temperature = 21.5   # float
height_m = 1.68      # float
print(age, type(age))
print(temperature, type(temperature))

Pitfalls with floating-point numbers

Floats are stored in a way that can’t exactly represent all decimal numbers. This sometimes leads to surprising results.

For example:

print(0.1 + 0.2)

You might expect 0.3, but Python could show something like:

0.30000000000000004

This isn’t a bug in Python; it’s how floating-point numbers work in almost all programming languages.

For beginners, the main takeaway:

  • Small “weird” extra decimals with float are normal.
  • Avoid directly comparing floats for exact equality in serious programs (this idea will matter more later).

Checking the type of a number

To confirm whether a value is int or float, you can use type():

a = 10
b = 10.0
print(type(a))  # <class 'int'>
print(type(b))  # <class 'float'>

You may also see isinstance() used to check types:

print(isinstance(a, int))    # True
print(isinstance(a, float))  # False
print(isinstance(b, float))  # True

This is useful when you want to know what kind of number you’re working with inside a program.

Simple practice ideas

Here are a few small things you can try on your own:

  1. Create two int variables, add them, and check the type of the result.
  2. Create one int and one float, multiply them, and check the type of the result.
  3. Convert an int to float and back to int using float() and int().
  4. Experiment with 0.1 + 0.2, 0.1 + 0.1 + 0.1, and see the results.

Views: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Chapter 1: What Is Programming?
    • ☰ 1.1. What is a computer program?
      • ☰ 1.2. What is Python?
        • ☰ 1.3. Where is Python used?
          • ☰ 1.4. How programming helps solve problems
            • ☰ 1.5. Your first Python program
            • ☰ 2. Chapter 2: Setting Up Python
              • ☰ 2.1. Installing Python (Windows / macOS / Linux)
                • ☰ 2.2. Using Python in the terminal
                  • ☰ 2.3. Using an IDE (IDLE / VS Code)
                    • ☰ 2.4. Running Python scripts
                      • ☰ 2.5. Python interactive mode
                      • ☰ 3. Chapter 3: Variables and Data Types
                        • ☰ 3.1. What is a variable?
                          • ☰ 3.2. Creating variables
                            • ☰ 3.3. Numbers
                              • ☰ 3.4. Test
                                • ☰ 3.5. Boolean values
                                  • ☰ 3.6. Type checking
                                  • ☰ 4. Chapter 4: Basic Operations
                                    • ☰ 4.1. Arithmetic operations
                                      • ☰ 4.2. Working with numbers
                                        • ☰ 4.3. String operations
                                          • ☰ 4.4. Combining text and variables
                                            • ☰ 4.5. Comments in Python
                                            • ☰ 5. Chapter 5: User Input and Output
                                              • ☰ 5.1. Getting input from the user
                                                • ☰ 5.2. Converting input to numbers
                                                  • ☰ 5.3. Formatting output
                                                    • ☰ 5.4. Writing interactive programs
                                                    • ☰ 6. Chapter 6: Conditions
                                                      • ☰ 6.1. Comparison operators
                                                        • ☰ 6.2. Logical operators
                                                          • ☰ 6.3. if, elif, else
                                                            • ☰ 6.4. Nested conditions
                                                              • ☰ 6.5. Common beginner mistakes
                                                              • ☰ 7. Chapter 7: Loops
                                                                • ☰ 7.1. Why loops are useful
                                                                  • ☰ 7.2. for loops
                                                                    • ☰ 7.3. while loops
                                                                      • ☰ 7.4. Loop counters
                                                                        • ☰ 7.5. break and continue
                                                                          • ☰ 7.6. Loop exercises
                                                                          • ☰ 8. Chapter 8: Data Collections
                                                                            • ☰ 8.1. Lists
                                                                              • ☰ 8.2. Tuples
                                                                                • ☰ 8.3. Dictionaries
                                                                                  • ☰ 8.4. Sets (basics)
                                                                                    • ☰ 8.5. Choosing the right data structure
                                                                                    • ☰ 9. Chapter 9: Functions
                                                                                      • ☰ 9.1. Why functions matter
                                                                                        • ☰ 9.2. Defining functions
                                                                                          • ☰ 9.3. Parameters and arguments
                                                                                            • ☰ 9.4. Return values
                                                                                              • ☰ 9.5. Scope (local vs global)
                                                                                                • ☰ 9.6. Reusing code
                                                                                                • ☰ 10. Chapter 10: Working with Files
                                                                                                  • ☰ 10.1. What is a file?
                                                                                                    • ☰ 10.2. Reading text files
                                                                                                      • ☰ 10.3. Writing and appending files
                                                                                                        • ☰ 10.4. File paths
                                                                                                          • ☰ 10.5. Simple file-based programs
                                                                                                          • ☰ 11. Chapter 11: Errors and Debugging
                                                                                                            • ☰ 11.1. Types of errors
                                                                                                              • ☰ 11.2. Reading error messages
                                                                                                                • ☰ 11.3. Common beginner errors
                                                                                                                  • ☰ 11.4. try and except
                                                                                                                    • ☰ 11.5. Debugging strategies
                                                                                                                    • ☰ 12. Chapter 12: Using Python Libraries
                                                                                                                      • ☰ 12.1. What is a library?
                                                                                                                        • ☰ 12.2. Importing modules
                                                                                                                          • ☰ 12.3. Standard libraries
                                                                                                                            • ☰ 12.3.1. math
                                                                                                                              • ☰ 12.3.2. random
                                                                                                                                • ☰ 12.3.3. datetime
                                                                                                                                • ☰ 12.4. Installing external libraries with pip
                                                                                                                                • ☰ 13. Chapter 13: Object-Oriented Programming
                                                                                                                                  • ☰ 13.1. What is object-oriented programming?
                                                                                                                                    • ☰ 13.2. Classes and objects
                                                                                                                                      • ☰ 13.3. The `__init__` method
                                                                                                                                        • ☰ 13.4. Attributes and methods
                                                                                                                                          • ☰ 13.5. Encapsulation
                                                                                                                                            • ☰ 13.6. Inheritance (basics)
                                                                                                                                              • ☰ 13.7. Simple OOP examples
                                                                                                                                              • ☰ 14. Chapter 14: Automation with Python
                                                                                                                                                • ☰ 14.1. What is automation?
                                                                                                                                                  • ☰ 14.2. Automating simple tasks
                                                                                                                                                    • ☰ 14.3. Working with files and folders
                                                                                                                                                      • ☰ 14.4. Automating text processing
                                                                                                                                                        • ☰ 14.5. Scheduling scripts
                                                                                                                                                          • ☰ 14.6. Practical automation examples
                                                                                                                                                          • ☰ 15. Chapter 15: Web Development with Python
                                                                                                                                                            • ☰ 15.1. How the web works (basic overview)
                                                                                                                                                              • ☰ 15.2. Introduction to Flask
                                                                                                                                                                • ☰ 15.3. Creating a simple web server
                                                                                                                                                                  • ☰ 15.4. Routing and templates
                                                                                                                                                                    • ☰ 15.5. Handling user input (forms)
                                                                                                                                                                      • ☰ 15.6. Very basic APIs
                                                                                                                                                                      • ☰ 16. Chapter 16: Data Science with Python
                                                                                                                                                                        • ☰ 16.1. What is data science?
                                                                                                                                                                          • ☰ 16.2. Working with data
                                                                                                                                                                            • ☰ 16.3. Introduction to NumPy
                                                                                                                                                                              • ☰ 16.4. Introduction to pandas
                                                                                                                                                                                • ☰ 16.5. Data visualization with matplotlib
                                                                                                                                                                                  • ☰ 16.6. Simple data analysis examples
                                                                                                                                                                                  • ☰ 17. Chapter 17: Mini Projects
                                                                                                                                                                                    • ☰ 17.1. Number guessing game
                                                                                                                                                                                      • ☰ 17.2. Simple calculator
                                                                                                                                                                                        • ☰ 17.3. To-do list
                                                                                                                                                                                          • ☰ 17.4. Password generator
                                                                                                                                                                                            • ☰ 17.5. Quiz application
                                                                                                                                                                                              • ☰ 17.6. Small web or data project
                                                                                                                                                                                              • ☰ 18. Chapter 18: Next Steps
                                                                                                                                                                                                • ☰ 18.1. How to practice Python
                                                                                                                                                                                                  • ☰ 18.2. Writing clean and readable code
                                                                                                                                                                                                    • ☰ 18.3. Choosing a specialization
                                                                                                                                                                                                      • ☰ 18.3.1. Web development
                                                                                                                                                                                                        • ☰ 18.3.2. Data science
                                                                                                                                                                                                          • ☰ 18.3.3. Automation
                                                                                                                                                                                                            • ☰ 18.3.4. Software development
                                                                                                                                                                                                            • ☰ 18.4. Learning resources and communities
                                                                                                                                                                                                            • ☰ 19. Appendix
                                                                                                                                                                                                              • ☰ 19.1. Python syntax cheat sheet
                                                                                                                                                                                                                • ☰ 19.2. Common errors and solutions
                                                                                                                                                                                                                  • ☰ 19.3. Glossary of terms