'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)


Solution 1:[1]

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); i.e., your range includes every number from 0 to 49.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1