泸州网站建设厦门网络推广哪家强
B站|公众号:啥都会一点的研究生
相关阅读
整理了几个100%会踩的Python细节坑,提前防止脑血栓
整理了十个100%提高效率的Python编程技巧,更上一层楼
Python-列表,从基础到进阶用法大总结,进来查漏补缺
Python-元组,从基础到进阶用法大总结及与列表核心区别,进来查漏补缺
Python-字典,从基础到进阶用法大总结,进来查漏补缺
Python-集合,从基础到进阶大总结,进来查漏补缺
订阅专栏 ===> Python
首先第一个例子,很多同学都有这个习惯,包括我自己很长一段时间都用这种方式,难看且繁琐
def case_1(name, score):if score > 60.0:print("Congratulations " + name + "! your score is " + str(score))else:print("Sorry " + name + ", you didn't pass the exam")
但其实,使用f-string能简化编写方式,提升效率
def case_1(name, score):if score > 60.0:print(f"Congratulations {name}! your score is {score}")else:print(f"Sorry {name}, you didn't pass the exam")
第二个,有的同学,可能知道Python中None,False,0,空列表,空元组,空集合,空字典,空钱包等都表否定,如这个例子,虽然没错,但在工程上代码长、变量多的时候可读性将变差
def case_2(lst):my_list = []if lst:my_list = lst[:]else:my_list.append(1)
最好规范编写
def case_2(lst):my_list = []if len(my_list) > 0:my_list = lst[:]else:my_list.append(1)
说到这个,在Python中使用if
结合bool(x)
或len(x)
或其他内置函数,等效于if x
,但还是得综合考虑可读性
def case_3(x):if x:passif bool(x):passif len(x):pass
这个例子很简单,但初学者确实容易混淆的用法,想实现x的y次幂,下意识地就会写成这样
def case_4(x, y):return x ^ y
但其实这是按位异或,正确的应该这样
def case_4(x, y):return x ** y
下一个,当需要对文件进行读写时,很多人习惯写成这样
def case_5(filepath):f = open(filepath, "w")f.write("Hello world\n") # heref.close()
但不知道的是,当写入发生错误时,该文件永远不会被关闭,正确的做法是使用with
,即使发生了异常也能正确关闭
def case_5(filepath):with open(filepath) as f:f.write("Hello world\n")
下一个,当程序中涉及数值很大的变量,通常你可能写成如下形式
def case_6():x = 10000000
但其实会影响阅读,而若使用这种方式会变得简洁明了
def case_6():x = 10_000_000
下一个,很多人喜欢用print来debug程序
def case_7():print("debug info")print("normal info")print("error info")
其实使用logging打印日志更加清晰,可以很明显的看到程序输出,且还能保存在本地查阅,虽然我自己很多时候也是print,确实方便
import loggingdef case_8():logging.debug("debug info")logging.info("normal info")logging.error("error info")def main():level = logging.DEBUGfmt = "[%(level)s] %(asctime)s - %(message)s"logging.basicConfig(level=level, format=fmt)case_8()>>> [DEBUG] 2022-10-23 14:54:55,763 - debug info
[INFO] 2022-10-23 14:54:55,763 - normal info
[ERROR] 2022-10-23 14:54:55,763 - error info
下一个,当定义函数时,若存在可变类型参数,尤其需要小心
def case_9(n, lst=[]):lst.append(n)return lstlst1 = case_9(0) # [0]
lst2 = case_9(1) # [0, 1]
因为参数的默认值是在定义函数时定义的,而不是运行时,在该例中,每一次的调用都共享同一个列表,所以除开第一次外,之后每次调用都是从前面的结果开始添加数据,可以稍作改变达到目的
def case_9(n, lst=None):if lst is None:lst = []lst.append(n)return lstlst1 = case_9(0) # [0]
lst2 = case_9(1) # [1]
说到这个可变类型,球球一定注意当你把列表、字典这种传给函数时稍不注意就会改动原始数据
def case_10(lst):lst2 = lstlst2.append(1)init_list = [0, 1, 2]case_10(init_list) # init_list : [0, 1, 2, 1]
此时需要对其进行拷贝,构造新的对象
def case_10(lst):lst2 = lst[:] # copy() deepcopy()lst2.append(1)init_list = [0, 1, 2]case_10(init_list) # init_list : [0, 1, 2]
当遍历字典时,默认是遍历键,这两种方式其实是相同的
def case_11():d = {"a": 1, "b": 2, "c": 3}for key in d:passfor key in d.keys():pass
关于推导式,不仅适用于列表,而元组、字典、集合均可以使用其各自的推导式
def case_12():data = [0, 1, 2, 3]d = {i: i * i for i in data}l = [i * i for i in data]s = {i * i for i in data}t = (i * i for i in data)
下一个,我们可以借助元组来进行解包,在Python中,其实形如x
这种类型的数据都是元组,只是没穿上括号的衣服,类似的还有return返回值
def case_13():x = 1, 2 # tupled1 = x[0]d2 = x[1]
下一个,很多时候我们会借助time
库统计程序运行时间,并习惯使用两个time.time()
相减的方式计算
import timedef case_14():t1 = time.time()time.sleep(1)t2 = time.time()print(t2 - t1)
但time.perf_counter()
具有最高测量分辨率的时钟,统计结果更加准确,此外,time.clock已经在3.8中被废除了
import timedef case_14():t1 = time.perf_counter()time.sleep(1)t2 = time.perf_counter()print(t2 - t1)
下一个,使用isinstance
代替==
检查类型,避免如namedtuple这种子类带来的判断错误问题
from collections import namedtupledef case_15():line = namedtuple('line', ['k', 'b'])l = line(1, 5)if type(l) == tuple:print("it's a tuple") else:print("it's not a tuple") # √if isinstance(l, tuple):print("it's a tuple") # √else:print("it's not a tuple")
以上就是本期的全部内容,整理不易,如果有帮助到你就点赞关注吧,我是啥都生,我们下期再见~