Program 1
Print 1 to N
Use range with a for loop.
python
for i in range(1, 11):
print(i, end=" ")Output
1 2 3 4 5 6 7 8 9 10Topic 04 of 17
Iterate with for and while loops, break, continue, and else clauses.
Program 1
Use range with a for loop.
for i in range(1, 11):
print(i, end=" ")Output
1 2 3 4 5 6 7 8 9 10Program 2
Print the table of a number.
n = 7
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")Program 1
Sum the digits of a number using while.
n, total = 1729, 0
while n > 0:
total += n % 10
n //= 10
print("Sum:", total)Program 2
Nested loops to print shapes.
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))Program 1
Loop else runs only if break wasn't hit.
n = 29
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
print(n, "is composite")
break
else:
print(n, "is prime")