摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载题我的运行环境如果你
刷题继续
昨天和大家分享了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
谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/43945.html
摘要:刷题继续大家好,我又回来了,昨天和大家分享了题,今天继续来看题解法一解法二解法一解法二解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载题 刷题继续 大家好,我又回来了,昨天和大家分享了31-40题,今天继续来看41~50题 Question 41: Write a program whi...
摘要:笨办法学第版结构非常简单,共包括个习题,其中个覆盖了输入输出变量和函数三个主题,另外个覆盖了一些比较高级的话题,如条件判断循环类和对象代码测试及项目的实现等。最后只想说,学习不会辜负任何人,笨办法学 内容简介 《笨办法学Python(第3版)》是一本Python入门书籍,适合对计...
摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载题我的运 刷题继续 昨天和大家分享了71-80题,今天继续来刷81~90题 Question 81: By using list comprehension, p...
摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法二解法一解法一解法二解法一解法一解法二解法一解法一解法二解法一解法二解法一解法二解法三解法一解法二源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下 刷题继续 昨天和大家分享了61-70题,今天继续来刷71~80题 Question 71: Please write a program to out...
摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载 刷题继续 昨天和大家分享了41-50题,今天继续来刷51~60题 Question 51: Write a function to compute 5/0 and use ...
阅读 2189·2021-11-22 09:34
阅读 1308·2021-10-11 10:59
阅读 4372·2021-09-22 15:56
阅读 3105·2021-09-22 15:08
阅读 3380·2019-08-30 14:01
阅读 746·2019-08-30 11:16
阅读 1112·2019-08-26 13:51
阅读 2850·2019-08-26 13:43