Python如何区分高手和小白

1. 函数简化

「小白」

def add(a, b):
 sum = a + b
 return sum

「高手」

def add(a, b):
    return a + b


2. 注释

「小白」

#注1
#注2

「高手」

'''
注1
注2
'''


3. 遍历

「小白」

squared_numbers = []
for i in range(10):
    squared_numbers.append(i ** 2)

「高手」

squared_numbers = [i ** 2 for i in range(10)]


4.字符串处理

「小白」

def print_hello(name):
    print("Hello, " + name)

print_hello("Alice")
print_hello("Bob")

「高手」

def print_hello(name):
    print(f"Hello, {name}")

print_hello("Alice")
print_hello("Bob")


5. 异常处理

「小白」

def calculate_discount(price, discount):
    return price * (1 - discount / 100)

「高手」

def calculate_discount(price, discount):
    assert 0 <= discount <= 100"Discount must be between 0 and 100"
    return price * (1 - discount / 100)


原文始发于微信公众号(python学霸):Python如何区分高手和小白

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/284755.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!