1.此为GitHub项目的学习记录,记录着我的思考,代码基本都有注释。 2.可以作为Python初学者巩固基础的绝佳练习,原题有些不妥的地方我也做了一些修正。 3.建议大家进行Python编程时使用英语。 4.6~17题为level1难度,18-22题为level3难度,其余都为level1难度。 项目名称:100+ Python challenging programming exercises for Python 3
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
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".
"""
'''Use % operator to check if a number is even or odd.'''
def checkNumber(a):
a = int(a)
if a % 2 == 0:
print('It is a even number.')
elif a % 2 != 0:
print('It is an odd number.')
else:
pass
checkNumber(7)
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
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 dicprac(a,b):
"""打印一个字典,键为1个整数,值为键所对应整数的平方"""
a = int(a)
b = int(b)
values = {} # 创建空字典
for i in range(a, b):
values[i] = i ** 2 # 创建键值对
print(values)
dicprac(1, 21)
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
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.
"""
'''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 dicprac2(a, b):
"""只打印一个字典的值,键为1个整数,值为键所对应整数的平方"""
a = int(a)
b = int(b)
values = {} # 创建空字典
for i in range(a, b):
values[i] = i ** 2 # 创建键值对
# print(values[i]) # 方法1:索引,只打印值
# for (k, v) in values.items(): # 方法2:用items(),只打印值
# print(v)
for k in values.keys(): # 用keys(),只打印键
print(k)
dicprac2(1, 21)
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
Define a function which can generate and print a list
where the values are square of numbers between 1 and 20 (both included).
"""
'''Use ** operator to get power of a number.
Use range() for loops.
Use list.append() to add values into a list.'''
def listprac(a, b):
"""打印一个列表,列表里的值为所给范围数字的平方"""
a = int(a)
b = int(b)
values = [] # 创建一个空列表
for i in range(a, b): # for循环添加值
values.append(i ** 2) # 值为平方
print(values)
listprac(1, 21) # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
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 listprac(a, b):
"""打印一个列表,列表里的值为所给范围数字的平方的前五个"""
a = int(a)
b = int(b)
values = [] # 创建一个空列表
for i in range(a, b): # for循环添加值
values.append(i ** 2) # 值为平方
print(values[0:5]) # 切片,打印前五个数
listprac(1, 21) # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
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 listprac(a, b):
"""打印一个列表,列表里的值为所给范围数字的平方的最后五个"""
a = int(a)
b = int(b)
values = [] # 创建一个空列表
for i in range(a, b): # for循环添加值
values.append(i ** 2) # 值为平方
print(values[-5:]) # 切片,打印最后五个数
listprac(1, 21) # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
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 listprac(a, b):
"""打印一个列表,列表里的值为除了所给范围数字的平方的前五个的其他所有值"""
a = int(a)
b = int(b)
values = [] # 创建一个空列表
for i in range(a, b): # for循环添加值
values.append(i ** 2) # 值为平方
print(values[5:]) # 切片,打印除前五个数之外的其他所有数
listprac(1, 21) # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
Define a function which can generate and print a tuple
where the value 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.
Use tuple() to get a tuple from a list.
'''
def tupleprac(a, b):
"""打印一个元组,元组里的值为除了所给范围数字的平方"""
a = int(a)
b = int(b)
values = [] # 创建一个空列表
for i in range(a, b): # for循环添加值
values.append(i ** 2) # 值为平方
print(tuple(values)) # 将列表转为元组并打印
tupleprac(1, 21) # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
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.
'''
def tupleprac(a, b):
"""打印一个元组,元组里的值为除了所给范围数字的平方"""
a = int(a)
b = int(b)
values = [] # 创建一个空列表
for i in range(a, b): # for循环添加值
values.append(i ** 2) # 值为平方
# print(tuple(values)) # 将列表转为元组并打印
print(tuple(values[0:10])) # 打印元组的前一半
print(tuple(values[11:20])) # 打印元组的后一半
tupleprac(1, 21) # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
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).
"""
'''
Hints:
Use "for" to iterate the tuple Use tuple() to generate a tuple from a list.
'''
list_practice = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # 先将这些数字放进列表里
print(tuple(list_practice)) # 转换为元组
even_nums = [] # 创建一个空列表
for i in list_practice: # for循环找到列表里的偶数
if i % 2 == 0:
even_nums.append(i) # 将找到的偶数加进列表里
print(tuple(even_nums)) # 转换为元组打印出来
# 源代码
"""tp = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
li = list()
for i in tp:
if tp[i] % 2 == 0:
li.append(tp[i])
tp2 = tuple(li)
print(tp2)"""