Python基础练习100题 ( 31~ 40)

栏目: Python · 发布时间: 6年前

内容简介:昨天和大家分享了21-30题,今天继续来刷31~40题

刷题继续

昨天和大家分享了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 3.6+,如果你用的是Python 2.7版本,绝大多数不同就体现在以下3点:

  • raw_input()在Python3中是input()
  • print需要加括号
  • fstring可以换成.format(),或者%s,%d

谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法精解

算法精解

Kyle Loudon / 肖翔、陈舸 / 机械工业出版社 / 2012-8 / 79.00元

本书是数据结构和算法领域的经典之作,十余年来,畅销不衰!全书共分为三部分:第一部分首先介绍了数据结构和算法的概念,以及使用它们的原因和意义,然后讲解了数据结构和算法中最常用的技术——指针和递归,最后还介绍了算法的分析方法,旨在为读者学习这本书打下坚实的基础;第二部分对链表、栈、队列、集合、哈希表、堆、图等常用数据结构进行了深入阐述;第三部分对排序、搜索数值计算、数据压缩、数据加密、图算法、几何算法......一起来看看 《算法精解》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具