摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題我的運(yùn)行環(huán)境如果你
刷題繼續(xù)
昨天和大家分享了21-30題,今天繼續(xù)來(lái)刷31~40題
Question 31: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.
def printDict(): d=dict() for i in range(1,21): d[i]=i**2 print(d) printDict()解法二
def printDict(): dict={i:i**2 for i in range(1,21)} print(dict) printDict()Question 32:
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.
def printDict(): dict = {i: i**2 for i in range(1, 21)} print(dict.keys()) printDict()解法二
def printDict(): d=dict() for i in range(1,21): d[i]=i**2 for k in d.keys(): print(k) printDict()Question 33:
Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).
def printList(): lst = [i ** 2 for i in range(1, 21)] print(lst) printList()Question 34:
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.
def printList(): lst = [i ** 2 for i in range(1, 21)] print(lst[:5]) printList()Question 35:
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.
def printList(): lst = [i ** 2 for i in range(1, 21)] print(lst[-5:]) printList()Question 36:
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.
def printList(): lst = [i ** 2 for i in range(1, 21)] print(lst[5:]) printList()Question 37:
Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included).
def printTuple(): lst = [i ** 2 for i in range(1, 21)] print(tuple(lst)) printTuple()Question 38:
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.
tpl = (1,2,3,4,5,6,7,8,9,10) for i in range(0,5): print(tpl[i],end = " ") print() for i in range(5,10): print(tpl[i],end = " ")解法二
tp = tuple(i for i in range(1,11)) lst1,lst2 = list(tp[:5]),list(tp[5:]) print(lst1) print(lst2)Question 39:
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).
tpl = (1,2,3,4,5,6,7,8,9,10) tpl_even = tuple(i for i in tpl if i%2 == 0) print(tpl_even)解法二
tpl = (1,2,3,4,5,6,7,8,9,10) tpl_even= tuple(filter(lambda x : x%2==0,tpl)) print(tpl_even)Question 40:
Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No".
s = input() if s.lower() == "yes": print("Yes") else: print("No")源代碼下載
這十道題的代碼在我的github上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載:
Python 31-40題
我的運(yùn)行環(huán)境Python 3.6+,如果你用的是Python 2.7版本,絕大多數(shù)不同就體現(xiàn)在以下3點(diǎn):
raw_input()在Python3中是input()
print需要加括號(hào)
fstring可以換成.format(),或者%s,%d
謝謝大家,我們下期見!希望各位朋友不要吝嗇,把每道題的更高效的解法寫在評(píng)論里,我們一起進(jìn)步!?。?/p>
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/43945.html
摘要:刷題繼續(xù)大家好,我又回來(lái)了,昨天和大家分享了題,今天繼續(xù)來(lái)看題解法一解法二解法一解法二解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題 刷題繼續(xù) 大家好,我又回來(lái)了,昨天和大家分享了31-40題,今天繼續(xù)來(lái)看41~50題 Question 41: Write a program whi...
摘要:笨辦法學(xué)第版結(jié)構(gòu)非常簡(jiǎn)單,共包括個(gè)習(xí)題,其中個(gè)覆蓋了輸入輸出變量和函數(shù)三個(gè)主題,另外個(gè)覆蓋了一些比較高級(jí)的話題,如條件判斷循環(huán)類和對(duì)象代碼測(cè)試及項(xiàng)目的實(shí)現(xiàn)等。最后只想說,學(xué)習(xí)不會(huì)辜負(fù)任何人,笨辦法學(xué) 內(nèi)容簡(jiǎn)介 《笨辦法學(xué)Python(第3版)》是一本Python入門書籍,適合對(duì)計(jì)...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題我的運(yùn) 刷題繼續(xù) 昨天和大家分享了71-80題,今天繼續(xù)來(lái)刷81~90題 Question 81: By using list comprehension, p...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法二解法一解法一解法二解法一解法一解法二解法一解法一解法二解法一解法二解法一解法二解法三解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下 刷題繼續(xù) 昨天和大家分享了61-70題,今天繼續(xù)來(lái)刷71~80題 Question 71: Please write a program to out...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載 刷題繼續(xù) 昨天和大家分享了41-50題,今天繼續(xù)來(lái)刷51~60題 Question 51: Write a function to compute 5/0 and use ...
閱讀 2277·2021-11-22 09:34
閱讀 1411·2021-10-11 10:59
閱讀 4524·2021-09-22 15:56
閱讀 3427·2021-09-22 15:08
閱讀 3470·2019-08-30 14:01
閱讀 851·2019-08-30 11:16
閱讀 1181·2019-08-26 13:51
閱讀 2968·2019-08-26 13:43