python编程100题31—40(第四天)
自学python提高编程能力(第四天)
以下为python编程100题,来自github,原文链接如下:
https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt
文章的最后是一些知识点总结和提示
完成这些题目的基础上,进行了一些题目要求的翻译理解;添加了自己理解的注释和出过的错误,请同样学习的朋友们指正!
QUESTION31
'''
Question:
Define a function that can accept an integer number as input and
print the "It is an even number" if the number is even, otherwise print "It is an odd number".
Hints:
Use % operator to check if a number is even or odd.
'''
def checknumber(n):
if n%2==0:
yield "this is an even number"
else:
yield "this is an odd number"
print(next(checknumber(3)))
QUESTION32
'''
Question:
Define a function which can print a dictionary where
the keys are numbers between 1 and 3 (both included) and the values are square of keys.
Hints:
Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
'''
def printdic():
d=dict()
for n in range(1,4):
d[n]=n**2
print(d)
printdic()
QUESTION33
'''
Question:
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.
Hints:
Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
Use range() for loops.
'''
def printdic(a,b):
d=dict()
for n in range(a,b+1):
d[n]=n**2
print(d)
printdic(1,20)
QUESTION34
'''
Question:
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 values only.
Hints:
Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
Use range() for loops.
Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
'''
def printdic(a,b):
d=dict()
for n in range(a,b+1):
d[n]=n**2
for (s,v) in d.items():#在字典中,可以认为其中元素都是(s,v)的形式
print(v)
printdic(1,20)
QUESTION35
'''
Question:
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.
Hints:
Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
Use range() for loops.
Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
'''
def printdic(a,b):
d=dict()
for n in range(a,b+1):
d[n]=n**2
for s in d.keys():#在字典中,可以认为其中元素都是(s,v)的形式
print(s)
printdic(1,20)
QUESTION36
'''
Question:
Define a function which can generate and print a list
where the values are square of numbers between 1 and 20 (both included).
Hints:
Use ** operator to get power of a number.
Use range() for loops.
Use list.append() to add values into a list.
'''
def listprint(a,b):
li=list()
for n in range(a,b+1):
li.append(n*n)
print(li)
listprint(1,20)
QUESTION37
'''
Question:
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.
Hints:
Use ** operator to get power of a number.
Use range() for loops.
Use list.append() to add values into a list.
Use [n1:n2] to slice a list
'''
def cutprint(a,b,c,d):
li=list()
for n in range(a,b+1):
li.append(n**2)
li_out=li[c-1:d-1] #这里可以直接写成:li[:5]
print(li_out)
cutprint(1,20,1,5)
QUESTION38
'''
Question:
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.
Hints:
Use ** operator to get power of a number.
Use range() for loops.
Use list.append() to add values into a list.
Use [n1:n2] to slice a list
'''
def lastcutprint(a,b,c):
li=[]
for n in range(a,b+1):
li.append(n**2)
print(li[-5:])#要切出后五个,那么直接用[-5:]
lastcutprint(1,20,5)
QUESTION39
'''
Question:
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.
Hints:
Use ** operator to get power of a number.
Use range() for loops.
Use list.append() to add values into a list.
Use [n1:n2] to slice a list
'''
def printf(a,b,c):
li=list()
for n in range(a,b+1):
li.append(n**2)
print(li[c:])
printf(1,20,5)
QUESTION40
'''
Question:
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.
Hints:
Use [n1:n2] notation to get a slice from a tuple.
'''
a=(1,2,3,4,5,6,7,8,9,10)
a1=a[:5]
a2=a[5:]
print(a1)
print(a2)
这十道题的难度都比较低,主要是针对python比较具有特色的列表、元组切片操作,其中需要注意的即是:
1.列表、元组中元素的下标是从0开始的,如一个10个元素的元组a,第一个元素是a[0],最后一个元素是a[9]
2.列表、元组的切片操作,a[x,y],所切取的元素是从下标x,一直截取到下表y-1;
列表切片举例
>>>a=[0,1,2,3,4,5,6,7]
>>>a[0:8]
[0, 1, 2, 3, 4, 5, 6, 7]
>>>a[1:2]
[1]
>>>a[3:7]
[3, 4, 5, 6]
3.字典操作中,.items()得到的是字典中所有元素组成的(key,value)形式,.keys()得到的是所有的key元素。