简体   繁体   English

如何打印范围内的数字但排除给定可整除数字的数字

[英]How to print numbers from range but exclude a number by a given divisible number

I need to print out the range between 0-50 with the exclusion of numbers divisible by 7.我需要打印出 0-50 之间的范围,不包括可被 7 整除的数字。

for x in range(0,50):#for loop range beginning 0-50
    if x % 7 == 0:
        print(x)

Just check if the number is divisible by using the modulo operator % .只需使用模运算符%检查数字是否可整除。

The expression x % 7 will return the remainder of x divided by 7. If the remainder is not zero, then x is not divisible by 7, so print the number.表达式x % 7将返回 x 除以 7 的余数。如果余数为零,则 x不能被 7 整除,因此打印该数字。

for x in range(0, 50):
    if x % 7 != 0:
        print(x)

Note that in Python, range(start, end) is [start, end);注意在Python中,range(start, end)是[start, end]; ie, your range includes every number from 0 to 49.即,您的范围包括从 0 到 49 的每个数字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 0 到给定数字范围内的所有数字的总和,这些数字可以被 7 整除 - Sum of all numbers in range from 0 to a given number that are divisible with 7 如何从范围生成器中打印出不能被给定列表中的任何数字整除的数字 - How to print out numbers from a range generator that are not divisible by any numbers in a given list 如何找到可被给定范围内的数字整除的数字? - how to find number that's divisible by a number in a given range? 回文数可被任何给定数整除 - Palindromic numbers divisible by any given number 如何从给定范围计算一个不能被数组的任何元素整除的数字? - How do I count from a given range a number that is not divisible by any element of the array? 给定前 10 个数字的范围,从开始数字迭代到结束数字并打印当前数字和前一个数字的总和 - Given a range of first 10 numbers, Iterate from start number to the end number and print the sum of the current number and previous number 打印所有可被 7 整除并包含 7 从 0 到 100 的数字 - print all number divisible by 7 and contain 7 from 0 to 100 如何打印0到100之间可被3和5整除的数字? - How to print numbers from 0 to 100 that are divisible by 3 and also 5? 高效的算法,可找到可被数字整除的数的计数,而该数没有范围内的余数 - Efficient algorithm to find the count of numbers that are divisible by a number without a remainder in a range 如何计算python中多个案例(C)在给定范围(A到B)内可被N整除的数字 - how to count the number divisible by N in given range (A to B) for multiple cases (C) in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM