python练习题(持续更新)

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道题吧,以后会一直更新的,如果有帮到大家,可以点一个赞或者关注!

你可能感兴趣的:(python)