Simple Python Exercises¶
These questions were taken directly from 100+ Python challenging programming exercises where you can also find the solutions. These are OPTIONAL exercises and will not be graded. I have just slightly formatted them and put into a notebook as a convenience to you
Notes:
- The questions are not actually “challenging” but may be useful to practice basic Python coding
- I have not attempted these questions
- I do not know if the provided solutions are correct
- Some questions/hints seem to be Python 2 specific - you may need to modify for Python 3
- Where it calls for a “program” and console input, you should just use a function instead
- You should be able to solve most of these challenges without reference to any outside material
In [ ]:
Question:
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.
Hints:
Consider use range(#begin, #end)
method
In [ ]:
Question:
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
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
In [ ]:
Question:
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:
{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()
In [ ]:
Question:
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')
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
In [ ]:
Question:
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.
Hints:
Use __init__
method to construct some parameters
In [ ]:
Question:
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
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.
In [ ]:
Question:
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]]
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.
In [ ]:
Question:
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
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
In [ ]:
Question:
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
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.
In [ ]:
Question:
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.
Example:
0100,0011,1010,1001
Then the output should be:
1010
Notes: Assume the data is input by console.
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
In [ ]:
Question:
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.
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
In [ ]:
Question:
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
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
In [ ]:
Question:
Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.
Suppose the following input is supplied to the program:
Hello world!
Then, the output should be:
UPPER CASE 1
LOWER CASE 9
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
In [ ]:
Question:
Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.
Suppose the following input is supplied to the program:
9
Then, the output should be:
11106
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
In [ ]:
Question:
Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers.
Suppose the following input is supplied to the program:
1,2,3,4,5,6,7,8,9
Then, the output should be:
1,3,5,7,9
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
In [ ]:
Question:
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 means deposit while W means withdrawal.
Suppose the following input is supplied to the program:
D 300
D 300
W 200
D 100
Then, the output should be:
500
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
In [ ]:
Question:
Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.
Hints:
Consider use yield
In [ ]:
Question:
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
Hints
In case of input data being supplied to the question, it should be assumed to be a console input.
In [ ]:
Question: Write a method which can calculate square value of number
Hints: Using the **
operator
In [ ]:
Question:
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 built-in functions.
Please write a program to print some Python built-in functions
documents, such as abs()
, int()
, raw_input()
And add document for your own function
Hints:
The built-in document method is __doc__
In [ ]:
Question:
Define a class, which have a class parameter and have a same instance parameter.
Hints:
Define a instance parameter, need add it in __init__
method
You can init a object with construct parameter or set the value later
In [ ]:
Question:
Define a class named American and its subclass NewYorker.
Hints:
Use class Subclass(ParentClass) to define a subclass.
In [ ]:
Question:
Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area.
Hints:
Use def methodName(self) to define a method.
In [ ]:
Question:
Assuming that we have some email addresses in the “username@companyname.com” format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only.
Example:
If the following email address is given as input to the program:
john@google.com
Then, the output of the program should be:
john
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
Use \w
to match letters.
In [ ]:
Question:
Assuming that we have some email addresses in the “username@companyname.com” format, please write program to print the company name of a given email address. Both user names and company names are composed of letters only.
Example: If the following email address is given as input to the program:
john@google.com
Then, the output of the program should be:
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
Use \w
to match letters.
In [ ]:
Question:
Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only.
Example:
If the following words is given as input to the program:
2 cats and 3 dogs.
Then, the output of the program should be:
['2', '3']
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
Use re.findall()
to find all substring using regex.
In [ ]:
Question:
Print a unicode string “hello world”.
Hints:
Use u’strings’ format to define unicode string.
In [ ]:
Question:
Write a special comment to indicate a Python source code file is in unicode.
Hints:
In [ ]:
Question:
Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).
Example:
If the following n is given as input to the program:
5
Then, the output of the program should be:
3.55
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
Use float()
to convert an integer to a float
In [ ]:
Question:
Write a program to compute:
f(n)=f(n-1)+100 when n>0 and f(0)=1
with a given n input by console (n>0).
Example:
If the following n is given as input to the program:
5
Then, the output of the program should be:
500
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
We can define recursive function in Python.
In [ ]:
Question:
The Fibonacci Sequence is computed based on the following formula:
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.
Example:
If the following n is given as input to the program:
7
Then, the output of the program should be:
13
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
We can define recursive function in Python.
In [ ]:
Question:
The Fibonacci Sequence is computed based on the following formula:
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 using list comprehension to print the Fibonacci Sequence in comma separated form with a given n input by console.
Example:
If the following n is given as input to the program:
7
Then, the output of the program should be:
0,1,1,2,3,5,8,13
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.
In [ ]:
Question:
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.
Example:
If the following n is given as input to the program:
10
Then, the output of the program should be:
0,2,4,6,8,10
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.
In [ ]:
Question:
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.
Example:
If the following n is given as input to the program:
100
Then, the output of the program should be:
0,35,70
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.
In [ ]:
Question:
Please write assert statements to verify that every number in the list [2,4,6,8] is even.
Hints:
Use “assert expression” to make assertion.
In [ ]:
Question:
Please write a program which accepts basic mathematic expression from console and print the evaluation result.
Example:
If the following string is given as input to the program:
35+3
Then, the output of the program should be:
38
Hints:
Use eval()
to evaluate an expression.
In [ ]:
Question:
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.
Hints: Use if/elif
to deal with conditions.
In [ ]:
Question:
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.
Hints:
Use if/elif
to deal with conditions.
In [ ]:
Question:
Please generate a random float where the value is between 10 and 100 using Python math module.
Hints:
Use random.random()
to generate a random float in [0,1].
In [ ]:
Question:
Please generate a random float where the value is between 5 and 95 using Python math module.
Hints:
Use random.random()
to generate a random float in [0,1].
In [ ]:
Question:
Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension.
Hints: Use random.choice()
to a random element from a list.
In [ ]:
Question:
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.
Hints:
Use random.choice()
to a random element from a list.
In [ ]:
Question:
Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive.
Hints:
Use random.sample()
to generate a list of random values.
In [ ]:
Question:
Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive.
Hints:
Use random.sample()
to generate a list of random values.
In [ ]:
Question:
Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive.
Hints:
Use random.sample()
to generate a list of random values.
In [ ]:
Question:
Please write a program to randomly print a integer number between 7 and 15 inclusive.
Hints:
Use random.randrange()
to a random integer in a given range.
In [ ]:
Question:
Please write a program to compress and decompress the string “hello world!hello world!hello world!hello world!”.
Hints:
Use zlib.compress()
and zlib.decompress()
to compress and
decompress a string.
In [ ]:
Question:
Please write a program to print the running time of execution of “1+1” for 100 times.
Hints:
Use timeit()
function to measure the running time.
In [ ]:
Question:
Please write a program to shuffle and print the list [3,6,7,8].
Hints:
Use shuffle()
function to shuffle a list.
In [ ]:
Question:
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”].
Hints:
Use list[index]
notation to get a element from a list.
In [ ]:
Question:
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].
Hints:
Use list comprehension to delete a bunch of element from a list.
In [ ]:
Question:
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.
In [ ]:
Question:
By using list comprehension, please write a program generate a 358 3D array whose each element is 0.
Hints:
Use list comprehension to make an array.
In [ ]:
Question:
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.
In [ ]:
Question:
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].
Hints:
Use list’s remove method to delete a value.
In [ ]:
Question:
With two given lists [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a program to make a list whose elements are intersection of the above given lists.
Hints:
Use set() and “&=” to do set intersection operation.
In [ ]:
Question:
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.
Hints:
Use Subclass(Parentclass) to define a child class.
In [ ]:
Question:
Please write a program which count and print the numbers of each character in a string input by console.
Example:
If the following string is given as input to the program:
abcdefgabc
Then, the output of the program should be:
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.
Question:
Please write a program which accepts a string from console and print it in reverse order.
Example:
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
Hints:
Use list[::-1]
to iterate a list in a reverse order.
In [ ]:
Question:
Please write a program which accepts a string from console and print the characters that have even indexes.
Example:
If the following string is given as input to the program:
H1e2l3l4o5w6o7r8l9d
Then, the output of the program should be:
Helloworld
Hints:
Use list[::2]
to iterate a list by step 2.
In [ ]:
Question:
Please write a program which prints all permutations of [1,2,3]
Hints:
Use itertools.permutations()
to get permutations of list.
In [ ]:
Question:
Write a program to solve a classic ancient Chinese puzzle: 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?
Hint:
Use for loop to iterate all possible solutions.
In [ ]: