Chapter 2 Exercises

Practice exercises for loops and iteration in Python.

Ali Berro

By Ali Berro

10 min read Section
From: Python Fundamentals: From Zero to Hero

Table of Contents

Exercises

Exercise 1: Countdown from n to 1

Write a program that reads a positive integer n from the user and prints a countdown from n to 1, each on a new line.

Countdown

Checks: 0 times
Answer:
n = int(input())
for i in range(n, 0, -1):
print(i)

Exercise 2: Print characters at even positions

Write a program that reads a string from the user and prints all characters at even positions (indices 0, 2, 4, …), each on a new line.

Even Position Characters

Checks: 0 times
Answer:
text = input()
for i in range(0, len(text), 2):
print(text[i])

Exercise 3: Largest digit in a number

Write a program that reads a positive integer and finds and prints the largest digit in that number.

Largest Digit

Checks: 0 times
Answer:
num = int(input())
largest = 0
while num > 0:
digit = num % 10
if digit > largest:
largest = digit
num //= 10
print(largest)

Exercise 4: Sum of digits of a number

Write a program that reads a positive integer and calculates and prints the sum of all its digits.

Sum of Digits

Checks: 0 times
Answer:
num = int(input())
sum_digits = 0
while num > 0:
digit = num % 10
sum_digits += digit
num //= 10
print(sum_digits)

Exercise 5: Convert decimal to binary

Write a program that reads a positive integer and converts it to binary representation (as a string) without using built-in conversion functions.

Decimal to Binary

Checks: 0 times
Answer:
n = int(input())
binary = ""
if n == 0:
binary = "0"
else:
while n != 0:
r = n % 2
binary = str(r) + binary
n = n // 2
print(binary)

Exercise 6: Sum of all factors of a number

Write a program that reads a positive integer n and calculates and prints the sum of all its factors (divisors).

Sum of Factors

Checks: 0 times
Answer:
n = int(input())
sum_factors = 0
for i in range(1, n + 1):
if n % i == 0:
sum_factors += i
print(sum_factors)

Exercise 7: Check if number is palindrome

Write a program that reads a positive integer and checks if it is a palindrome (reads the same forwards and backwards). Print "Palindrome" if it is, otherwise print "Not Palindrome".

Palindrome Check

Checks: 0 times
Answer:
num = int(input())
original = num
reversed_num = 0
while num > 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
if original == reversed_num:
print("Palindrome")
else:
print("Not Palindrome")

Exercise 8: Calculate harmonic series

Write a program that reads a positive integer n and calculates the harmonic series Hn=1+12+13+...+1nH_n = 1 + \frac{1}{2} + \frac{1}{3} + ... + \frac{1}{n}. Print the result rounded to 6 decimal places.

Harmonic Series

Checks: 0 times
Answer:
n = int(input())
harmonic = 0.0
for i in range(1, n + 1):
harmonic += 1.0 / i
print(round(harmonic, 6))

Exercise 9: Calculate Un series

Write a program that reads a positive integer n and calculates Un=i=1ni2i+1U_n = \sum_{i=1}^{n} \frac{i^2}{i + 1}. Print the result rounded to 6 decimal places.

Un Series

Checks: 0 times
Answer:
n = int(input())
total = 0.0
for i in range(1, n + 1):
total += (i * i) / (i + 1)
print(round(total, 6))

Exercise 10: Equilateral triangle pattern

Write a program that reads a positive integer n and prints an equilateral triangle pattern with n rows using asterisks.

Example: If n = 4, the output should be:

*
***
*****
*******

Equilateral Triangle

Checks: 0 times
Answer:
n = int(input())
for i in range(1, n + 1):
spaces = n - i
stars = 2 * i - 1
print(" " * spaces + "*" * stars)

Exercise 11: Diamond pattern

Write a program that reads a positive integer n and prints a diamond pattern with n rows in the upper half.

Example: If n = 4, the output should be:

*
***
*****
*******
*****
***
*

Diamond Pattern

Checks: 0 times
Answer:
n = int(input())
for i in range(1, n + 1):
spaces = n - i
stars = 2 * i - 1
print(" " * spaces + "*" * stars)
for i in range(n - 1, 0, -1):
spaces = n - i
stars = 2 * i - 1
print(" " * spaces + "*" * stars)

Exercise 12: FizzBuzz

Write a program that prints numbers from 1 to n (read from input), but:

  • For multiples of 3, print "Fizz" instead of the number
  • For multiples of 5, print "Buzz" instead of the number
  • For multiples of both 3 and 5, print "FizzBuzz" instead of the number
  • Otherwise, print the number

Each output should be on a new line.

FizzBuzz

Checks: 0 times
Answer:
n = int(input())
for i in range(1, n + 1):
if i % 15 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)

Exercise 13: Find missing number

Write a program that reads an integer n, then reads n unique numbers (all between 0 and n inclusive, except one missing number). Find and print the missing number.

Example: If n = 5 and the numbers are 0, 1, 3, 4, 5, then 2 is missing.

Missing Number

Checks: 0 times
Answer:
n = int(input())
total_sum = n * (n + 1) // 2
actual_sum = 0
for i in range(n):
num = int(input())
actual_sum += num
missing = total_sum - actual_sum
print(missing)

Exercise 14: Approximate exponential function

Write a program that reads two numbers x and n, and approximates exe^x using the Taylor series expansion:

1+x+x22×1+x33×2×1+...+xnfactorial(n)1 + x + \frac{x^2}{2 \times 1} + \frac{x^3}{3 \times 2 \times 1} + ... + \frac{x^n}{factorial(n)}

where factorial(n) is the product of all positive integers from 1 to n. Print the result rounded to 6 decimal places.

Exponential Approximation

Checks: 0 times
Answer:
x = float(input())
n = int(input())
result = 1.0
factorial = 1
power = 1.0
for i in range(1, n + 1):
factorial *= i
power *= x
result += power / factorial
print(round(result, 6))

Exercise 15: Caesar cipher

Write a program that reads a string message and an integer n (shift position), then shifts each letter in the message by n positions in the alphabet. Only shift letters (a-z, A-Z), leave other characters unchanged. Assume wrapping around (z shifts to a, Z shifts to A).

Caesar Cipher

Checks: 0 times
Answer:
message = input()
n = int(input())
result = ""
for char in message:
if 'a' <= char <= 'z':
shifted = ord(char) + n
if shifted > ord('z'):
shifted -= 26
result += chr(shifted)
elif 'A' <= char <= 'Z':
shifted = ord(char) + n
if shifted > ord('Z'):
shifted -= 26
result += chr(shifted)
else:
result += char
print(result)

Course Progress

Section 19 of 61

Back to Course