python 100道练习题
这些题都是我从GitHub上找到的,原题都是英文的,我对每道题都做了翻译,也附上了自己的做法,一般情况下,第二种解法是原答主给出的解决方法,如果一道题我只给出了一个解法就说明我的解法和原答主是相同的。
我会一点点的发出来的,希望大家和我们一起进步。
原答主用的是python2.x版本,可能会有一些语法不同,我只修改了其中的一部分.
原网址如下:
原答主的题目链接
强调:
每一道题都是有等级评定的,按照原答主的理解,可以分为以下三类:
- lv1 对于那些初步浏览过python课程的初学者,只能独立解决一两道Python类或者函数的题,而这些题通常可以在课本上找到.
- lv2 已经对python有初步的了解,能解决涉及到3个或3个以上类或函数的问题,这些问题一般在课本上不能直接找到答案.
- lv3 已经可以用python的各种库解决一些复杂的问题.
1 lv1
# 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.
# 问题:
# 写一个程序,找出所有在2000至3200之间(包括2000和3200)的能被7整除但不是5的倍数的数,
# 获得的数字应该以逗号分隔的序列打印在单行上。
```python
list1=[]
for i in range(2000,3201):
list1.append(i)
print(list((filter(lambda x: x % 7 == 0 and x % 5 != 0,list1))))
l=[]
for i in range(2000, 3201):
if (i%7==0) and (i%5!=0):
l.append(str(i))
print(','.join(l))
2 lv1
用递归函数求解
# 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
#写一个计算阶乘的程序
#当输入8时,输出40320
def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
x=int(input())
print(fact(x))
3 lv1
# 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}
#生成从1到n的一个字典,这个字典的key是i,value是i*i,输出如下例:
# {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
dict1={
}
def generate_dict(n):
for i in range(1,n+1):
dict1[i]=i*i
return dict1
print(generate_dict(8))
n=int(raw_input())
d=dict()
for i in range(1,n+1):
d[i]=i*i
print d
4 lv1
# 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')
#以用逗号隔开的一串数字作为输入,并且产生由这串数字组成的列表和元祖,如:
#输入
# 34,67,55,33,12,98
# 输出
# ['34', '67', '55', '33', '12', '98']
# ('34', '67', '55', '33', '12', '98')
n=int(input())
list1=[]
for i in range(0,n):
list1.append(input())
print(list1)
print(tuple(list1))
values=input()
print(type(values))
l=values.split(",")
t=tuple(l)
print(l)
print(t)
5 lv1
# 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.
# 定义一个至少有两种方法的类:
# 一、获取输入控制台的字符串
# 二、用大写的形式将其打印出来
# 三、包含一些简单的测试函数去测试这些方法
class in_and_out_string():
def __init__(self):
self.st=''
def get_string(self):
self.st=input()
def print_string(self):
print(self.st.upper())
test=in_and_out_string()
test.get_string()
test.print_string()
先来5道题吧,以后会一直更新的,如果有帮到大家,可以点一个赞或者关注!
我打算把所有的题目全部搬到这儿来,方便大家阅读。
6 lv2
这一道题我在写代码的时候做了一些修改,没有对一些变量进行赋值。
# 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
#写一个这样的程序,在满足Q=[(2*50*D)/30]^0.5的情况下,D为任意值,当输入100,150,180,它的输出结果是18,22,24
a=input()
b=a.split(',')
P=[]
import math
for i in range(0,len(b)-1):
Q=math.sqrt((2*50*int(b[i]))/30)
print(int(Q),end='')
print(',',end='')
Q=math.sqrt((2*50*int(b[-1]))/30)
print(int(Q))
for i in b:
P.append(str(int(math.sqrt((2 * 50 * int(i)) / 30))))
print(P)
print(','.join(P))
7 lv2
# 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]]
# 写一个二维数组,第i行和第j列的值为i*j,如:
# 当输入3,5时
# 输出:
# [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
x=input()
i=int(x.split(',')[0])
j=int(x.split(',')[1])
two_dimensional_array=[]
save=[]
for k in range(0,i):
for l in range(0,j):
save.append(k*l)
two_dimensional_array.append(save)
save=[]
print(two_dimensional_array)
input_str = input()
dimensions=[int(x) for x in input_str.split(',')]
rowNum=dimensions[0]
colNum=dimensions[1]
multilist = [[0 for 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)
8 lv2
# 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
#写一个程序,接受逗号隔开的单词作为输入,并且将它们按单词表排序用以逗号隔开的方式打印
#例如:输入:
# without,hello,bag,world
# 输出:
# bag, hello, without, world
items=[x for x in input().split(',')]
items.sort()
print(','.join(items))
#我们可以通过冒泡排序来进行排序,代码如下:(其实是没有必要的,因为python是面向对象的语言)
def bubble(x):
n=len(x)
for i in</