Python Workbook: Exercises For
Beginners With Solutions
Table Of Contents
List Data Type:
Write a Python program to find the largest number from a list.
Write a Python program to find the smallest number in a list.
Write a Python program to multiply all the items in a list.
Write a Python program to remove duplicates from a list.
Write a Python program to check if a list is empty or not.
Dictionary Data Type:
Write a Python program to add a key to a dictionary.
Write a Python program to remove a key from a dictionary.
Write a Python program to multiply all the items in a dictionary.
Write a Python program to sum all the items in a dictionary.
Write a Python program to iterate over dictionaries using for loop.
Write a Python program to merge two python dictionaries.
Python Basics:
Write a Python program to compute the area of triangle.
Write a Python program to display current date and time.
Write a Python program to find the volume of a sphere.
Write a Python program to compute the GCD.
Write a Python program to compute the LCM.
Write a Python program to convert feet and inches to centimeters.
Write a Python program to convert all units of time to seconds.
Write a Python program to convert the distance in feet to inches, yards and
miles.
Write a Python program to get an absolute file path.
Write a Python program to check whether a file exists.
Write a Python program to compute the distance between two points.
Write a Python program to sum all the items in a list.
Write a Python program to multiply all items in a list.
Write a Python program to get the largest number from a list.
Write a Python program to get the smallest number from a list .
Write a Python program to clone or copy a list.
Write a Python program to compute the difference between two lists.
Write a Python program to create a list with infinite elements.
Write a Python program to remove duplicates from a list.
Write a Python program to generate all permutations of a list.
Write a Python program to access the index of a list.
Write a Python program to convert a list of characters into a string
Write a Python program to find the index of an item in a specified list.
Write a Python program to get unique values from a list.
Write a Python program to get the frequency of elements in a list.
Write a Python program to count the number of elements in a list within a
specified range.
Write a Python program to check whether a list contains a sublist.
Write a Python program to find factorial of a non-negative integer.
Write a Python program of recursion list sum.
Write a Python program append a list to the second list.
Write a Python program to select an item randomly from a list.
Python Data Types exercises
List Data Type:
1. Write a Python program to find the largest number from a list.
Output:
2
2. Write a Python program to find the smallest number in a list.
Output:
-8
3. Write a Python program to multiply all the items in a list.
Output:
-16
4. Write a Python program to remove duplicates from a list.
Output:
{ 40, 10, 80, 50, 20, 60, 30 }
5. Write a Python program to check if a list is empty or not.
Output:
List is empty
Dictionary Data Type:
6. Write a Python program to add a key to a dictionary.
Output:
{ 0: 10, 1: 20 }
{ 0: 10, 1: 20, 2: 30 }
7. Write a Python program to remove a key from a dictionary.
Output:
{ ‘a’ : 1, ‘b’ : 2, ‘c’ : 3, ‘d’ : 4 }
{ ‘b’ : 2, ‘c’ : 3, ‘d’ : 4 }
8. Write a Python program to multiply all the items in a dictionary.
Output:
-1333800
9. Write a Python program to sum all the items in a dictionary.
Output:
293
10. Write a Python program to iterate over dictionaries
using for loop.
Output:
Red corresponds to 1
Green corresponds to 2
Blue corresponds to 3
11. Write a Python program to merge two python
dictionaries.
Output:
{ ‘x’ : 300, ‘y’ : 200, ‘a’ : 100, ‘b’ : 200}
Python Basics:
1. Write a Python program to compute the area of triangle.
Output:
Input the base: 10
Input the height: 20
Area: 100
2. Write a Python program to display current date and time.
Output:
Current date and time :
2021-05-23 20:29:15
3. Write a Python program to find the volume of a sphere.
Output:
The volume of the sphere is: 904.7786842338603
4. Write a Python program to compute the GCD.
Output:
1
2
5. Write a Python program to compute the LCM.
Output:
12
255
6. Write a Python program to convert feet and inches to centimeters.
Output:
Input your height:
Feet: 5
Inches: 3
Your height is : 160 cm
7. Write a Python program to convert all units of time to seconds.
Output:
Input days: 4
Input hours: 5
Input minutes: 20
Input seconds: 10
The amounts of seconds 364810
8. Write a Python program to convert the distance in feet to inches,
yards and miles.
Output:
Input the distance in feet: 100
The distance in inches is 1200 inches.
The distance in yards is 33.33 yards.
The distance in miles is 0.02 miles.
9. Write a Python program to get an absolute file path.
Output:
Absolute file path: /home/students/path_fname
10. Write a Python program to check whether a file
exists.
Output:
True
11. Write a Python program to compute the distance
between two points.
Output:
6.324555320336759
12. Write a Python program to sum all the items in a list.
Output:
-5
13. Write a Python program to multiply all items in a
list.
Output:
-16
14. Write a Python program to get the largest number
from a list.
Output:
2
15. Write a Python program to get the smallest number
from a list .
Output:
-8
16. Write a Python program to clone or copy a list.
Output:
[ 10, 22, 44, 23, 4 ]
[ 10, 22, 44, 23, 4 ]
17. Write a Python program to compute the difference
between two lists.
Output:
Color1-Color2: [ ‘red’, ‘white’, ‘orange’ ]
Color2-Color1: [ ‘black’, ‘yellow’, ]
18. Write a Python program to create a list with infinite
elements.
Output:
0
1
2
3
4
19. Write a Python program to remove duplicates from a
list.
Output:
{ 40, 10, 80, 50, 20, 60, 30 }
20. Write a Python program to generate all permutations
of a list.
Output:
[ (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3,1,2), (3, 2, 1)]
21. Write a Python program to access the index of a list.
Output:
05
1 15
2 35
38
4 98
22. Write a Python program to convert a list of
characters into a string.
Output:
abcd
23. Write a Python program to find the index of an item
in a specified list.
Output:
1
24. Write a Python program to get unique values from a
list.
Output:
Original list: [ 10, 20, 30, 40, 20, 50, 60, 40 ]
List of unique numbers : [ 40, 10, 50, 20, 60, 30 ]
25. Write a Python program to get the frequency of
elements in a list.
Output:
Original list: [ 10, 10, 10, 10, 20, 20, 20, 20, 40, 40, 50, 50, 30 ]
Frequency of the elements in the list: counter( { 10: 4, 20: 4, 40: 2, 50: 2,
30:1})
26. Write a Python program to count the number of elements
in a list within a specified range.
Output:
6
5
27. Write a Python program to check whether a list contains a
sublist.
Output:
True
False
28. Write a Python program to find factorial of a non-
negative integer.
Output:
120
29. Write a Python program of recursion list sum.
Output:
21
30. Write a Python program append a list to the second list.
Output:
[ 1, 2, 3, 0, ‘Red’, ‘Green’, ‘Black’ ]
31. Write a Python program to select an item randomly from
a list.
Output:
Black