100道Python编程练习题
100+ Python challenging programming exercise
表格制作教程大全100道Python编程练习题,这些题如果能坚持每天⾄少完成⼀道,⼀定可以帮⼤家轻松 get Python 的编程技能。⽬前,这个项⽬已经获得了 2924 Stars,2468 Forks。
⾸先,这 100+ 练习题根据难易程度分为三个等级:Level 1、2 和 3。下⾯对如何定义这三个 Level 进⾏了说明,⼤家可以结合⾃⾝的学习能⼒和实践经验进⾏选择。
Level 1:初级。刚⼊门 Python 或者正在学⼀些基础课程的同学们。通常包含 1 到 2 个类或函数的问题都可以解决,甚⾄答案都可能在⼀些教材中就能到。
Level 2:中级。已经系统学习过 Python,并且已经有⼀定的编程背景的同学们,可以解决包含 3 个及以上类或函数的问题,不过这些答案就在教材不到了。
Level 3:⾼级:可以⽤ Python 中⾮常丰富的各种库、标准包或更⾼级的技术,结合数据结构和算法,来解决复杂的问题。
其次,每题都有问题描述、提⽰和解决⽅案。⼤家⼀定要先独⽴完成,然后再看参考答案哦~
前 25 题中,Q1~5、Q22~25 都是 Level 1 的难度,Q6~17 为 Level 2,Q18~22 为 Level 3。⼤家正好利⽤这五道题学习、巩固⼀下基础,然后就开始准备挑战⾃⼰吧!
原始的题⽬⼤家也可以在 Github 上看到:
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,
between 2000and3200(both included).
The numbers obtained should be printed in a comma-separated sequence on a single line.
Hints:
Consider use range(#begin, #end) method
# P001 solution
l=[]
for i in range(2000,3201):
if(i%7==0)and(i%5!=0):
l.append(str(i))
print(','.join(l))
#----------------------------------------#
#----------------------------------------#
Question 2
Level 1
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.
# P002 solution
return1
学python编程入门return x * fact(x -1)
x=int(input())
print(fact(x))
#----------------------------------------#
#----------------------------------------#
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 1and 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:javascript上网课
In case of input data being supplied to the question, it should be assumed to be a console input. Consider use dict()
# P003 solution:
n=int(input())
d=dict()
for i in range(1,n+1):
d[i]=i*i
print(d)
#----------------------------------------#
#----------------------------------------#
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.
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
# P004 solution:
values=input()
l=values.split(",")
t=tuple(l)
print(l)
print(t)
#----------------------------------------#
#----------------------------------------#
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.
Hints:
Use __init__ method to construct some parameters
class InputOutString(object):
def__init__(self):
self.s =""
def getString(self):
self.s =input()
def printString(self):
print(self.s.upper())
strObj = InputOutString()
strObj.printString()
#----------------------------------------#
#----------------------------------------#
Question 6
Level 2
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 is50. H is30.
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 is26.0, it should be printed as26)
In case of input data being supplied to the question, it should be assumed to be a console input. # P006 solution
import math
c, h, value =50,30,[]
items =[x for x in input().split(',')]
for d in items:
value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))
print(','.join(value))
#----------------------------------------#
#----------------------------------------#
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.
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.
# P007 solution
input_str =input()
dimensions =[int(x)for x in input_str.split(',')]
rowNum = dimensions[0]
colNum = dimensions[1]
multilist =[[0for col in range(colNum)]for row in range(rowNum)]
for row in range(rowNum):
for col in range(colNum):
multilist[row][col]= row*col
print(multilist)
#----------------------------------------#
#----------------------------------------#
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.
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.
# P008 solution:
inject
items =[ x for x in input().split(',')]
items.sort()
print(','.join(items))
#----------------------------------------#
#----------------------------------------#
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.
Suppose the following input is supplied to the program:
Hello world
Practice makes perfect
Then, the output should be:
HELLO WORLD
PRACTICE MAKES PERFECT
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
lines =[]
while True:
s =input()
if s:
lines.append(s.upper())
else:
break;
for sentence in lines:
print(sentence)
#----------------------------------------#
#----------------------------------------#
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.
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.
Solution:
s =input()
words =[ word for word in s.split(' ')]
print(' '.join(sorted(list(set(words)))))
解释器模式的优缺点#----------------------------------------#
#----------------------------------------#
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
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.
Solution:
value =[]
items =[x for x in input().split(',')]
for p in items:
intp =int(p,2)
if not intp %5:
value.append(p)
print(','.join(value))
#----------------------------------------#
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论