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 -*-

"""
Question:
A robot moves in a plane starting from the original point (0,0).
The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps.
The trace of robot movement is shown as the following: UP 5 DOWN 3 LEFT 3 RIGHT 2 ;
The numbers after the direction are steps.
Please write a program to compute the distance from current position after a sequence of movement and original point.
If the distance is a float, then just print the nearest integer.
Example: If the following tuples are given as input to the program: UP 5 DOWN 3 LEFT 3 RIGHT 2
Then, the output of the program should be: 2
"""
'''
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.
'''

import math

temp = input('请输入机器人移动指令:')
x = 0                                       # 设置x轴方向初始参数
y = 0                                       # 设置y轴方向初始参数
t = [(i[0], int(i[1])) for i in [tuple(t.split(',')) for t in temp.split(' ')]]     # 处理输入数据为列表形式
for m in t:
    if m[0] == 'UP':                        # UP 和 DOWN 计算x轴位移
        x += m[1]
    elif m[0] == 'DOWN':
        x -= m[1]
    elif m[0] == 'LEFT':                    # LEFT 和 RIGHT 计算y轴位移
        y += m[1]
    elif m[0] == 'RIGHT':
        y -= m[1]
    else:
        pass
print("distance = ", math.floor(math.sqrt(x ** 2 + y ** 2)))    # 利用数学里的floor(向下取整)和sqrt(计算平方根)得出距离



#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-


"""
Question:
Write a program to compute the frequency of the words from the input.(计算词组输入频次)
The output should output after sorting the key alphanumerically.(经过排序后输出)
Suppose the following input is supplied to the program:
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.

Then, the output should be:
New:1 Python:5 Read:1 and:1 between:1 choosing:1 or:2 to:1
"""

'''
Hints:In case of input data being supplied to the question, it should be assumed to be a console input.
'''

freq = {}                       # 创建一个空字典
temp = input('请输入语句:')
for word in temp.split():       # 分割输入的语句并进行for循环计算词组出现的频率
    freq[word] = freq.get(word, 0) + 1      # 利用字典的内置函数get(),返回指定键的值,如果值不在字典中返回默认值0
words = sorted(freq.keys())                 # 对字典的键进行排序


for w in words:
    print('%s:%d' % (w, freq[w]))           # 输出词组出现频率的键值对



#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-


"""
Question: Write a method which can calculate square value of number
"""
'''
Hints: Using the ** operator
'''


def square_cal():  # 封装函数
    """求输入数字的平方,输入数字必须是整数"""
    a = int(input('请输入您要计算的平方数字:'))
    print(a ** 2)  # 计算平方和


square_cal()  # 调用函数



#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-

"""
Question:

Python has many built-in functions, and if you do not know how to use it,
you can read document online or find some books.
But Python has a built-in document function for every built-in functions.

Please write a program to print some Python built-in functions documents, such as abs(), int(), raw_input()

And add document for your own function
"""

'''Hints: The built-in document method is doc'''

print(abs.__doc__)
print(pow.__doc__)
print(input.__doc__)


def my_function_dox(num):
    """返回数字的平方值,输入的数字必须是整数"""  # 文档语言,说明函数的功能
    return num ** 2


print(my_function_dox(3))
print(my_function_dox.__doc__)



#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-

"""
Question: Define a class, which have a class parameter and have a same instance parameter.
"""

'''
Hints: Define a instance parameter, need add it in init method 
You can init a object with construct parameter or set the value later
'''


class Exp1:
    # 定义类参数
    name = 'person\'s'

    def __init__(self, name=None):      # 实例参数
        self.name = name


alan = Exp1('Alan')
print('%s name is %s' % (Exp1.name, alan.name))             # 第一种调用类函数的方法

mark = Exp1()                                               # 第二种调用类函数的方法
mark.name = 'Mark'
print('%s name is %s' % (Exp1.name, mark.name))



#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-

"""
Question:Define a function which can compute the sum of two numbers.
"""

'''
Hints: Define a function with two numbers as arguments. 
You can compute the sum in the function and return the value.
'''


def two_nums_sum():
    """用于计算两个数字之和"""
    temp = input('Please input two numbers:')
    num = temp.split(',')  # 分割
    a = int(num[0])  # 赋值
    b = int(num[1])
    num_sum = a + b  # 求和
    print(num_sum)  # 输出结果


two_nums_sum()  # 调用函数

# 源代码,相比于我的代码更加简洁易懂
"""def SumFunction(number1, number2):
	return number1+number2

print(SumFunction(1,2))"""



#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-

"""
Define a function that can convert a integer into a string and print it in console.
"""

'''Use str() to convert a number to string.'''


def Con_Int_To_Str(a):
    return str(a)


print(Con_Int_To_Str(6))



#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-

"""
Define a function that can receive two integral numbers in string form and compute their sum
and then print it in console.
"""

'''Use + to concatenate the strings'''


def strNumsum(a, b):
    print(int(a) + int(b))


strNumsum('4', '6')



#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-

"""
Define a function that can accept two strings as input and concatenate them and then print it in console.
"""

'''Use + to concatenate the strings'''


def strCon(a, b):
    print(a + b)        # ‘+’可以连接字符串


strCon('7', '8')



#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-

"""
Define a function that can accept two strings as input and print the string with maximum length in console.
If two strings have the same length, then the function should print all strings line by line.
"""

'''Use len() function to get the length of a string'''


def strlines(strline1, strline2):
    strline1 = str(strline1)  # 字符串形式
    strline2 = str(strline2)
    if len(strline1) > len(strline2):  # 判定字符串长度
        print(strline1)
    elif len(strline1) < len(strline2):
        print(strline2)
    else:
        print(strline1, strline2)


strlines('asfefwrvsqdqef', 'wefgrgerqgrgf')  # 调用函数

第三期先到这儿,共十期。

“自主学习,快乐学习”

再附上Python常用标准库供大家学习使用:
Python一些常用标准库解释文章集合索引(方便翻看)

“学海无涯苦作舟”