简体   繁体   中英

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.

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.

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

Note that in Python, range(start, end) is [start, end); ie, your range includes every number from 0 to 49.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM