Python Loops
Python program to print multiplication table
Description: Write a Python program to print the multiplication table of a number.
Rules and hints:
- Use a
forloop withrange(1, 11). - Print each line in the form
7 x 1 = 7.
Python program for multiplication table
num = 7
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
How it works
range(1, 11)gives the numbers 1 to 10. The end value 11 is not included.- In each round the f-string puts the values of
num,iand the product in the line.
Learn more in Python for loop.
Python program to find factorial of a number
Description: Write a Python program to find the factorial of a number using a loop.
Rules and hints:
- Factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120.
- Start with 1 and multiply by each number.
Python program for factorial using a loop
num = 5
factorial = 1
for i in range(1, num + 1):
factorial = factorial * i
print("Factorial of", num, "is", factorial)
Output:
Factorial of 5 is 120
How it works
factorialstarts with 1.- The loop multiplies it with 1, 2, 3, 4 and 5 one by one: 1, 2, 6, 24, 120.
- For 0, the loop does not run, and the answer stays 1, which is correct.
You can also solve it with a function that calls itself. See Python Functions (recursion).
Python program to print Fibonacci series
Description: Write a Python program to print the first 10 numbers of the Fibonacci series.
Rules and hints:
- Each number is the sum of the two numbers before it.
- The series starts with 0 and 1.
Python program for Fibonacci series
a, b = 0, 1
for _ in range(10):
print(a, end=" ")
a, b = b, a + b
Output:
0 1 1 2 3 5 8 13 21 34
How it works
aandbhold two neighbour numbers of the series.print(a, end=" ")prints on the same line with a space.a, b = b, a + bmoves to the next pair at once.- The loop variable
_means we do not use its value.
Python program to check prime number
Description: Write a Python program to check whether a number is prime.
Rules and hints:
- A prime number is greater than 1 and has no divisor other than 1 and itself.
- You only need to test divisors up to the square root of the number.
Python program to check a prime number
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
for n in (2, 15, 17, 1):
if is_prime(n):
print(n, "is prime")
else:
print(n, "is not prime")
Output:
2 is prime
15 is not prime
17 is prime
1 is not prime
How it works
- Numbers below 2 are not prime.
- For 15 the loop tests 2 and 3. Since
15 % 3 == 0, it returnsFalse. - For 17 the loop tests 2, 3 and 4. None divides it, so it is prime.
- As soon as one divisor is found,
return Falseends the function early.
Python program to print star pattern (right triangle)
Description: Write a Python program to print a right angled triangle pattern of stars with 5 rows.
Rules and hints:
- Row 1 has one star, row 2 has two stars, and so on.
- You can repeat a string with
*, like"*" * 3.
Python star pattern program
rows = 5
for i in range(1, rows + 1):
print("*" * i)
Output:
*
**
***
****
*****
How it works
- In round
iwe print the staritimes. print()adds a new line after each row.
Related: Python Strings.
Python program to find the sum of digits of a number
Description: Write a Python program to find the sum of the digits of a number.
Rules and hints:
- Use
% 10to get the last digit. - Use
// 10to remove the last digit.
Python program for the sum of digits
num = 1234
total = 0
while num > 0:
total += num % 10
num = num // 10
print("Sum of digits:", total)
Output:
Sum of digits: 10
How it works
1234 % 10is 4, and then1234 // 10is 123.- The loop continues with 123, 12 and 1, adding 3, 2 and 1.
- So the total is 4 + 3 + 2 + 1 = 10. The loop stops when the number becomes 0.
Learn more in Python While Loop.