Learn Python: Numeric Types and Operations
Python handles numeric data in flexible ways, supporting both integer (int) and floating-point (float) types out of the box. Integers in Python have unlimited precision, meaning they can grow to very large numbers without overflow (though they take more memory as they grow). Floats, on the other hand, approximate real numbers but have limited precision. This lesson explores basic arithmetic operators, covers the difference between standard division and floor division, and introduces a few helpful numeric functions. You will also see how to import the math module for more advanced operations.
What will be covered
• Distinguishing between int and float.
• Arithmetic operators (+, -, *, /, //, %, **).
• Floor division vs. classic division.
• Simple numeric functions like abs() and round().
• Using Python’s math module for constants (like math.pi) and functions (like math.sqrt).
Integer vs. Float
An integer represents a whole number (such as 3 or 100). A float represents a number with a decimal point (like 3.14). Integers in Python can get arbitrarily large, but floats can only be so precise because of how they are stored in memory. For example, 3.14 might be slightly off internally when represented as a float.
Arithmetic Operators
Python provides a set of operators for basic arithmetic:
Floor division (//) differs from classic division in that it discards the fractional part. For instance, 5 // 2 is 2, whereas 5 / 2 is 2.5. The modulus (%) gives the remainder after division (5 % 2 is 1).
Examples of Arithmetic
Below is a small script showing how to use some of these operators. Save this as basic_arithmetic.py in VS Code and run it with python basic_arithmetic.py.
print(5 + 2)
print(5 - 2)
print(5 * 2)
print(5 / 2)
print(5 // 2)
print(5 % 2)
print(5 ** 2)
You can experiment with these operations using different numbers, including floats like 2.5 or 7.0, to see how the output changes.
Using abs(), round(), and the math Module
Python includes convenient numeric functions. abs() returns the absolute value, and round() can round floats to a specified number of decimal places if you pass a second argument. The math module offers more advanced tools:
import math
print(abs(-10))
print(round(3.14159, 2))
print(math.sqrt(16))
print(math.pi)
math.sqrt(16) yields 4.0, and math.pi is a float approximation of π (~3.1415926535...). Note that rounding or floating-point precision can affect the exact output you see.
Practice Exercises
- Compute the area of a circle with radius 5. Use 3.14 for π or import math and use math.pi. Print the result. Example formula: area = π * (5 ** 2).
- Write a script named division_demo.py that asks the user for two numbers using input(), then prints both the result of regular division (/) and floor division (//). Also print the remainder (%) for extra practice.
- Import the math module in a file named math_practice.py. Print the square root of 2 using math.sqrt(2). Then convert 90 degrees to radians with math.radians(90) and pass it to math.sin() to see the sine of 90 degrees. Also try math.floor() on a float like 3.7.
- Optional: experiment with negative numbers in floor division. For instance, do -5 // 2 and see that the result might be different than you expect (it is -3, because it floors to the next lower integer).
Summary
You explored how Python handles numeric data, including the distinction between int and float, and how to perform arithmetic using +, -, *, /, //, %, and **. You also saw that floor division truncates towards negative infinity, while normal division produces a float result. Python’s built-in numeric functions can simplify tasks like rounding and taking absolute values. The math module opens up additional capabilities, letting you compute square roots, trigonometric functions, and more. With these fundamentals, you can confidently handle calculations and basic mathematical operations in your Python code.