• 分享
  • 25.1.13 集训 —— 劳老师: PEP8 / 文档 / debug / 函数 / 周赛

  • @ 2025-1-15 10:09:37

主讲人: 劳老师

时间: 2025.01.14

地点: B411机房

PEP 8 规范

'''
PEP 规范(部分)
语法结构
1. 类和函数定义后空两行
2. 注释(#)前空2格,后空1格
3. 赋值符号(=)、大多数运算符左右两侧各1个空格
4. 字典的冒号符号前不空, 后有1个空格

命名
1. 变量名、函数名以小写字母、下划线分隔, 如:student_names / is_prime()
2. 常量使用全大写字母, 如: PI / MAX_N
3. 类以驼峰法命名, 如: StudentLibrary / NameArray


'''
def object_repeater(x):
	return x  # 这里是一个PEP8规范的注释

PI = 3.1415926
student_num = 5
ch_en_dict = {1: 'one', 2: 'two', 3: 'three'}

python 官方文档

在线帮助

官方文档地址: https://docs.python.org/zh-cn/3.13/

python解释器地址: https://www.python.org/downloads/

pycharm社区版地址: https://www.jetbrains.com/pycharm/download

官方文档 - 中文

搜索type的结果

离线帮助

print(help('list'))  # 输出 list 类的帮助文档
print(help('turtle'))  # 输出 turtle 模块的帮助文档

DEBUG 调试

def f(x):
    return True if x % 2 == 0 else False

lst = [1, 2, 3, 4, 5, 6]
print(lst)

for i in range(len(lst)):  # for(int i = 0; i < 6; i ++)
    print(f(lst[i]))

调试步骤:

  1. 选择希望运行暂停的程序位置(通常打在报错行上);
  2. 在左侧打上断点;
  3. 右键debug '文件名';
  4. 观察底部窗口中的各变量名, 也可以追加想观察的表达式;
  5. concole 中观察当前的输出内容, 输入代码进行交互调试;
  6. 点击 step over 可以逐行运行接下去的程序。

函数

函数定义

算法:输出性

编写的四种方式

  • 无输入无返回值
  • 有输入无返回值
  • 无输入有返回值
  • 有输入有返回值

无输入无返回值

无返回值看似毫无意义, 但是依然可以有输出, 如: 打印文本 / 生成一个文件

def welcome() -> None:
	print('欢迎你来到嘉善技师学院')

welcome()
def reduceBasicfile() -> None:
    with open('./README.md', 'w') as file:  
        file.write('FILENAME: README.md')  
  
reduceBasicfile()	

有输入无返回值

有输入, 意味着我们可以让函数对我们的数据进行操作, 无返回值意味着我们大多数情况下可以将结果打印或写入到文件中, 特殊情况是传入的是可变对象, 即: list dict set 我们可以对传入的可变对象直接修改。

def addNum(lst: list[int]) -> None:  
    maxValue = max(lst)  
    lst.append(maxValue + 1)  
  
lst = [1, 3, 2]  
addNum(lst)  
print(lst)

无输入有返回值

无输入有返回值, 非常适合产生一些随机数。

import random  
def reduceRandom10Num():  
    return [random.randint(1, 1000) for _ in range(10)]  

print(reduceRandom10Num())

有输入有返回值

功能最强大、使用最普遍的方式, 有输入也有输出。多做一些操作: 判别某个对象的特性后返回 TrueFalse; 多某个对象做一些操作, 返回一些相关的值......

# def is_prime(num: int) -> bool:

def create_student(name, class_num, gender = '', description = ''):

    print(f'name: {name}\nclass: {class_num}\ngender: {gender}\ndescription: {description}')

create_student('于思言', '2430', '男', '我想写代码')

周赛

Q1038:『三角如山 峰顶自现』

edges = [int(x) for x in input().split()]
edges.sort()
a, b, c = edges
if a + b > c:
    if a * a + b * b == c * c:
        print('直角三角形')
    elif a * a + b * b > c * c:
        print('锐角三角形')
    elif a * a + b * b < c * c:
        print('钝角三角形')
    if a == b:
        print('等腰三角形')
    if b == c:
        print('等边三角形')
else:
    print('无法围成三角形')

Q1039: 『圆润如水 圆中藏天』

n = int(input())
count = 0
for i in range(-n-2, n + 2):
    for j in range(-n-2, n+2):
        if i * i + j * j <= n * n:
            count += 1
print(count)

Q1040: 『方正如石 四角皆安』

def is_exist(lst) -> bool:
    for i in range(0, 2):
        for j in range(0, 2):
            if lst[i][j] == lst[i+1][j] == lst[i][j+1] == lst[i+1][j+1]:
                return True
    return False

lst = []
for i in range(3):
    lst.append(list(input()))
if is_exist(lst):
    print('Exist')
else:
    flag = False
    for i in range(0, 3):
        for j in range(0, 3):
            lst[i][j] = 'B' if lst[i][j] == 'W' else 'W'
            if is_exist(lst):
                print(f'True {i} {j}')
                flag = True
                break
            lst[i][j] = 'B' if lst[i][j] == 'W' else 'W'
        if flag == True:
            break
    if flag == False:
        black = 0
        for i in range(3):
            for j in range(3):
                if lst[i][j] == 'B':
                    black += 1
        white = 9 - black
        print(f'False {white} {black}')

Q1041: 『菱形似星 心中有光』

n = int(input())
lst = []
for i in range(n // 2):
    s = ' ' * i + '*' * (n - 1 - 2 * i)
    lst.append(s)
    print(s)
for s in lst[::-1]:
    print(s)

集训剪影

0 条评论

目前还没有评论...