For Loop in Python
The for
loop is used to iterate over a sequence (like a list, tuple, or string) or a range of numbers. It’s one of the fundamental tools for iteration in Python. Here are some beginner-friendly exercises to help you practice using for
loops.
Practice Problems
Problems Index
- Print numbers from 1 to 10 using a
for
loop. - Input a number and print its multiplication table.
- Input a string and print each character on a new line.
- Input a list of numbers and calculate their sum.
- Print all even numbers between 1 and 50.
- Input a number and calculate its factorial.
- Print a pattern of stars for
n
rows (e.g., a right-angled triangle). - Input a number and check if it’s prime using a
for
loop. - Print all numbers divisible by 3 and 5 in the range 1 to 100.
- Input a string and count the number of vowels in it.
Try It Yourself
Now that you’ve reviewed the problems, practice them below using the code runner!
1. Print Numbers
Pyground
Print numbers from 1 to 10 using a for loop.
Expected Output:
1 2 3 4 5 6 7 8 9 10
Output:
2. Multiplication Table
Pyground
Input a number and print its multiplication table.
Output:
3. Print Characters
Pyground
Input a string and print each character on a new line.
Output:
4. Sum of Numbers
Pyground
Input a list of numbers and calculate their sum.
Output:
5. Even Numbers
Pyground
Print all even numbers between 1 and 50.
Expected Output:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Output:
6. Calculate Factorial
Pyground
Input a number and calculate its factorial.
Output:
7. Star Pattern
Pyground
Print a pattern of stars for n rows (e.g., a right-angled triangle).
Output:
8. Prime Number Check
Pyground
Input a number and check if it's prime using a for loop.
Output:
9. Divisible by 3 and 5
Pyground
Print all numbers divisible by 3 and 5 in the range 1 to 100.
Expected Output:
15 30 45 60 75 90