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 # linux环境运行必须写上
# -*- coding:UTF-8 -*- # 写一个特殊的注释来表明Python源代码文件是unicode格式的
\"\"\"Question:
Write a program which will find all such numbers
which are divisible by 7 but are not a multiple of 5,
between 2000 and 3200 (both included).
The numbers obtained should be printed
in a comma-separated sequence on a single line.\"\"\"
\'\'\'Hints: Consider use range(#begin, #end) method\'\'\'
l1 = [] # 建立空列表
for i in range(2000, 3201): # for循环寻找能被7整除但不能被5整除的数字
if (i % 7 == 0) and (i % 5 != 0):
l1.append(str(i)) # 列表里的数据需要是字符串
print(\',\'.join(l1)) # 在一行显示,以逗号隔开
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
\"\"\"
Question:
Write a program which can compute the factorial of a given numbers.
The results should be printed in a comma-separated sequence on a single line.
Suppose the following input is supplied to the program: 8 Then, the output should be: 40320
\"\"\"
\'\'\'Hints: In case of input data being supplied to the question, it should be assumed to be a console input.\'\'\'
i = 1
result = 1
t = int(input(\'请输入一个数字: \'))
while i <= t: # while循环
result = result * i # 实现阶乘
i += 1
print(result)
# 源代码
\'\'\'
def fact(x):
if x == 0:
return 1
return x * fact(x - 1)
x=int(input(\'请输入一个数字: \'))
print(fact(x))
\'\'\'
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
\"\"\"
Question:
With a given integral number n,
write a program to generate a dictionary
that contains (i, i*i) such that is an integral number between 1 and n (both included).
and then the program should print the dictionary.
Suppose the following input is supplied to the program: 8
Then, the output should be: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
\"\"\"
\'\'\'
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.
Consider use dict()
\'\'\'
m = int(input(\'请输入一个数字1~n:\'))
n = {} # 创建一个空字典
for i in range(1, m+1):
n[i] = (i * i) # 将键值对存入空字典
i += 1
print(n)
# 源代码
\'\'\'
n=int(input())
d=dict()
for i in range(1,n+1):
d[i]=i*i
print(d)
\'\'\'
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
\"\"\"
Question:
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple
which contains every number.
Suppose the following input is supplied to the program: 34,67,55,33,12,98
Then, the output should be: [\'34\', \'67\', \'55\', \'33\', \'12\', \'98\'] (\'34\', \'67\', \'55\', \'33\', \'12\', \'98\')
\"\"\"
\'\'\'
Hints: In case of input data being supplied to the question,
it should be assumed to be a console input.
tuple() method can convert list to tuple
\'\'\'
import re
t = input(\'请输入数据:\')
d = t.split(\',\') # split()函数按照所给参数分割序列
k = re.findall(r\'\\d+\', t) # findall()函数在字符串中找到正则表达式所匹配的所有子串,并组成一个列表返回
# \\d 相当于[0-9],匹配0-9数字
r = tuple(k) # 转换为元组类型
print(k)
print(r)
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
\"\"\"
Question:
Define a class which has at least two methods:
getString: to get a string from console
input printString: to print the string in upper case.
Also please include simple test function to test the class methods.
\"\"\"
\'\'\'
Hints: Use init method to construct some parameters
\'\'\'
class InputOutputString(): # 创建类函数
def __init__(self):
self.s = \'\'
def __getString__(self):
self.s = input(\'Please input what you want:\')
def __printString__(self):
print(self.s.upper()) # upper()函数使字符串字母变为大写
stringRan = InputOutputString()
stringRan.__getString__()
stringRan.__printString__()
#!usr\\bin\\env Python3
# encoding:UTF-8
\"\"\"
Question:
Write a program that calculates and prints the value according to the given formula:
Q = Square root of [(2 * C * D)/H]
Following are the fixed values of C and H: C is 50. H is 30.
D is the variable whose values should be input to your program in a comma-separated sequence.
Example Let us assume the following comma separated input sequence is given to the program: 100,150,180
The output of the program should be: 18,22,24
\"\"\"
\'\'\'
If the output received is in decimal form,
it should be rounded off to its nearest value
(for example, if the output received is 26.0, it should be printed as 26)
In case of input data being supplied to the question, it should be assumed to be a console input.
\'\'\'
import math
C = 50
H = 30
value = [] # 创建空列表
t = input(\'Please input your values, example:34, 23, 180, ... :\')
x = [i for i in t.split(\',\')] # 列表推导式
for d in x:
value.append(str(round(math.sqrt((2 * C * float(d)) / H)))) # round()函数实现四舍五入,sqrt表示平方
print(\',\'.join(value))
#!usr\\bin\\env Python3
# encoding:UTF-8
\"\"\"
Question:
Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array.
The element value in the i-th row and j-th column of the array should be i*j.
Note: i=0,1.., X-1; j=0,1,¡..., Y-1.
Example Suppose the following inputs are given to the program: 3,5
Then, the output of the program should be: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
\"\"\"
\'\'\'
Note: In case of input data being supplied to the question,
it should be assumed to be a console input in a comma-separated form.
\'\'\'
input_String = input(\'Please input 2 digits, example:2,3.:\')
dimensions = [int(x) for x in input_String.split(\',\')] # 表示数组行数和列数
rowNum = dimensions[0] # 赋予行数
colNum = dimensions[1] # 赋予列数
multi_list = [[0 for col in range(colNum)] for row in range(rowNum)] # 创建一个空数组列表
for col in range(colNum):
for row in range(rowNum):
multi_list[row][col] = col * row # 向空数组填入数值
print(multi_list)
#!usr\\bin\\env Python3
# encoding:UTF-8
\"\"\"
Question:
Write a program that accepts a comma separated sequence of words as input
and prints the words in a comma-separated sequence after sorting them alphabetically.
Suppose the following input is supplied to the program: without,hello,bag,world
Then, the output should be: bag,hello,without,world
\"\"\"
\'\'\'
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.
\'\'\'
temp = input(\'Please input your words:\')
inputStr = temp.split(\',\') # 按“, ”分隔输入的单词
inputStr.sort() # 按字母顺序进行排列
print(\',\'.join(inputStr)) # 输出按字母顺序排列的单词,单词之间用逗号分隔
# 源代码
\"\"\"
items=[x for x in input().split(\',\')]
items.sort()
print(\',\'.join(items))
\"\"\"
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
\"\"\"
Question:
Write a program that accepts sequence of lines as input
and prints the lines after making all characters in the sentence capitalized.
Suppose the following input is supplied to the program: Hello world Practice makes perfect
Then, the output should be: HELLO WORLD PRACTICE MAKES PERFECT
\"\"\"
\'\'\'
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.
\'\'\'
lines = []
while True:
s = input(\'Please input your sentences:\')
if s:
lines.append(s.upper()) # 将大写过后的句子放进列表里,可以放进去多个句子
else:
break
for sentence in lines: # 打印每一句句子
print(sentence)
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
\"\"\"
Question:
Write a program that accepts a sequence of whitespace separated words as input
and prints the words after removing all duplicate words and sorting them alphanumerically.
Suppose the following input is supplied to the program: hello world and practice makes perfect and hello world again
Then, the output should be: again and hello makes perfect practice world
\"\"\"
\'\'\'
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.
We use set container to remove duplicated data automatically and then use sorted() to sort the data.
\'\'\'
temp = input(\'Please input your words: \') # 输入想说的话
words = [x for x in temp.split(\' \')] # 按照空格分割,把单词放进列表
print(\' \'.join(sorted(list(set(words))))) # 用set()函数创建一个无序的、不重复(可利用这一点删除重复元素)
# 元素集,并可进行与或等运算。经过排序后再用空格分隔装进列表打印出来
再附上Python常用标准库供大家学习使用:
Python一些常用标准库解释文章集合索引(方便翻看)
“学海无涯”
来源:https://www.cnblogs.com/qin1999/p/Python-programming-exercises-1.html
本站部分图文来源于网络,如有侵权请联系删除。