python 5的倍数_查找所有低于1000的数字的和,这是Python中3或5的倍数

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的倍数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值