English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced Python knowledge

Python reference manual

Python program to find a number's factors

Complete Python examples

In this program, you will learn to use a for loop to find the factors of a number.

To understand this example, you should understand the followingPython programmingTopic:

Source code

# Use Python program to find a number's factors
# This function calculates the factors of the passed parameter
def print_factors(x):
   print(x, "'s factors are:")
   for i in range(1, x + 1)
       if x % i == 0:
           print(i)
num = 320
print_factors(num)

Output result

32The factors of 0 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320

Note:To find the factors of another number, change the value of num.

In this program, the number whose factors are found is stored in num, which is passed to the print_factors() function. This value is assigned to the variable x in print_factors().

In this function, we use a for loop to iterate from i equals x, if x is completely divisible by i, it is a factor of x.

Complete Python examples