python Standard
python开发中常用代码
1、让代码既可以被导入又可以被执行
1 | if __name__ == '__main__': |
2、逻辑判断
1 | name = 'jelly' |
3、善用in
1 | if 'I' in name: |
4、不使用临时变量替换两个值
1 | a,b=b,a |
5、EAFP优于LBYL。
1 | EAFP - Easier to Ask Forgiveness than Permission. |
1 | #好的代码: |
6、使用enumerate进行迭代
1 | fruits = ['orange', 'grape', 'pitaya', 'blueberry'] |
7、善用生成式
1 | data=[1,2,3] |
8、zip组合键和值创建字典
1 | key = ['101,'102','103'] |
- Title: python Standard
- Author: MinjieY
- Created at : 2026-01-04 21:51:24
- Updated at : 2024-06-05 00:37:51
- Link: https://github.com/MinjieY/post/c3f5278c.html
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments