Python, a versatile and beginner-friendly programming language, offers numerous ways to tackle problems related to numbers and arithmetic operations. One common task involves identifying numbers that meet specific criteria, such as being divisible by another number. In this context, let’s explore how to find and output all numbers within the range of 1 to 100 that are divisible by 7.
Understanding divisibility is fundamental. A number a
is divisible by another number b
if the remainder of a
divided by b
is zero. In Python, we can use the modulo operator %
to check for divisibility. The modulo operator returns the remainder of a division operation. If a % b == 0
, then a
is divisible by b
.
Here’s a simple Python script to find and print all numbers between 1 and 100 that are divisible by 7:
pythonCopy Code# Loop through numbers from 1 to 100
for number in range(1, 101):
# Check if the number is divisible by 7
if number % 7 == 0:
print(number)
This script iterates through each number in the specified range using a for
loop. For each number, it checks if the number is divisible by 7 using the modulo operator. If the condition is true, it prints the number.
This example demonstrates basic arithmetic and control flow in Python, making it an excellent starting point for those learning the language. It highlights how Python can be used to solve mathematical problems and perform operations on numbers efficiently.
[tags]
Python, Divisibility, Arithmetic Operations, Modulo Operator, Programming Basics, Control Flow