# 50 Python For Loop Examples
# 1. Print numbers from 1 to 5
for i in range(1, 6):
print(i)
# 2. Print even numbers from 2 to 10
for i in range(2, 11, 2):
print(i)
# 3. Print odd numbers from 1 to 9
for i in range(1, 10, 2):
print(i)
# 4. Print squares of numbers 1–5
for i in range(1, 6):
print(i**2)
# 5. Print elements of a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# 6. Print characters of a string
for ch in "hello":
print(ch)
# 7. Sum of first 5 natural numbers
total = 0
for i in range(1, 6):
total += i
print(total)
# 8. Print multiplication table of 3
for i in range(1, 11):
print(f"3 x {i} = {3*i}")
# 9. Print list with index
colors = ["red", "green", "blue"]
for index, color in enumerate(colors):
print(index, color)
# 10. Print elements of a tuple
numbers = (10, 20, 30)
for n in numbers:
print(n)
# 11. Reverse a string using for loop
word = "python"
rev = ""
for ch in word:
rev = ch + rev
print(rev)
# 12. Find factorial of 5
fact = 1
for i in range(1, 6):
fact *= i
print(fact)
# 13. Print elements of dictionary
student = {"name": "Ali", "age": 18, "grade": "A"}
for key in student:
print(key, ":", student[key])
# 14. Print numbers from 10 down to 1
for i in range(10, 0, -1):
print(i)
# 15. Print only vowels from a string
text = "education"
for ch in text:
if ch in "aeiou":
print(ch)
# 16. Count even numbers in list
nums = [1, 2, 3, 4, 5, 6]
count = 0
for n in nums:
if n % 2 == 0:
count += 1
print(count)
# 17. Print pattern of stars
for i in range(1, 6):
print("*" * i)
# 18. Print pattern in reverse
for i in range(5, 0, -1):
print("*" * i)
# 19. Print each word in a sentence
sentence = "Python is easy"
for word in [Link]():
print(word)
# 20. Print numbers divisible by 3
for i in range(1, 21):
if i % 3 == 0:
print(i)
# 21. Find largest number in list
nums = [4, 9, 2, 7, 5]
largest = nums[0]
for n in nums:
if n > largest:
largest = n
print(largest)
# 22. Calculate average of list
nums = [10, 20, 30, 40]
total = 0
for n in nums:
total += n
print(total / len(nums))
# 23. Print Fibonacci sequence (first 7 terms)
a, b = 0, 1
for i in range(7):
print(a)
a, b = b, a + b
# 24. Print elements greater than 5
nums = [2, 7, 4, 9, 1, 6]
for n in nums:
if n > 5:
print(n)
# 25. Convert list of strings to uppercase
words = ["cat", "dog", "fish"]
for w in words:
print([Link]())
# 26. Print all combinations of 2 lists
colors = ["red", "green"]
objects = ["pen", "car"]
for c in colors:
for o in objects:
print(c, o)
# 27. Print diagonal pattern
for i in range(5):
print(" " * i + "*")
# 28. Count digits in a string
text = "abc123xyz45"
count = 0
for ch in text:
if [Link]():
count += 1
print(count)
# 29. Print reverse of list
nums = [1, 2, 3, 4]
for i in range(len(nums)-1, -1, -1):
print(nums[i])
# 30. Multiply all elements of list
nums = [2, 3, 4]
result = 1
for n in nums:
result *= n
print(result)
# 31. Print first 10 even numbers
for i in range(2, 21, 2):
print(i)
# 32. Print ASCII value of characters
for ch in "ABC":
print(ch, "=", ord(ch))
# 33. Print elements using range and index
nums = [5, 10, 15]
for i in range(len(nums)):
print("Index", i, "=", nums[i])
# 34. Count words in a sentence
sentence = "Python is powerful and simple"
count = 0
for word in [Link]():
count += 1
print(count)
# 35. Print a list of prime numbers (1–20)
for n in range(2, 21):
for i in range(2, n):
if n % i == 0:
break
else:
print(n)
# 36. Print pattern of numbers
for i in range(1, 6):
for j in range(1, i + 1):
print(j, end=" ")
print()
# 37. Print all keys of dictionary
info = {"name": "Sara", "age": 22, "city": "Lahore"}
for key in [Link]():
print(key)
# 38. Print all values of dictionary
for value in [Link]():
print(value)
# 39. Convert list of numbers to strings
nums = [1, 2, 3]
for n in nums:
print(str(n))
# 40. Print numbers skipping 3
for i in range(1, 10):
if i == 3:
continue
print(i)
# 41. Stop loop when number = 5
for i in range(1, 10):
if i == 5:
break
print(i)
# 42. Find sum of digits
num = 1234
s=0
for d in str(num):
s += int(d)
print(s)
# 43. Print multiplication tables 2–4
for i in range(2, 5):
for j in range(1, 11):
print(f"{i} x {j} = {i*j}")
print()
# 44. Print list elements separated by commas
nums = [1, 2, 3, 4]
for n in nums:
print(n, end=", ")
print()
# 45. Count uppercase letters
text = "PyThOn"
count = 0
for ch in text:
if [Link]():
count += 1
print(count)
# 46. Find minimum in list
nums = [8, 3, 5, 1, 9]
min_num = nums[0]
for n in nums:
if n < min_num:
min_num = n
print(min_num)
# 47. Print pattern of numbers (reverse triangle)
for i in range(5, 0, -1):
for j in range(1, i + 1):
print(j, end=" ")
print()
# 48. Count occurrences of 'a' in string
text = "banana"
count = 0
for ch in text:
if ch == 'a':
count += 1
print(count)
# 49. Print sum of even numbers from 1–10
s=0
for i in range(1, 11):
if i % 2 == 0:
s += i
print(s)
# 50. Nested loop to print coordinates
for x in range(3):
for y in range(3):
print(f"({x},{y})")