Python programming exercises and solutions

In this post, we are going to share about python programming exercises and solutions which helps to python beginners and whose who are preparing for the python interviews.

We have curated this python programming exercises with solution for the beginners and advance level python programmers and we have divided python exercise and solution level wise.

LevelDescriptions
Level 1Beginner means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks.
Level 2Intermediate means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks.
Level 3Advanced. He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques.

Python programming exercises and solutions: Level 1

Question 1. Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, between 2000 and 3200 (both included).

The numbers obtained should be printed in a comma-separated sequence on a single line.

Solution:

Question 2. Write a program which can compute the factorial of a given numbers. The results should be printed in a comma-separated sequence on a single line.

Suppose the following input is supplied to the program: 8

Then, the output should be: 40320

Solution:

Question 3. With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.

Suppose the following input is supplied to the program: 8

Then, the output should be:

Solution:

Question 4. Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.

Suppose the following input is supplied to the program:
34, 67, 55, 33, 12, 98

Then, the output should be:
[’34’, ’67’, ’55’, ’33’, ’12’, ’98’]
(’34’, ’67’, ’55’, ’33’, ’12’, ’98’)

Solution:

Question 5. Define a class which has at least two methods:

getString: to get a string from console input
printString: to print the string in upper case.
Also please include simple test function to test the class methods.

Solution:

Question 6. Please write a program to print the running time of execution of “1+1” for 100 times.

Solution:

Question 7. Please write a program to shuffle and print the list [3,6,7,8].

Solution:

Python programming exercises and solutions: Level 2

Question 1. Write a program that calculates and prints the value according to the given formula:

Q = Square root of [(2 * C * D)/H]
Following are the fixed values of C and H:
C is 50. H is 30.
D is the variable whose values should be input to your program in a comma-separated sequence.
Example
Let us assume the following comma separated input sequence is given to the program:
100, 150, 180
The output of the program should be:
18, 22, 24

Solution:

Question 2. Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.
Note: i=0,1.., X-1; j=0,1,¡­Y-1.
Example
Suppose the following inputs are given to the program:
3, 5

Then, the output of the program should be:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]

Solutions:

Question 3. Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.
Suppose the following input is supplied to the program:
without,hello,bag,world

Then, the output should be:
bag,hello,without,world

Question 4. Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.
Suppose the following input is supplied to the program:
Hello world
Practice makes perfect

Then, the output should be:
HELLO WORLD
PRACTICE MAKES PERFECT

Solution:

Question 5. Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically.
Suppose the following input is supplied to the program:
hello world and practice makes perfect and hello world again

Then, the output should be:
again and hello makes perfect practice world

Solution:

Question 6. Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number.
The numbers obtained should be printed in a comma-separated sequence on a single line.

Solution:

Question 7. Write a program that accepts a sentence and calculate the number of letters and digits.
Suppose the following input is supplied to the program:
hello world! 123

Then, the output should be:
LETTERS 10
DIGITS 3

Solution:

Python programming exercises and solutions: Level 3

Question 1. A website requires the users to input username and password to register. Write a program to check the validity of password input by users.

Following are the criteria for checking the password:

  1. At least 1 letter between [a-z]
  2. At least 1 number between [0-9]
  3. At least 1 letter between [A-Z]
  4. At least 1 character from [$#@]
  5. Minimum length of transaction password: 6
  6. Maximum length of transaction password: 12

Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma.

Example

If the following passwords are given as input to the program:
[email protected],a F1#,2w3E*,2We3345

Then, the output of the program should be:
[email protected]

Solution:

Question 2. You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers. The tuples are input by console. The sort criteria is:

  1. Sort based on name;
  2. Then sort based on age;
  3.  Then sort by score.

The priority is that name > age > score.
If the following tuples are given as input to the program:

Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85

Then, the output of the program should be:
[(‘John’, ’20’, ’90’), (‘Jony’, ’17’, ’91’), (‘Jony’, ’17’, ’93’), (‘Json’, ’21’, ’85’), (‘Tom’, ’19’, ’80’)]

Solution:

Question 3. Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.

Solution: 

Question 4. A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:
UP 5
DOWN 3
LEFT 3
RIGHT 2
¡­
The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer.

Example:

If the following tuples are given as input to the program:
UP 5
DOWN 3
LEFT 3
RIGHT 2

Then, the output of the program should be:
2

Solution: 

Question 5. Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically.

Suppose the following input is supplied to the program:
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.

Then, the output should be:
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1

Solution:

Conclusion

We have curated this tutorial from various online and offline source to help the python developers from beginners to expert level. Do like and share this python programming exercise and solutions to friends on Facebook and Twitter. Credit go to Jeffrey Hu for sharing this guide here.

Recommended Posts:


Editorial Staff

Editorial Staff at FreeWebMentor is a team of professional developers leads by Prem Tiwari

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Solved Programs