Python Basic Exercises 100 Questions
Python Basic Exercises 100 Questions
Website: www.analytixlabs.co.in
Email: info@analytixlabsl.co.in
Disclaimer: This material is protected under copyright act AnalytixLabs©, 2011-2015. Unauthorized
use and/ or duplication of this material or any part of this material including data, in any form
without explicit and written permission from AnalytixLabs is strictly prohibited. Any violation of this
copyright will attract legal actions.
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.
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
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.
Then, the output should be: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input. Consider use dict()
Suppose the input is supplied to the program: 34, 67, 55, 33, 12, 98
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input. tuple() method can convert list to tuple
6. Write a program that calculates and prints the value according to the given formula:
Let us assume the following comma separated input sequence is given to the program:
100,150,180
Hints: If the output received is in decimal form, it should be rounded off to its nearest value
(for example, if the output received is 26.0, it should be printed as 26)
In case of input data being supplied to the question, it should be assumed to be a console
input.
7. 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.
Then, the output of the program should be: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input in a comma-separated form.
8. 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 input is supplied to the program: without, hello, bag, world
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
9. Write a program that accepts sequence of lines as input and prints the lines after making
all characters in the sentence capitalized.
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
10. 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 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
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input. We use set container to remove duplicated data automatically and then use
sorted() to sort the data.
11. Write a program which accepts a sequence of comma separated 4 digit binary numbers
as its input and then check whether they are divisible by 5 or not. The numbers that are
divisible by 5 are to be printed in a comma separated sequence.
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
12. 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.
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
13. Write a program that accepts a sentence and calculate the number of letters and digits.
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
14. Write a program that accepts a sentence and calculate the number of upper case letters
and lower case letters.
LOWER CASE 9
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
15. Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the
value of a.
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
16. Use a list comprehension to square each odd number in a list. The list is input by a
sequence of comma-separated numbers.
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
17. Write a program that computes the net amount of a bank account based a transaction
log from console input. The transaction log format is shown as following:
D 100
W 200
D 300
W 200
D 100
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
18. A website requires the users to input username and password to register. Write a
program to check the validity of password input by users.
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.
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
19. 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:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input. We use itemgetter to enable multiple sort keys.
20. Define a class with a generator which can iterate the numbers, which are divisible by 7,
between a given range 0 and n.
21. 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.
UP 5
DOWN 3
RIGHT 2
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
22. Write a program to compute the frequency of the words from the input. The output
should output after sorting the key alphanumerically.
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1
Hints: In case of input data being supplied to the question, it should be assumed to be a
console input.
24. Python has many built-in functions, and if you do not know how to use it, you can read
document online or find some books. But Python has a built-in document function for every
25. Define a class, which have a class parameter and have a same instance parameter.
You can init a object with construct parameter or set the value later
26. Define a function which can compute the sum of two numbers.
Hints: Define a function with two numbers as arguments. You can compute the sum in the
function and return the value.
27. Define a function that can convert a integer into a string and print it in console.
28. Define a function that can convert a integer into a string and print it in console.
29. Define a function that can receive two integral numbers in string form and compute
their sum and then print it in console.
30. Define a function that can accept two strings as input and concatenate them and then
print it in console.
31. Define a function that can accept two strings as input and print the string with maximum
length in console. If two strings have the same length, then the function should print al l
strings line by line.
32. Define a function that can accept an integer number as input and print the "It is an even
number" if the number is even, otherwise print "It is an odd number".
33. Define a function which can print a dictionary where the keys are numbers between 1
and 3 (both included) and the values are square of keys.
34. Define a function which can print a dictionary where the keys are numbers between 1
and 20 (both included) and the values are square of keys.
35. Define a function which can generate a dictionary where the keys are numbers between
1 and 20 (both included) and the values are square of keys. The function should just print
the values only.
Hints: Use dict[key]=value pattern to put entry into a dictionary. Use ** operator to get
power of a number. Use range() for loops. Use keys() to iterate keys in the dictionary. Also
we can use item() to get key/value pairs.
36. Define a function which can generate a dictionary where the keys are numbers between
1 and 20 (both included) and the values are square of keys. The function should just print
the keys only.
Hints: Use dict[key]=value pattern to put entry into a dictionary. Use ** operator to get
power of a number. Use range() for loops. Use keys() to iterate keys in the dictionary. Also
we can use item() to get key/value pairs.
37. Define a function which can generate and print a list where the values are square of
numbers between 1 and 20 (both included).
Hints: Use ** operator to get power of a number. Use range() for loops. Use list.append() to
add values into a list.
38. Define a function which can generate a list where the values are square of numbers
between 1 and 20 (both included). Then the function needs to print the first 5 elements in
the list.
Hints: Use ** operator to get power of a number. Use range() for loops. Use list.append() to
add values into a list. Use [n1:n2] to slice a list
39. Define a function which can generate a list where the values are square of numbers
between 1 and 20 (both included). Then the function needs to print the last 5 elements in
the list.
Hints: Use ** operator to get power of a number. Use range() for loops. Use list.append() to
add values into a list. Use [n1:n2] to slice a list
Hints: Use ** operator to get power of a number. Use range() for loops. Use list.append() to
add values into a list. Use [n1:n2] to slice a list
41. Define a function which can generate and print a tuple where the value are square of
numbers between 1 and 20 (both included).
Hints: Use ** operator to get power of a number. Use range() for loops. Use list.append() to
add values into a list. Use tuple() to get a tuple from a list.
42. With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in
one line and the last half values in one line.
43. Write a program to generate and print another tuple whose values are even numbers in
the given tuple (1,2,3,4,5,6,7,8,9,10).
Hints: Use "for" to iterate the tuple. Use tuple() to generate a tuple from a list.
44. Write a program which accepts a string as input to print "Yes" if the string is "yes" or
"YES" or "Yes", otherwise print "No".
45. Write a program which can filter even numbers in a list by using filter function. The list
is: [1,2,3,4,5,6,7,8,9,10].
Hints: use filter() to filter some elements in a list. Use lambda to define anonymous
functions.
46. Write a program which can map() to make a list whose elements are square of elements
in [1,2,3,4,5,6,7,8,9,10].
Hints: Use map() to generate a list. Use lambda to define anonymous functions.
47. Write a program which can map() and filter() to make a list whose elements are square
of even number in [1,2,3,4,5,6,7,8,9,10].
Hints: Use map() to generate a list. Use filter() to filter elements of a list. Use lambda to
define anonymous functions.
48. Write a program which can filter() to make a list whose elements are even number
between 1 and 20 (both included).
49. Write a program which can map() to make a list whose elements are square of numbers
between 1 and 20 (both included).
Hints: Use map() to generate a list. Use lambda to define anonymous functions.
50. Define a class named American which has a static method called printNationality.
52. Define a class named Circle which can be constructed by a radius. The Circle class has a
method which can compute the area.
53. Define a class named Rectangle which can be constructed by a length and width. The
Rectangle class has a method which can compute the area.
54. Define a class named Shape and its subclass Square. The Square class has an init function
which takes a length as argument. Both classes have a area function which can print the
area of the shape where Shape's area is 0 by default.
Hints: To override a method in super class, we can define a method with the same name in
the super class.
55. Write a function to compute 5/0 and use try/except to catch the exceptions.
56. Define a custom exception class which takes a string message as attribute.
Hints: To define a custom exception, we need to define a class inherited from Exception.
In case of input data being supplied to the question, it should be assumed to be a console
input.
59. Write a program which accepts a sequence of words separated by whitespace as input
to print the words composed of digits only.
If the following words are given as input to the program: 2 cats and 3 dogs.
In case of input data being supplied to the question, it should be assumed to be a console
input.
61. Write a program to read an ASCII string and to convert it to a unicode string encoded by
utf-8.
62. Write a special comment to indicate a Python source code file is in unicode.
and f(0)=1
In case of input data being supplied to the question, it should be assumed to be a console
input.
f(n)=0 if n=0
f(n)=1 if n=1
f(n)=f(n-1)+f(n-2) if n>1
Please write a program to compute the value of f(n) with a given n input by console.
In case of input data being supplied to the question, it should be assumed to be a console
input.
f(n)=0 if n=0
f(n)=1 if n=1
f(n)=f(n-1)+f(n-2) if n>1
Hints: We can define recursive function in Python. Use list comprehension to generate a list
from an existing list. Use string.join() to join a list of strings. In case of input data being
supplied to the question, it should be assumed to be a console input.
67. Please write a program using generator to print the even numbers between 0 and n in
comma separated form while n is input by console.
Hints: Use yield to produce the next value in generator. In case of input data being supplied
to the question, it should be assumed to be a console input.
68. Please write a program using generator to print the numbers which can be divisible by 5
and 7 between 0 and n in comma separated form while n is input by console.
Hints: Use yield to produce the next value in generator. In case of input data being supplied
to the question, it should be assumed to be a console input.
69. Please write assert statements to verify that every number in the list [2,4,6,8] is even.
70. Please write a program which accepts basic mathematic expression from console and
print the evaluation result.
71. Please write a binary search function which searches an item in a sorted list. The
function should return the index of element to be searched in the list.
73. Please generate a random float where the value is between 10 and 100 using Python
math module.
74. Please generate a random float where the value is between 5 and 95 using Python math
module.
75. Please write a program to output a random even number between 0 and 10 inclusive
using random module and list comprehension.
76. Please write a program to output a random number, which is divisible by 5 and 7,
between 0 and 10 inclusive using random module and list comprehension.
77. Please write a program to generate a list with 5 random numbers between 100 and 200
inclusive.
78. Please write a program to randomly generate a list with 5 even numbers between 100
and 200 inclusive.
79. Please write a program to randomly generate a list with 5 numbers, which are divisible
by 5 and 7 , between 1 and 1000 inclusive.
80. Please write a program to randomly print a integer number between 7 and 15 inclusive.
81. Please write a program to compress and decompress the string "hello world!hello
world!hello world!hello world!".
83. Please write a program to shuffle and print the list [3,6,7,8].
84. Please write a program to shuffle and print the list [3,6,7,8].
85. Please write a program to generate all sentences where subject is in ["I", "You"] and
verb is in ["Play", "Love"] and the object is in ["Hockey","Football"].
86. Please write a program to print the list after removing delete even numbers in
[5,6,77,45,22,12,24].
87. By using list comprehension, please write a program to print the list after removing
delete numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155].
88. By using list comprehension, please write a program to print the list after removing the
0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155].
Hints: Use list comprehension to delete a bunch of element from a list. Use enumerate() to
get (index, value) tuple.
89. By using list comprehension, please write a program generate a 3*5*8 3D array whose
each element is 0.
90. By using list comprehension, please write a program to print the list after removing the
0th,4th,5th numbers in [12,24,35,70,88,120,155].
Hints: Use list comprehension to delete a bunch of element from a list. Use enumerate() to
get (index, value) tuple.
91. By using list comprehension, please write a program to print the list after removing the
value 24 in [12,24,35,24,88,120,155].
93. With a given list [12, 24, 35, 24, 88, 120, 155, 88, 120, 155], write a program to print this
list after removing all duplicate values with original order reserved.
94. Define a class Person and its two child classes: Male and Female. All classes have a
method "getGender" which can print "Male" for Male class and "Female" for Female class.
95. Please write a program which count and print the numbers of each character in a string
input by console.
a,2
c,2
b,2
e,1
d,1
g,1
f,1
Hints: Use dict to store key/value pairs. Use dict.get() method to lookup a key with default
value.
96 Please write a program which accepts a string from console and print it in reverse order.
If the following string is given as input to the program: rise to vote sir
Then, the output of the program should be: ris etov ot esir
97. Please write a program which accepts a string from console and print the characters
that have even indexes.
We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits
and how many chickens do we have?
100. Create a dictionary with phone numbers (phonebook = {“John”: 938477566, "Jack”:
938377264, "Jill”: 947662781}). Add "Jake" to the phonebook with the phone number
938273443, and remove Jill from the phonebook.