If you want every number that divides another one with nothing left over, you’re looking for the factors of a number in Python terms. One check finds them: n % i == 0.
Keep i when the remainder is zero, and you’ve got your list.
In this tutorial, we’ll walk through four ways to get the factors of a number in Python: a for loop, a list comprehension, a faster square-root function and prime factorization, all tested on Python 3.12.5.
The Sample Numbers We’ll Factor
Every example imports the same numbers. To set it up:
- In VS Code, create a file named
sample.py. - Paste the code below and save it.
- Save each example in the same folder and run it in the VS Code terminal.
# Cookies a Columbus, Ohio bakery packs into equal boxes
COOKIES = 120
TEST_NUMBERS = [120, 97, 36, 1]
The code also runs in a Jupyter notebook. 120 is the cookie count. 97 is prime, 36 is a perfect square, and 1 has only itself as a factor.
Method 1: Python Program to Print Factors of a Number with a for Loop
Python classes usually ask for this one: it tests every number from 1 to n. Save this as factors_loop.py and run python factors_loop.py in Command Prompt:
from sample import COOKIES
def print_factors(n):
factors = []
for i in range(1, n + 1):
if n % i == 0:
factors.append(i)
return factors
print(COOKIES, "->", print_factors(COOKIES))
n = int(input("Enter a whole number: "))
print(n, "->", print_factors(n))
Output:
120 -> [1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120]
Enter a whole number: 84
84 -> [1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84]

How does this code work?
The Python for loop walks range(1, n + 1), so 120 itself is included. 120 % 8 is 0, so 8 is kept; 120 % 7 isn’t.
Method 2: Find All Factors of a Number with a List Comprehension
The same test fits on one line. Same sample, all four numbers. Save this as factors_comp.py and run it in the VS Code terminal:
from sample import TEST_NUMBERS
for n in TEST_NUMBERS:
factors = [i for i in range(1, n + 1) if n % i == 0]
print(f"{n:>4}: {factors}")
Output:
120: [1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120]
97: [1, 97]
36: [1, 2, 3, 4, 6, 9, 12, 18, 36]
1: [1]

How does this code work?
The Python list comprehension keeps each i where the remainder is 0. It reads cleanly, but it still checks every number up to n.
Method 3: A Faster get_factors() Function with the Square Root Trick
Factors come in pairs (8 x 15 = 120), so you don’t need to go past the square root. Save this as factors_sqrt.py and run it in the VS Code terminal:
import math
def get_factors(n):
if n < 1:
raise ValueError("n must be a positive integer")
small, large = [], []
for i in range(1, math.isqrt(n) + 1):
if n % i == 0:
small.append(i)
if i != n // i:
large.append(n // i)
return small + large[::-1]
print(get_factors(120))
print(get_factors(36))
big = 9_699_690
print(f"{big:,}: {len(get_factors(big))} factors")
print("loop checks:", big, "| sqrt checks:", math.isqrt(big))
Output:
[1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120]
[1, 2, 3, 4, 6, 9, 12, 18, 36]
9,699,690: 256 factors
loop checks: 9699690 | sqrt checks: 3114

How does this code work?
math.isqrt(120) is 10, so the loop stops at 10. Each hit adds i and its partner n // i.
The i != n // i check keeps 6 from showing twice for 36. See the square root in Python for isqrt() vs sqrt().
Important: every integer divides 0, so get_factors() raises ValueError for 0 and negatives instead of looping.
How Do You Factor a Number in Python into Prime Factors?
Prime factors aren’t the same thing: 120 = 2 x 2 x 2 x 3 x 5. Save this as factors_prime.py and run it in the VS Code terminal:
def prime_factors(n):
result, d = [], 2
while d * d <= n:
while n % d == 0:
result.append(d)
n //= d
d += 1
if n > 1:
result.append(n)
return result
print(120, "=", prime_factors(120))
print(600851475143, "=", prime_factors(600851475143))
Output:
120 = [2, 2, 2, 3, 5]
600851475143 = [71, 839, 1471, 6857]

How does this code work?
Each divisor d is divided out as many times as it fits, so only primes get through. Whatever is left above 1 is prime. To test one number, use my Python prime number check.
What to Watch for When Finding Factors in Python
- Negatives: pass
abs(n), orrange()returns an empty list. - Pick: I’d use
get_factors()for big numbers; for factors two numbers share, use GCD in Python. - Perfect numbers: the factors minus n add up to n; see perfect number in Python.
A few more Python number programs you may like:
Factors of a Number in Python: Common Questions
How do I get the factors of a number in Python?
Loop i from 1 to n and keep it when n % i == 0.
What is the fastest way to find all factors of a number in Python?
Loop up to math.isqrt(n) and add both i and n // i.
What is the difference between factors and prime factors?
Factors of 12 are 1, 2, 3, 4, 6, 12; its prime factors are 2, 2 and 3.
Is 1 a factor of every number?
Yes. 1 and the number itself are factors of every positive integer.
How do I count how many factors a number has?
Use len(get_factors(n)); 120 has 16.
Where can I read about math.isqrt()?
See math.isqrt() in the Python docs.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn