100+Python程式設計題帶你快速上手(附答案)
春節馬上就要到了,怎麼能讓自己在假期裡不掉隊?今天,給大家準備一個專案: 100+ 程式設計練習,這些題如果能堅持每天至少完成一道,一定可以幫大家輕鬆 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、22~25 都是 Level 1 的難度,Q6~17 為 Level 2,Q18~22 為 Level 3。大家正好利用這五道題學習、鞏固一下基礎,然後就開始準備挑戰自己吧!
前五道題
下面先給出前五道題,其他的題目大家可以在 Github 上看到~
Question 1Level 1 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
Question 2Level 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.
Question 3Level 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. 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()
Question 4Level 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
答案要點
Question 5Level 1 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:
*Solution 1* l=[] for i in range(2000, 3201): if (i%7==0) and (i%5!=0): l.append(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=values.split(",") 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 self.s.upper() strObj = InputOutString() strObj.getString() strObj.printString()
Use __init__ method to construct some parameters
更多題目可以查閱以下Github地址:
作者:zhiwehu
https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt