All 17 scripts in ~/PycharmProjects/Devel/Python/2.Numbers have been audited, refactored, modernized, and covered with automated unit tests.
Detailed documentation and side-by-side version comparison diffs have been saved to docs.md.
-
Python 3.3 – 3.13 Standardized Code:
- Standardized
print()function syntax across all scripts (from __future__ import print_function). - Standardized true float division
/vs explicit integer floor division//(from __future__ import division). - Extracted pure, reusable functions (
calculate(),decimal_operations(),is_prime_v(),is_prime_number(),shift_multiply(),calculate_average(), etc.) so logic can be imported directly into test runners without side-effects.
- Standardized
-
Fixed Bugs & Cross-Version Issues:
primnumber.py: Refactored logic to fix broken return statements (return print('No')), zero-division error in step loop (n / i == 0), and addedis_prime_number(num)pure function.multipl.py: Extracted modular bitwise multiplication functionsshift_multiply()andshift_sequence(), demonstrating left-shift (<<=) power-of-two multiplication.get_average.py: Refactored withcalculate_average()andfrom __future__ import divisionfor true float division across Py2.7 and Py3.3-3.13.calculator.py: Fixed a critical default argument bug wherenum1=int(input(...))evaluated input at module load time.is_prime_v2.py/is_prime_v3.py: Wrappedmath.floor()inint()(int(math.floor(math.sqrt(num)))) to preventTypeErrorwhen passing floating-point range limits in legacy Python versions.f_num.py&nearst_number.py: Converted unescaped f-strings tostr.format()where universal version support was needed, while documenting f-string evolution from 3.6 to 3.13.random_num.py: Wrapped ranges inlist(range(...))to handle sequence sampling safely across version iterators.
-
Added Comprehensive Unit Tests (
test_numbers.py):- Created test_numbers.py using Python's standard
unittestframework. Covers all 17 modules with test cases testing assertions, edge cases, exceptions (ZeroDivisionError,ValueError), boundary conditions, bitwise shifts, averages, and prime limits.
- Created test_numbers.py using Python's standard
| File | Primary Changes | Python Version Impact |
|---|---|---|
bin_hex_oct_num.py |
Extracted convert_number_bases(x) & to_complex(y)
|
Handles Py3 0o vs Py2 0 octal prefixes |
calculator.py |
Fixed input() default argument execution; extracted calculate(num1, op, num2)
|
Safe headless CLI & unit testing |
complex_num.py |
Extracted get_complex_details(val) returning real & imag components |
Standardized complex tuple inspection |
deciamel.py |
Extracted decimal_operations(num1, num2) with get_input compatibility |
Fixed input vs raw_input security |
example.py |
Extracted parse_base_string(val_str, base)
|
Positional radix parsing (Base 2, 3, 4) |
f_num.py |
Replaced Py3.6+ f-strings with str.format(); extracted add_constant_to_list()
|
Grants universal Py2.7 - Py3.13 compatibility |
floats.py |
Added float_operations() and inspect_special_floats()
|
Tests IEEE 754 nan, inf, -inf, 3e8
|
get_average.py |
Extracted calculate_average() and run_average_demo()
|
Ensures true division float return across Py2.7 & Py3 |
inte.py |
Extracted integer_operations(num1, num2)
|
Documents Py3 int arbitrary precision |
is_prime_v.py |
Added benchmark_prime_v()
|
|
is_prime_v2.py |
Wrapped math.floor() in int() cast for range bounds |
Fixed TypeError on float limits |
is_prime_v3.py |
Added int(math.floor(...)) cast & even number skip logic |
Optimized |
multipl.py |
Extracted shift_multiply() & shift_sequence()
|
Bitwise left-shift power-of-two multiplication |
nearst_number.py |
Extracted format_and_round(); added comma formatting "{:,}".format()
|
Documents Banker's Rounding vs Round-Half-Away |
none_bool.py |
Extracted evaluate_truthiness(); fixed typo (o os o is) |
Standardized truthiness testing for 15 types |
primnumber.py |
Fixed return statements & trial division modulo bug; added is_prime_number()
|
Clean |
random_num.py |
Extracted get_random_single() and get_random_sample()
|
Safe range sampling across Python 3.3-3.13 |
# --- PYTHON 2.7 ---
res = 5 / 2 # Truncates to 2 (int)
# --- PYTHON 3.3 - 3.13 ---
res = 5 / 2 # Always returns 2.5 (float)
floor_div = 5 // 2 # Returns 2 (int floor division)# --- PYTHON 2.7 ---
round(2.5) # Returns 3.0 (Rounds half away from zero)
# --- PYTHON 3.3 - 3.13 ---
round(2.5) # Returns 2 (Banker's Rounding: rounds to nearest EVEN integer)
round(3.5) # Returns 4# --- PYTHON 2.7 ---
print "Number:", 10 # Language keyword statement
# --- PYTHON 3.3 - 3.13 ---
print("Number:", 10, sep=" ", end="\n") # Built-in function with sep/end/file/flush-
Beginner Summary: Python handles whole numbers (
int), decimals (float), and complex numbers (complex). Modern Python 3 uses/for exact division (e.g. 5/2 = 2.5),//to chop off decimals (5//2 = 2), and%for remainders (5 % 2 = 1). Truthiness evaluates0,0.0,None, and[]asFalse, while non-zero numbers areTrue. -
Senior Summary: CPython 3 uses
PyLongObjectwith digit arrays for arbitrary-precision integers, eliminating integer overflow. Floats adhere to 64-bit double-precision IEEE 754 semantics. Python 3'sround()implements standard IEEE 754 Banker's Rounding (round-half-to-even) to minimize cumulative statistical bias. Prime checking algorithms progress from$O(N)$ to$O(\sqrt{N})$ and$O(\frac{\sqrt{N}}{2})$ with explicitint(math.floor(...))conversions to preserve step ranges across bytecodes.
Execute the test runner directly from the terminal:
python -m unittest discover -s ~/PycharmProjects/Devel/Python/2.Numbers -p "test_*.py"