Python杂记
来源:互联网 发布:常见国内域名购买商 编辑:程序博客网 时间:2024/05/21 22:51
1 - 关于python的import:
- import math :最推荐的方式,虽然可能会让代码变长,但是增加可可读性。
- from math import sqrt,version :这种导入模块资源的时候要注意名字是否雷同,在使用的时候直接使用sqrt,不需要math.sqrt,简洁。但是在使用version的时候就产生了歧义。
- from math import * :导入所有除了__开头的资源,
2 - 读写文件的一些便利
- original:
try: f = open(r'c:\user\administater\test.text','rb') passfinally: f.close()
- neat:
with open(r'c:\user\administater\test.text','rb') as f: pass
上述两种等价,第二种更便捷,with语句会自动执行close。
3 - 一行多句和多行一句语句
- 使用封号进行一行书写多句语句,可以这样写,但是不推荐,不符合PYTHON书写风格
a = 5;a += 10;print(a)
- 使用斜杠\来将一行语句分成多行书写,很有用
cursor.execute("INSERT INTO fc_project_tag_pairs \ (tag1,tag2,num_projs) \ VALUES(%s,%s,%s)",(tag1,tag2,count))print('abcdefgh',\ 567,'ijklmn')
最后,建议python初学者看一看A Byte of Python这本书,真的很不错。
阅读全文
0 0
- Python杂记
- python杂记
- python杂记
- Python 杂记
- Python 杂记
- python杂记
- python杂记
- Python杂记
- python杂记
- Python杂记
- python杂记
- python杂记
- python杂记
- Python杂记
- Python-杂记
- python杂记
- python 杂记
- Python杂记
- UIImageView显示动态图
- 最小花费
- CLR via C# 总结之Chap5 Primitive, Reference, and Value Types
- kubernetes2权威指南随书代码
- python datetime一些简单使用
- Python杂记
- C++ 花括号位置
- (6)nodejs学习---cookie和session
- MipMap
- Oracle 密码权限修改
- multiset
- Linux下安装SVN服务端小白教程
- HDU
- 出现Runtime Error的一般原因