100 Python Programming Exercises
100 Python Programming Exercises
Level Description
Level 1 Beginner 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 2 Intermediate 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 3 Advanced. 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.
Questions :
#----------------------------------------#
Question 1
Level 1
Question:
Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,
Hints:
#----------------------------------------#
#----------------------------------------#
Question 2
Level 1
Question:
40320
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
#----------------------------------------#
Question 3
Level 1
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.
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
#----------------------------------------#
Question 4
Level 1
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.
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.
#----------------------------------------#
#----------------------------------------#
Question 5
Level 1
Question:
Also please include simple test function to test the class methods.
Hints:
Use __init__ method to construct some parameters
#----------------------------------------#
#----------------------------------------#
Question 6
Level 2
Question:
Write a program that calculates and prints the value according to the given formula:
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
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.
#----------------------------------------#
#----------------------------------------#
Question 7
Level 2
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.
Example
3,5
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.
#----------------------------------------#
#----------------------------------------#
Question 8
Level 2
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.
without,hello,bag,world
bag,hello,without,world
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
#----------------------------------------#
Question 9
Level 2
Question:
Write a program that accepts sequence of lines as input and prints the lines after making all
characters in the sentence capitalized.
Hello world
HELLO WORLD
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
#----------------------------------------#
Question 10
Level 2
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.
hello world and practice makes perfect and hello world again
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.
#----------------------------------------#
#----------------------------------------#
Question 11
Level 2
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
1010
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
#----------------------------------------#
Question 12
Level 2
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.
#----------------------------------------#
#----------------------------------------#
Question 13
Level 2
Question:
Write a program that accepts a sentence and calculate the number of letters and digits.
LETTERS 10
DIGITS 3
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
#----------------------------------------#
Question 14
Level 2
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!
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.
#----------------------------------------#
#----------------------------------------#
Question 15
Level 2
Question:
Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.
11106
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
#----------------------------------------#
Question 16
Level 2
Question:
Use a list comprehension to square each odd number in a list. The list is input by a sequence of
comma-separated numbers.
1,2,3,4,5,6,7,8,9
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.
#----------------------------------------#
Question 17
Level 2
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 300
D 300
W 200
D 100
500
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
#----------------------------------------#
Question 18
Level 3
Question:
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.
Example
ABd1234@1,a F1#,2w3E*,2We3345
ABd1234@1
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
#----------------------------------------#
Question 19
Level 3
Question:
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.
#----------------------------------------#
#----------------------------------------#
Question 20
Level 3
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:
#----------------------------------------#
#----------------------------------------#
Question 21
Level 3
Question:
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:
UP 5
DOWN 3
LEFT 3
RIGHT 2
2
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
#----------------------------------------#
Question 22
Level 3
Question:
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.
#----------------------------------------#
#----------------------------------------#
Question 23
level 1
Question:
Hints:
#----------------------------------------#
#----------------------------------------#
Question 24
Level 1
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()
Hints:
#----------------------------------------#
#----------------------------------------#
Question 25
Level 1
Question:
Define a class, which have a class parameter and have a same instance parameter.
Hints:
You can init a object with construct parameter or set the value later
#----------------------------------------#
#----------------------------------------#
Question 26
Hints:
Define a function with two numbers as arguments. You can compute the sum in the function and
return the value.
#----------------------------------------#
Question 27
Define a function that can convert a integer into a string and print it in console.
Hints:
#----------------------------------------#
Question 28
Define a function that can receive two integral numbers in string form and compute their sum and
then print it in console.
Hints:
Question 29
Define a function that can accept two strings as input and concatenate them and then print it in
console.
Hints:
#----------------------------------------#
Question 30
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.
Hints:
#----------------------------------------#
Question 31
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".
Hints:
#----------------------------------------#
Question 32
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.
Hints:
Use dict[key]=value pattern to put entry into a dictionary.
#----------------------------------------#
Question 33
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.
Hints:
#----------------------------------------#
Question 34
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 keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
#----------------------------------------#
Question 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 keys only.
Hints:
Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
#----------------------------------------#
Question 36
Define a function which can generate and print a list where the values are square of numbers
between 1 and 20 (both included).
Hints:
#----------------------------------------#
Question 37
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:
#----------------------------------------#
Question 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 last 5 elements in the list.
Hints:
#----------------------------------------#
Question 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 all values except the first 5 elements in the list.
Hints:
#----------------------------------------#
2.10
Question 40
Define a function which can generate and print a tuple where the value are square of numbers
between 1 and 20 (both included).
Hints:
#----------------------------------------#
Question 41
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.
Hints:
#----------------------------------------#
Question 42
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:
#----------------------------------------#
Question 43
Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes",
otherwise print "No".
Hints:
Question 44
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:
#----------------------------------------#
Question 45
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:
#----------------------------------------#
Question 46
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:
Question 47
Write a program which can filter() to make a list whose elements are even number between 1 and 20
(both included).
Hints:
#----------------------------------------#
Question 48
Write a program which can map() to make a list whose elements are square of numbers between 1
and 20 (both included).
Hints:
#----------------------------------------#
Question 49
Define a class named American which has a static method called printNationality.
Hints:
Question 50
Hints:
#----------------------------------------#
Question 51
Define a class named Circle which can be constructed by a radius. The Circle class has a method
which can compute the area.
Hints:
#----------------------------------------#
Question 52
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.
Hints:
#----------------------------------------#
Question 53
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.
#----------------------------------------#
Question 54
Hints:
#----------------------------------------#
Question 55
Write a function to compute 5/0 and use try/except to catch the exceptions.
Hints:
#----------------------------------------#
Question 56
Define a custom exception class which takes a string message as attribute.
Hints:
#----------------------------------------#
Question 57
Example:
john@[Link]
john
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
#----------------------------------------#
Question 58
Assuming that we have some email addresses in the "username@[Link]" 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:
john@[Link]
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
#----------------------------------------#
Question 59
Write a program which accepts a sequence of words separated by whitespace as input to print the
words composed of digits only.
Example:
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
#----------------------------------------#
Question 60
Hints:
#----------------------------------------#
Question 61
Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8.
Hints:
#----------------------------------------#
Question 62
Example:
3.55
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
#----------------------------------------#
Question 63
and f(0)=1
Example:
500
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
#----------------------------------------#
Question 64
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:
13
In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
#----------------------------------------#
Question 65
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:
0,1,1,2,3,5,8,13
Hints:
#----------------------------------------#
Question 66
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:
10
0,2,4,6,8,10
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
Question 67
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:
0,35,70
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
#----------------------------------------#
Question 68
Please write assert statements to verify that every number in the list [2,4,6,8] is even.
Hints:
#----------------------------------------#
Question 69
Please write a program which accepts basic mathematic expression from console and print the
evaluation result.
Example:
35+3
Then, the output of the program should be:
38
Hints:
#----------------------------------------#
Question 70
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:
#----------------------------------------#
Question 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.
Hints:
#----------------------------------------#
Question 72
Please generate a random float where the value is between 10 and 100 using Python math module.
Hints:
#----------------------------------------#
Question 73
Please generate a random float where the value is between 5 and 95 using Python math module.
Hints:
#----------------------------------------#
Question 74
Please write a program to output a random even number between 0 and 10 inclusive using random
module and list comprehension.
Hints:
#----------------------------------------#
Question 75
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:
#----------------------------------------#
Question 76
Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive.
Hints:
#----------------------------------------#
Question 77
Please write a program to randomly generate a list with 5 even numbers between 100 and 200
inclusive.
Hints:
#----------------------------------------#
Question 78
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 [Link]() to generate a list of random values.
#----------------------------------------#
Question 79
Please write a program to randomly print a integer number between 7 and 15 inclusive.
Hints:
#----------------------------------------#
Question 80
Please write a program to compress and decompress the string "hello world!hello world!hello world!
hello world!".
Hints:
#----------------------------------------#
Question 81
Please write a program to print the running time of execution of "1+1" for 100 times.
Hints:
#----------------------------------------#
Question 82
Please write a program to shuffle and print the list [3,6,7,8].
Hints:
#----------------------------------------#
Question 83
Hints:
#----------------------------------------#
Question 84
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:
#----------------------------------------#
Question 85
Please write a program to print the list after removing delete even numbers in [5,6,77,45,22,12,24].
Hints:
#----------------------------------------#
Question 86
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:
#----------------------------------------#
Question 87
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:
#----------------------------------------#
Question 88
By using list comprehension, please write a program generate a 3*5*8 3D array whose each element
is 0.
Hints:
#----------------------------------------#
Question 89
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:
#----------------------------------------#
Question 90
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:
#----------------------------------------#
Question 91
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:
#----------------------------------------#
Question 92
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.
Hints:
Use set() to store a number of values without duplicate.
#----------------------------------------#
Question 93
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:
#----------------------------------------#
Question 94
Please write a program which count and print the numbers of each character in a string input by
console.
Example:
abcdefgabc
a,2
c,2
b,2
e,1
d,1
g,1
f,1
Hints:
#----------------------------------------#
Question 95
Please write a program which accepts a string from console and print it in reverse order.
Example:
Hints:
#----------------------------------------#
Question 96
Please write a program which accepts a string from console and print the characters that have even
indexes.
Example:
Helloworld
Hints:
#----------------------------------------#
Question 97
Hints:
#----------------------------------------#
Question 98
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:
#----------------------------------------#
Solutions:-
Solution:-1
l=[]
[Link](str(i))
print ','.join(l)
Solution:-2
def fact(x):
if x == 0:
return 1
return x * fact(x - 1)
x=int(raw_input())
print fact(x)
Solution:-3
n=int(raw_input())
d=dict()
for i in range(1,n+1):
d[i]=i*i
print d
Solution:-4
values=raw_input()
l=[Link](",")
t=tuple(l)
print l
print t
Solution:-5
class InputOutString(object):
def __init__(self):
self.s = ""
def getString(self):
self.s = raw_input()
def printString(self):
print [Link]()
strObj = InputOutString()
[Link]()
[Link]()
Solution:-6
#!/usr/bin/env python
import math
c=50
h=30
value = []
for d in items:
[Link](str(int(round([Link](2*c*float(d)/h)))))
print ','.join(value)
Solution:-7
input_str = raw_input()
rowNum=dimensions[0]
colNum=dimensions[1]
multilist[row][col]= row*col
print multilist
Solution:-8
[Link]()
print ','.join(items)
Solution:-9
lines = []
while True:
s = raw_input()
if s:
[Link]([Link]())
else:
break;
print sentence
Solution:-10
s = raw_input()
Solution:-11
value = []
for p in items:
intp = int(p, 2)
if not intp%5:
[Link](p)
print ','.join(value)
Solution:-12
values = []
s = str(i)
[Link](s)
print ",".join(values)
Solution:-13
s = raw_input()
d={"DIGITS":0, "LETTERS":0}
for c in s:
if [Link]():
d["DIGITS"]+=1
elif [Link]():
d["LETTERS"]+=1
else:
pass
Solution:-14
s = raw_input()
for c in s:
if [Link]():
d["UPPER CASE"]+=1
elif [Link]():
d["LOWER CASE"]+=1
else:
pass
Solution:-15
a = raw_input()
n1 = int( "%s" % a )
print n1+n2+n3+n4
Solution:-16
values = raw_input()
print ",".join(numbers)
Solution:-17
netAmount = 0
while True:
s = raw_input()
if not s:
break
operation = values[0]
amount = int(values[1])
if operation=="D":
netAmount+=amount
elif operation=="W":
netAmount-=amount
else:
pass
print netAmount
Solutions:-18
import re
value = []
for p in items:
if len(p)<6 or len(p)>12:
continue
else:
pass
if not [Link]("[a-z]",p):
continue
continue
elif not [Link]("[A-Z]",p):
continue
continue
elif [Link]("\s",p):
continue
else:
pass
[Link](p)
print ",".join(value)
Solutions:-19
l = []
while True:
s = raw_input()
if not s:
break
[Link](tuple([Link](",")))
Solution:-20
def putNumbers(n):
i=0
while i<n:
j=i
i=i+1
if j%7==0:
yield j
for i in reverse(100):
print i
Solution:-21
import math
pos = [0,0]
while True:
s = raw_input()
if not s:
break
direction = movement[0]
steps = int(movement[1])
if direction=="UP":
pos[0]+=steps
elif direction=="DOWN":
pos[0]-=steps
elif direction=="LEFT":
pos[1]-=steps
elif direction=="RIGHT":
pos[1]+=steps
else:
pass
print int(round([Link](pos[1]**2+pos[0]**2)))
Solution:-22
line = raw_input()
freq[word] = [Link](word,0)+1
words = [Link]()
[Link]()
for w in words:
Solution:-23
def square(num):
return num ** 2
print square(2)
print square(3)
Solution:-24
print abs.__doc__
print int.__doc__
print raw_input.__doc__
def square(num):
'''
return num ** 2
print square(2)
print square.__doc__
Solution:-25
class Person:
# Define the class parameter "name"
name = "Person"
[Link] = name
jeffrey = Person("Jeffrey")
nico = Person()
[Link] = "Nico"
Solution:-26
return number1+number2
print SumFunction(1,2)
Solution:-27
def printValue(n):
print str(n)
printValue(3)
Solution:-28
def printValue(s1,s2):
print int(s1)+int(s2)
printValue("3","4") #7
Solution:-29
def printValue(s1,s2):
print s1+s2
printValue("3","4") #34
Solution:-30
def printValue(s1,s2):
len1 = len(s1)
len2 = len(s2)
if len1>len2:
print s1
elif len2>len1:
print s2
else:
print s1
print s2
printValue("one","three")
Solution:-31
def checkValue(n):
if n%2 == 0:
else:
checkValue(7)
Solution:-32
def printDict():
d=dict()
d[1]=1
d[2]=2**2
d[3]=3**2
print d
printDict()
Solution:-33
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
print d
printDict()
Solution:-34
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
print v
printDict()
Solution:-35
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
for k in [Link]():
print k
printDict()
Solution:-36
def printList():
li=list()
for i in range(1,21):
[Link](i**2)
print li
printList()
Solution:-37
def printList():
li=list()
for i in range(1,21):
[Link](i**2)
print li[:5]
printList()
Solution:-38
def printList():
li=list()
for i in range(1,21):
[Link](i**2)
print li[-5:]
printList()
Solution:-39
def printList():
li=list()
for i in range(1,21):
[Link](i**2)
print li[5:]
printList()
Solution:-40
def printTuple():
li=list()
for i in range(1,21):
[Link](i**2)
print tuple(li)
printTuple()
Solution:-41
tp=(1,2,3,4,5,6,7,8,9,10)
tp1=tp[:5]
tp2=tp[5:]
print tp1
print tp2
Solution:-42
tp=(1,2,3,4,5,6,7,8,9,10)
li=list()
for i in tp:
if tp[i]%2==0:
[Link](tp[i])
tp2=tuple(li)
print tp2
Solution:-43
s= raw_input()
print "Yes"
else:
print "No"
Solution:-44
li = [1,2,3,4,5,6,7,8,9,10]
print evenNumbers
Solution:- 45
li = [1,2,3,4,5,6,7,8,9,10]
print squaredNumbers
Solution:-46
li = [1,2,3,4,5,6,7,8,9,10]
print evenNumbers
Solution:-47
print evenNumbers
Solution:-48
print squaredNumbers
Solution:-49
class American(object):
@staticmethod
def printNationality():
print "America"
anAmerican = American()
[Link]()
[Link]()
Solution:-50
class American(object):
pass
class NewYorker(American):
pass
anAmerican = American()
aNewYorker = NewYorker()
print anAmerican
print aNewYorker
Solution:-51
class Circle(object):
[Link] = r
def area(self):
return [Link]**2*3.14
aCircle = Circle(2)
print [Link]()
Solution:-52
class Rectangle(object):
[Link] = l
[Link] = w
def area(self):
return [Link]*[Link]
aRectangle = Rectangle(2,10)
print [Link]()
Solution:-53
class Shape(object):
def __init__(self):
pass
def area(self):
return 0
class Square(Shape):
Shape.__init__(self)
[Link] = l
def area(self):
return [Link]*[Link]
aSquare= Square(3)
print [Link]()
Solution :-54
Solution:-55
def throws():
return 5/0
try:
throws()
except ZeroDivisionError:
finally:
Solution:-56
class MyError(Exception):
Attributes:
msg -- explanation of the error
"""
[Link] = msg
Solution:-57
import re
emailAddress = raw_input()
pat2 = "(\w+)@((\w+\.)+(com))"
r2 = [Link](pat2,emailAddress)
print [Link](1)
Solution:-58
import re
emailAddress = raw_input()
pat2 = "(\w+)@(\w+)\.(com)"
r2 = [Link](pat2,emailAddress)
print [Link](2)
Solution:-59
import re
s = raw_input()
print [Link]("\d+",s)
Solution:-60
print unicodeString
Solution:-61
s = raw_input()
u = unicode( s ,"utf-8")
print u
Solution:- 62
n=int(raw_input())
sum=0.0
for i in range(1,n+1):
sum += float(float(i)/(i+1))
print sum
Solution:-63
def f(n):
if n==0:
return 0
else:
return f(n-1)+100
n=int(raw_input())
print f(n)
Solution:-64
def f(n):
if n == 0: return 0
elif n == 1: return 1
n=int(raw_input())
print f(n)
Solution:-65
def f(n):
if n == 0: return 0
elif n == 1: return 1
n=int(raw_input())
print ",".join(values)
Solution:-66
def EvenGenerator(n):
i=0
while i<=n:
if i%2==0:
yield i
i+=1
n=int(raw_input())
values = []
for i in EvenGenerator(n):
[Link](str(i))
print ",".join(values)
Solution:-67
def NumGenerator(n):
for i in range(n+1):
yield i
n=int(raw_input())
values = []
for i in NumGenerator(n):
[Link](str(i))
print ",".join(values)
Solution:-68
li = [2,4,6,8]
for i in li:
assert i%2==0
Solution:-69
expression = raw_input()
print eval(expression)
Solution:-70
import math
bottom = 0
top = len(li)-1
index = -1
mid = int([Link]((top+bottom)/2.0))
if li[mid]==element:
index = mid
elif li[mid]>element:
top = mid-1
else:
bottom = mid+1
return index
li=[2,5,7,9,11,17,222]
print bin_search(li,11)
print bin_search(li,12)
Solution:-71
import math
bottom = 0
top = len(li)-1
index = -1
mid = int([Link]((top+bottom)/2.0))
if li[mid]==element:
index = mid
elif li[mid]>element:
top = mid-1
else:
bottom = mid+1
return index
li=[2,5,7,9,11,17,222]
print bin_search(li,11)
print bin_search(li,12)
Solution:-72
import random
print [Link]()*100
Solution:-73
import random
print [Link]()*100-5
Solution:-74
import random
Solution:-75
import random
print [Link]([i for i in range(201) if i%5==0 and i%7==0])
Solution:-76
import random
print [Link](range(100), 5)
Solution:-77
import random
Solution:- 78
import random
Solution:-79
import random
print [Link](7,16)
Solution:-80
import zlib
t = [Link](s)
print t
print [Link](t)
Solution:-81
t = Timer("for i in range(100):1+1")
print [Link]()
Solution:-82
li = [3,6,7,8]
shuffle(li)
print li
Solution:-83
li = [3,6,7,8]
shuffle(li)
print li
Solution:-84
subjects=["I", "You"]
verbs=["Play", "Love"]
objects=["Hockey","Football"]
for i in range(len(subjects)):
for j in range(len(verbs)):
for k in range(len(objects)):
print sentence
Solution:-85
li = [5,6,77,45,22,12,24]
li = [x for x in li if x%2!=0]
print li
Solution:- 86
li = [12,24,35,70,88,120,155]
print li
Solution:-87
li = [12,24,35,70,88,120,155]
print li
Solution:-88
array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)]
print array
Solution:-89
li = [12,24,35,70,88,120,155]
print li
Solution:-90
li = [12,24,35,24,88,120,155]
li = [x for x in li if x!=24]
print li
Solution:-91
set1=set([1,3,6,78,35,55])
set2=set([12,24,35,24,88,120,155])
li=list(set1)
print li
Solution:-92
def removeDuplicate( li ):
newli=[]
seen = set()
[Link]( item )
[Link](item)
return newli
li=[12,24,35,24,88,120,155,88,120,155]
print removeDuplicate(li)
Solution:-93
class Person(object):
return "Unknown"
return "Male"
return "Female"
aMale = Male()
aFemale= Female()
print [Link]()
print [Link]()
Solution:-94
dic = {}
s=raw_input()
for s in s:
dic[s] = [Link](s,0)+1
s=raw_input()
s = s[::-1]
print s
Solution:-96
s=raw_input()
s = s[::2]
print s
Solution:-97
import itertools
print list([Link]([1,2,3]))
Solution:-98
def solve(numheads,numlegs):
ns='No solutions!'
for i in range(numheads+1):
j=numheads-i
if 2*i+4*j==numlegs:
return i,j
return ns,ns
numheads=35
numlegs=94
solutions=solve(numheads,numlegs)
print solutions