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
forloop. - 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
nrows (e.g., a right-angled triangle). - Input a number and check if it’s prime using a
forloop. - 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
PygroundTry It Out
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
PygroundTry It Out
Input a number and print its multiplication table.
Output:
3. Print Characters
PygroundTry It Out
Input a string and print each character on a new line.
Output:
4. Sum of Numbers
PygroundTry It Out
Input a list of numbers and calculate their sum.
Output:
5. Even Numbers
PygroundTry It Out
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
PygroundTry It Out
Input a number and calculate its factorial.
Output:
7. Star Pattern
PygroundTry It Out
Print a pattern of stars for n rows (e.g., a right-angled triangle).
Output:
8. Prime Number Check
PygroundTry It Out
Input a number and check if it's prime using a for loop.
Output:
9. Divisible by 3 and 5
PygroundTry It Out
Print all numbers divisible by 3 and 5 in the range 1 to 100.
Expected Output:
15 30 45 60 75 90
Output:
10. Count Vowels
PygroundTry It Out
Input a string and count the number of vowels in it.
Output:
Last updated on