Python Integers & Floats: Visual Guide & Diagrams
This guide distills core concepts of numeric data in Python—covering types, operations, built-in functions,
comparisons, and casting—enhanced with ASCII-style illustrations for clarity.
1. Integer vs. Float
• Integer ( int ): whole numbers, no fractional part.
• Float ( float ): numbers with decimal component.
num = 3
print(type(num)) # <class 'int'>
num = 3.14
print(type(num)) # <class 'float'>
Diagram: Type Classification
3 → int
3.14 → float
2. Basic Arithmetic Operators
Operator Symbol Description Example
Addition + sum two values 3 + 2 = 5
Subtract - difference 3 - 2 = 1
Multiply * product 3 * 2 = 6
Divide / true division 3 / 2 = 1.5
Floor Div // integer division (floor) 3 // 2 = 1
Power ** exponentiation 3 ** 2 = 9
Modulo % remainder 3 % 2 = 1
Floor vs. True Division
1
True Division (/) Floor Division (//)
3 / 2 → 1.5 3 // 2 → 1
3. Modulo & Even/Odd Check
• % yields remainder of integer division.
• Commonly used to test parity: n % 2 == 0 → even, == 1 → odd.
Remainder Diagram
Dividend ÷ Divisor = Quotient with Remainder
5 ÷ 2 = 2 remainder 1
5 % 2 → 1
4. Order of Operations & Parentheses
Python respects PEMDAS (Parentheses, Exponents, Multiply/Divide, Add/Subtract). Use parentheses to
override default precedence.
print(3 * 2 + 1) # (3*2) + 1 = 7
print(3 * (2 + 1)) # 3 * (2+1) = 9
Operation Tree
Expression: 3 * (2 + 1)
*
/ \
3 ()
|
+
/ \
2 1
5. Increment & Augmented Assignment
• Standard increment:
2
num = 1
num = num + 1 # num → 2
• Shorthand with += , -= , *= , /= , etc.:
num = 1
num += 1 # num → 2
num *= 10 # num → 20
6. Built‑in Numeric Functions
Function Purpose Example
abs(x) absolute value abs(-3) → 3
round(x[, n]) round to n decimals round(3.75) → 4 , round(2.38,1) → 2.4
Rounding Behavior
round(3.75) → 4 # nearest int
round(2.38, 1) → 2.4 # one decimal place
7. Comparison Operators & Booleans
Comparisons return True or False :
Operator Meaning Example
== equal 3 == 2 → False
!= not equal 3 != 2 → True
> greater than 3 > 2 → True
< less than 3 < 2 → False
>= ≥ 3 >= 3 → True
<= ≤ 2 <= 3 → True
3
8. Casting & Type Conversion
Convert between numeric types (or from strings):
# String to int/float
a = "100"
b = "3.14"
i = int(a) # 100 (int)
f = float(b) # 3.14 (float)
# Numeric to string
s = str(123) # "123"
Casting Flow
"3.14" --float()--> 3.14
"100" --int()--> 100
Master these fundamentals to handle Python numeric data with confidence!