Python Loop Assignment: Rakeb Tadele
Python Loop Assignment: Rakeb Tadele
Rakeb Tadele
Page 1 of 6
Python Loop Assignment
1. Write a program that prints numbers from 1 to 10 using a for loop.
for i in range(1, 11):
print(i)
2. Modify the above program to print numbers in reverse order (10 to 1).
for i in range(10, 0, -1):
print(i)
4. Write a program that calculates the sum of numbers from 1 to 50 using a for loop.
total = 0
for i in range(1, 51):
total += i
print(total)
Page 2 of 6
Python Loop Assignment
6. Write a Python program that prints the multiplication table of any number provided by the user.
num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {i} = {num*i}")
7. Write a Python program that contains names of students, iterate through a list of names and print each name.
students = ["Alice", "Bob", "Charlie"]
for name in students:
print(name)
9. Write a program that prints all prime numbers between 1 and 100 using for loops.
for num in range(2, 101):
for i in range(2, int(num**0.5)+1):
if num % i == 0:
break
else:
print(num)
10. Write a Python program to generate Fibonacci series up to `n` terms using a for loop.
n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a)
a, b = b, a + b
Page 3 of 6
Python Loop Assignment
11. Write a Python program to convert a list of numbers into their squares using a loop.
nums = [1, 2, 3, 4, 5]
squares = []
for num in nums:
squares.append(num**2)
print(squares)
12. Write a Python program that prints numbers from 1 to 10 using a while loop.
i = 1
while i <= 10:
print(i)
i += 1
13. Write a Python program to print all odd numbers between 1 and 50 using a while loop.
i = 1
while i <= 50:
if i % 2 != 0:
print(i)
i += 1
14. Write a Python program to calculate the sum of digits in a number using a while loop.
num = int(input("Enter a number: "))
sum_digits = 0
while num:
sum_digits += num % 10
num //= 10
print(sum_digits)
15. Write a Python program that keep asking the user for input until they enter "exit".
while True:
inp = input("Enter something (type 'exit' to quit): ")
if inp.lower() == 'exit':
break
Page 4 of 6
Python Loop Assignment
16. Write a Python program to find the factorial of a number using a while loop.
num = int(input("Enter a number: "))
fact = 1
while num > 1:
fact *= num
num -= 1
print(fact)
17. Write a Python program that ask the user to input a password, and keep asking until they enter the correct
one.
correct_password = "python123"
while True:
pwd = input("Enter password: ")
if pwd == correct_password:
print("Access granted")
break
18. Write a while loop that prints squares of numbers until the squared value exceeds 1000.
i = 1
while i**2 <= 1000:
print(i**2)
i += 1
19. Implement a basic banking system where the user enters deposits and withdrawals until they decide to quit.
balance = 0
while True:
action = input("Deposit/Withdraw/Quit: ").lower()
if action == "quit":
break
amount = float(input("Amount: "))
if action == "deposit":
balance += amount
elif action == "withdraw":
balance -= amount
print("Final Balance:", balance)
20. Write a Python program to simulate a simple login system where a user gets three chances to enter the
correct password.
password = "pass123"
for _ in range(3):
if input("Enter password: ") == password:
print("Login successful")
break
else:
print("Access denied")
Page 5 of 6