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 * fact(z – 1) |
z=int(raw_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 raw_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 = raw_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 multi-list |
I wanted to provide here examples for level 3 as well with the solutions but I assume that people should really get going with the first two levels we already listed few examples of exercises to get busy, rest will come later as usual.