python 5的倍数
Sometimes, we need to find the sum of all integers or numbers that are completely divisible by 3 and 5 up to thousands, since thousands are a too large number that’s why it becomes difficult for us. So, here we will do it in Python programming language that solves the problem in just a few seconds. To solve this problem, we will use the range function. So, before going to find the sum we will learn a little bit about range function.
有时,我们需要找到可以被3和5整除的所有整数或数字的总和,直到数千,因为数千是一个太大的数字,这就是为什么我们很难做到这一点。 因此,在这里,我们将使用Python编程语言来完成此任务,并在几秒钟内解决问题。 为了解决这个问题,我们将使用范围函数。 因此,在找到总和之前,我们将学习一些有关范围函数的知识。
What is the range function in Python?
Python中的范围函数是什么?
The range() is a built-in function available in Python. In simple terms, the range allows them to generate a series of numbers within a given interval. This function only works with the integers i.e. whole numbers.
range()是Python中可用的内置函数。 简而言之,该范围允许它们在给定间隔内生成一系列数字。 此函数仅适用于整数,即整数。
Syntax of range() function:
range()函数的语法:
range(start, stop, step)
It takes three arguments to start, stop, and step and it depends on users choose how they want to generate a sequence of numbers? By default range() function takes steps of 1.
它需要三个参数来开始 , 停止和步进 ,并且取决于用户选择如何生成数字序列? 默认情况下, range()函数采用步骤1。
Program:
程序:
# initialize the value of n
n=1000
# initialize value of s is zero.
s=0
# checking the number is divisible by 3 or 5
# and find their sum
for k in range(1,n+1):
if k%3==0 or k%5==0: #checking condition
s+=k
# printing the result
print('The sum of the number:',s)
Output
输出量
The sum of the number: 234168
翻译自: https://www.includehelp.com/python/find-the-sum-of-all-numbers-below-1000-which-are-multiples-of-3-or-5.aspx
python 5的倍数