Python programming exercises: practice from beginner to intermediate

What you will learn: loops, conditionals, factorials, square-root calculations, input handling and nested lists. Work through each exercise before examining a solution, and compare your output with the examples.

Python 3 update: Some older solutions below were originally formatted for Python 2. In current Python versions, use input() instead of input(), and use print(...). The corrected, runnable Python 3 examples appear after the original exercises.

Guided Python debugging and programming help Β· Project help Β· Related programming samples Β· Official Python tutorial.

Python programming exercises for all levels of knowledge:

Level Description

Level 1 Beginner means someone who has just heard about Python programming exercise. He can solve something using very basic classes and/or functions with an amount of one or two of them. Answers can be found in Python help or so with least try.
Level 2 Intermediate means someone already learning Python, but also coming from some very good programming background. He can solve something using very basic classes and/or functions with the amount of up to three of them. You can’t find answers in Python help or similar places.
Level 3 Advanced. He should use Python on a very good professional level knowing many language-specific algorithms and certain data structures and functions of it. He really should be able to solve all these tasks using quite demanding techniques from the programming point of view really and probably close to expert level on other languages as well.
Β 
2. Problem template
Β 
#—————————————-#
Question
Hints
Solution
Β 
3. Questions
Β 
#—————————————-#
Level 1

Level 1 of Python programming exercises

Question:
Develop a program which will find all special numbers which are divisible by 3 but are not a multiple of 7,
between 3000 and 4200 (both included).
The output should be done like a comma-separated sequence and in a single line.
Β 
Hints:
Consider use range(#begin, #end) method
Β 
Solution:
y=[]
for x in range(3000, 4201):
if (x%3==0) and (x%7!=0):
y.append(str(x))
Β 
print(‘,’.join(y))

Β 

Level 1
Β 
Question:
Construct a program that can calculate the factorial of a given number.
The output should be done like a comma-separated sequence and in a single line.
Assume the following input is entered into the program:
9
Then, the output should be:
362880
Β 
Hints for doing the Python programming exercises
When data is provided to the question, it should be acknowledged to be a console method of input.
Β 
Solution:
def factorial(z):
if z == 0:
return 1
return z * factorial(z – 1)
Β 
z=int(input())
print(factorial(z))

Β 

Level 2 of Python programming exercises

Β 
Question:
Invent a program that compiles and outputs the value according to the formula below:
D = Square root of [(2 * A * C)/B]
Following are the fixed values of A and B:
A is 50. B is 30.
C variable here values of which should be served as an input to your program in a sequence over coma.
Example
Let us assume the input sequence over coma below is given to the program:
10,15,18
The output of the program should be:
6,7,8
Β 
Hints:
If the output received is in decimal form, it should be rounded off to its nearest value (let’s say the output received is 37.0, then it should be printed as 37)
When data being provided to the question, it should be acknowledged to be a console method of input.
Β 
Solution:
#!/usr/bin/env python
import math
a=50
b=30
value = []
items=[y for y in input().split(‘,’)]
for c in items:
value.append(str(int(round(math.sqrt(2*a*float(c)/b)))))
Β 
print(‘,’.join(value))

Β 

Level 2

Level 2 of Python programming exercises
Question:
Develop a program that takes 2 digits, A, B as input and generates a two-dimensional array. The element value in the i-th row and jth column of this array should be i*j.
Note: i=0,1.., A-1; j=0,1,‘­B-1.
Example
Assume the following inputs were given to the program:
3,5
The output of the program will be:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
Β 
Hints:
Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form.
Β 
Solution:
input_str = input()
dimensions=[int(y) for y in input_str.split(‘,’)]
rowNumber=dimensions[0]
colNumber=dimensions[1]
multilist = [[0 for col in range(colNumber)] for row in range(rowNumber)]
Β 
for row in range(rowNumber):
for col in range(colNumber):
multilist[row][col]= row*col
Β 
print(multilist)

Β 

Corrected Python 3 solutions you can run

These solutions demonstrate Python 3 syntax and preserve the learning objectives of the original exercises. Code blocks below include correct indentation.

1. Numbers divisible by 3 but not by 7

numbers = [str(n) for n in range(3000, 4201) if n % 3 == 0 and n % 7 != 0]
print(",".join(numbers))

2. Factorial using a recursive function

def factorial(n):
    if n < 0:
        raise ValueError("Factorial requires a non-negative integer")
    if n == 0:
        return 1
    return n * factorial(n - 1)

print(factorial(9))  # 362880

3. Calculate square roots for comma-separated inputs

import math
A, B = 50, 30
values = [10, 15, 18]
result = [str(int(round(math.sqrt(2 * A * c / B)))) for c in values]
print(",".join(result))  # 6,7,8

4. Generate a two-dimensional multiplication array

rows, columns = 3, 5
matrix = [[row * col for col in range(columns)] for row in range(rows)]
print(matrix)
# [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]

Next: Review the functional programming languages guide, browse real learning samples, or request programming help and optional one-to-one tutoring.

Discuss a programming problem with a specialist β†’