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

Python basic tutorial

Python flow control

Python Functions

Python Data Types

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python program to find the factors of a number

Python example大全

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

# Find the factors of a number using a Python program
# 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, please change the value of num.

In this program, the number whose factors are found will be stored in num, which will be 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 can be completely divided by i, it is a factor of x.

Python example大全