国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Python基礎練習100題 ( 31~ 40)

miracledan / 2129人閱讀

摘要:刷題繼續昨天和大家分享了題,今天繼續來刷題解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結果,可以點擊以下鏈接下載題我的運行環境如果你

刷題繼續

昨天和大家分享了21-30題,今天繼續來刷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上,如果大家想看一下每道題的輸出結果,可以點擊以下鏈接下載:

Python 31-40題

我的運行環境Python 3.6+,如果你用的是Python 2.7版本,絕大多數不同就體現在以下3點:

raw_input()在Python3中是input()

print需要加括號

fstring可以換成.format(),或者%s,%d

謝謝大家,我們下期見!希望各位朋友不要吝嗇,把每道題的更高效的解法寫在評論里,我們一起進步!!!

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/43945.html

相關文章

  • Python基礎練習100 ( 41~ 50)

    摘要:刷題繼續大家好,我又回來了,昨天和大家分享了題,今天繼續來看題解法一解法二解法一解法二解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結果,可以點擊以下鏈接下載題 刷題繼續 大家好,我又回來了,昨天和大家分享了31-40題,今天繼續來看41~50題 Question 41: Write a program whi...

    mochixuan 評論0 收藏0
  • 測試開發必看:《笨辦法學Python3》PDF中文高清版,豆瓣高分8.0

    摘要:笨辦法學第版結構非常簡單,共包括個習題,其中個覆蓋了輸入輸出變量和函數三個主題,另外個覆蓋了一些比較高級的話題,如條件判斷循環類和對象代碼測試及項目的實現等。最后只想說,學習不會辜負任何人,笨辦法學 內容簡介   《笨辦法學Python(第3版)》是一本Python入門書籍,適合對計...

    不知名網友 評論0 收藏0
  • Python基礎練習100 ( 81~ 90)

    摘要:刷題繼續昨天和大家分享了題,今天繼續來刷題解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結果,可以點擊以下鏈接下載題我的運 刷題繼續 昨天和大家分享了71-80題,今天繼續來刷81~90題 Question 81: By using list comprehension, p...

    劉德剛 評論0 收藏0
  • Python基礎練習100 ( 71~ 80)

    摘要:刷題繼續昨天和大家分享了題,今天繼續來刷題解法一解法二解法一解法一解法二解法一解法一解法二解法一解法一解法二解法一解法二解法一解法二解法三解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結果,可以點擊以下鏈接下 刷題繼續 昨天和大家分享了61-70題,今天繼續來刷71~80題 Question 71: Please write a program to out...

    Jeff 評論0 收藏0
  • Python基礎練習100 ( 51~ 60)

    摘要:刷題繼續昨天和大家分享了題,今天繼續來刷題解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結果,可以點擊以下鏈接下載 刷題繼續 昨天和大家分享了41-50題,今天繼續來刷51~60題 Question 51: Write a function to compute 5/0 and use ...

    岳光 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<