昨天和你们分享了21-30题,今天继续来刷31~40题git
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()
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()
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()
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()
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()
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()
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()
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)
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)
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上,若是你们想看一下每道题的输出结果,能够点击如下连接下载:github
个人运行环境Python 3.6+,若是你用的是Python 2.7版本,绝大多数不一样就体如今如下3点:code
谢谢你们,咱们下期见!但愿各位朋友不要吝啬,把每道题的更高效的解法写在评论里,咱们一块儿进步!!!orm