# 刷题继续

### 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")```

# 源代码下载

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

0 条评论

• ### Python进阶之Matplotlib入门(九)

Matplotlib是Python的画图领域使用最广泛的绘图库，它能让使用者很轻松地将数据图形化以及利用它可以画出许多高质量的图像，是用Python画图的必备技...

• ### 许嵩粉丝地域分布热力图，看看你的爱豆受到哪些地方的人喜欢

这次我们想绘制一个歌手许嵩的粉丝地域分布的热力图。我们通过对网易云音乐网站爬虫，获取了所有粉丝的信息，包括所在地区。然后将这些地区按照关注人数通过热力图的形式，...

• ### 如何在Windows上使用Python，看看微软的官方教程

随着Python持续火热状态，微软开始重视Python在Windows系统上的运行，于是它出品了一个官方教程。这个教程包含了入门，web开发，自动化和脚本，数据...

• ### PHP 到底是不是宇宙第一？TIOBE 排行榜来证明！

做为一名程序员，都比较关注其使用编程语言的热度，一方面编程语言的热度决定了它拥有多大的市场，另一方面也关系到行业内程序员选择机会有多大。

• ### Python SDK入门(1)——让NAO说话

在python for naoqi的环境安装完成后，我们先来尝试让NAO说出“Hello world”：

• ### Vue脚手架搭建项目中的坑

Vue 框架如同 Python 中的 Django 框架一样，让一切操作变得超级简单，只需在规定的目录下书写规定的代码即可，至于如何运作，框架自己搞定。此篇文章...

• ### Python之前准备工作，轻松安装好这些软件就可以开始入门了

Python作为一个目前最火的编程语言之一，已经渗透到了各行各业，笔者所在的人工智能科研领域就以Python为主要编程语言，尤其是神经网络几乎已经离不开Pyth...

• ### 当Excel不够用的时候如何用Python救场？

Excel是很多公司非常流行的工具，数据分析师和数据科学家经常发现他们把它作为数据分析和可视化工具的一部分，但这并不总是最好的选择。

• ### 使用Python『秒开』100GB+数据！

第二种使用分布式计算：虽然在某些情况下这是一种有效的方法，但是它带来了管理和维护集群的巨大开销。想象一下，必须为一个刚好超出RAM范围的数据集设置一个集群，比如...

• ### [必备工具]Python可视化绘图库Altair

Altair is a declarative statistical visualization library for Python, based on V...