Integers (int)
Integers are whole numbers — positive, negative, or zero. Python integers have unlimited precision: they can be arbitrarily large with no overflow.
x = 10
y = -5
z = 0
big = 10 ** 100 # Python handles huge numbers!
print(type(x)) # <class 'int'>
print(big) # 1 followed by 100 zeros
Floats (float)
Floats are decimal numbers. They use 64-bit IEEE 754 double-precision, which means they have ~15-17 significant digits but can have precision issues.
pi = 3.14159
temp = -23.5
scientific = 1.5e-3 # 0.0015
print(type(pi)) # <class 'float'>
print(0.1 + 0.2) # Floating point gotcha!
print(round(0.1 + 0.2, 2)) # Fix: use round()
Never use floats for financial calculations. Use the decimal module instead: from decimal import Decimal
Arithmetic Operations
Python supports all standard arithmetic operations. Note: ** is power, // is floor division, % is modulo (remainder).
a, b = 17, 5
print(a + b) # 22 - addition
print(a - b) # 12 - subtraction
print(a * b) # 85 - multiplication
print(a / b) # 3.4 - true division (always float)
print(a // b) # 3 - floor division (rounds down)
print(a % b) # 2 - modulo (remainder)
print(a ** b) # 1419857 - power (17^5)
The math Module
Python's built-in math module provides advanced mathematical functions.
import math
print(math.sqrt(16)) # 4.0 - square root
print(math.pi) # 3.141592653589793
print(math.ceil(4.2)) # 5 - round up
print(math.floor(4.8)) # 4 - round down
print(math.log(100, 10))# 2.0 - log base 10
print(math.factorial(5))# 120
print(math.gcd(48, 18)) # 6 - greatest common divisor
Number Type Conversion
Convert between number types with int(), float(), and complex(). Note that int() truncates (does not round) floats.
print(int(3.9)) # 3 - truncates, does NOT round
print(float(5)) # 5.0
print(int("42")) # 42 - from string
print(float("3.14"))# 3.14
# Check if string is numeric before converting
value = "123"
if value.isdigit():
print(int(value) * 2) # 246
Numbers: int Is Unbounded, float Is Not Exact
Python has two everyday number types with very different behavior. int has arbitrary precision — it never overflows. float is IEEE-754 double precision — fast, but it can't represent many decimals exactly.
2 ** 1000 # a 300+ digit int — no overflow, ever
0.1 + 0.2 # 0.30000000000000004 ← float rounding!
0.1 + 0.2 == 0.3 # False (surprises everyone)
7 // 2 # 3 floor division
7 % 2 # 1 remainder
7 / 2 # 3.5 true division always gives float
| Need | Use |
|---|---|
| counting, ids, big integers | int |
| measurements, science | float |
| money, exact decimals | decimal.Decimal |
The money rule: never use float for currency — those tiny rounding errors compound. Use Decimal("0.1") for exact decimal math. And to compare floats, don't use ==; use math.isclose(a, b) to allow for the tiny representation error. Division with / always returns a float even for whole results (4 / 2 == 2.0); use // when you want an integer.
🏋️ Practical Exercise
Work with both numeric types:
- Create an integer and a float, then print the
type()of each. - Compute integer division, float division, modulo, and exponentiation on two numbers.
- Use the
mathmodule to find a square root and round a value up withmath.ceil(). - Convert a float to an int and observe how the decimal part is truncated.
🔥 Challenge Exercise
Write a program that takes the radius of a circle and reports its area and circumference using math.pi, rounded to two decimal places. Then demonstrate the classic floating-point surprise by printing 0.1 + 0.2 and explain in a comment why it is not exactly 0.3. Bonus: fix the comparison using math.isclose().
📋 Summary
- Python has three numeric types:
int(unlimited precision),float, andcomplex. /always returns a float;//performs floor (integer) division.%gives the remainder and**raises to a power.- Floats follow IEEE 754, so some decimals (like 0.1) cannot be represented exactly.
- The
mathmodule providessqrt,ceil,floor,pi, and more. - Use
int()andfloat()to convert;int()truncates toward zero.
Interview Questions on Numbers
- What numeric types does Python provide?
- What is the difference between
/and//in Python? - Why is
0.1 + 0.2not exactly0.3? - How do you round a number up or down in Python?
- What does the
%operator do with numbers? - How do you convert between
intandfloat? - What is the difference between
round()andmath.floor()?
Related Topics
FAQ
0.1 + 0.2 give 0.30000000000000004? +Floats are stored in binary (IEEE 754), and 0.1 and 0.2 have no exact binary representation, so a tiny rounding error remains. Use round() for display or math.isclose() for comparisons, and the decimal module when exactness matters (e.g. money).
No. Python integers have arbitrary precision and grow to fit available memory, so you can compute huge factorials without overflow — unlike fixed-width integers in C or Java.
// and /? +/ is true division and always returns a float (7 / 2 == 3.5). // is floor division and returns the largest whole number not greater than the result (7 // 2 == 3).
decimal or fraction modules? +Use decimal for exact base-10 arithmetic like currency, and fractions for exact rational numbers. Both avoid the binary rounding issues of float at the cost of speed.

