Tips#1. 变量交换
- Example
x, y = 10, 20 print(x, y) x, y = y, x print(x, y)
- Output
10 20 20 10
Tips#2. 链接比较运算符
有时候比较运算符的聚合是另一个可以方便的手段
- Example
n = 10 result = 1 < n < 20 print(result) result = 1 > n <= 9 print(result)
- Output
True False
Tips#3. 使用三元运算符进行条件判断
三元运算符是if-else语句的快捷方式,也称为条件运算符
[on_true] if [expression] else [on_false]
以下是您可以使用的一些示例,使您的代码紧凑简洁
- Example
如果y
为9
,则将10
分配给x
,否则将20
分配给x
x = 10 if (y == 9) else 20
同样,我们可以对类对象做同样的事情
x = (classA if y == 1 else classB)(param1, param2)
在上面的例子中,classA
和classB
是两个类,一个类构造函数将被调用。
- Example
算出最小的数量
def small(a, b, c): return a if a <= b and a <= c else(b if b <= a and b <= c else c) print(small(1, 0, 1)) print(small(1, 2, 2)) print(small(2, 2, 3)) print(small(5, 4, 3))
- Output
0 1 2 3
我们甚至可以使用具有列表理解的三元运算符
>>> [m**2 if m > 10 else m**4 for m in range(10)] [0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561]
Tips#4. 使用多行字符串
基本的方法是使用C语言导出的反斜杠
- Example
>>> "select * from multi_row \\ ... where row_id < 5" 'select * from multi_row where row_id < 5'
另外一个诀窍就是使用三个引号
- Example
>>> """select * from multi_row ... where row_id < 5""" 'select * from multi_row \\nwhere row_id < 5'
又或者你可以将字符串放在括号中
- Example
>>> ("select * from multi_row " ... "where row_id < 5 " ... "order by age") 'select * from multi_row where row_id < 5 order by age'
Tips#5. 将列表的元素存储到新变量中
我们可以使用一个列表来初始化一个no
的变量。在拆包列表时,变量的数量不应超过no
的列表中的元素。
- Example
testList = [1, 2, 3] x, y, z = testList print(x, y, z)
- Output
1 2 3
Tips#6. 输出导入模块的文件路径
如果您想知道在代码中导入模块的绝对位置,则使用以下技巧
- Example
import threading import socket print(threading) print(socket)
- Output
<module 'threading' from '/home/ansheng/.pyenv/versions/3.6.2/lib/python3.6/threading.py'> <module 'socket' from '/home/ansheng/.pyenv/versions/3.6.2/lib/python3.6/socket.py'>
Tips#7. 使用交互式_
运算符
在Python控制台中,每当我们测试一个表达式或者调用一个函数时,结果将分派到一个临时名称_(一个下划线)
- Example
>>> 2 + 1 3 >>> _ 3
_引用最后执行的表达式的输出
Tips#8. 词典/集合推导
像列表推导一样,字典/集合同样也有推导,它们使用简单,同样有效
- Example
>>> {i: i * i for i in range(10)} {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81} >>> {i * 2 for i in range(10)} {0, 2, 4, 6, 8, 10, 12, 14, 16, 18}
Tips#9. 调试脚本
我们可以借助于pdb
模块在Python脚本中设置断点
- Example
import pdb pdb.set_trace()
Tips#10. 文件共享
Python允许运行一个HTTP服务器,您可以使用它来从服务器根目录共享文件
- Example
~$ python -m http.server Serving HTTP on 0.0.0.0 port 8000 (<http://0.0.0.0:8000/>) ...
以上命令将启动默认端口即8000
的服务器,也可以通过将自定义端口作为上一个参数传递给上述命令
Tips#11. 检查Python中的对象
我们可以通过调用dir()
方法来检查Python中的对象。
- Example
>>> test = [1,2,3] >>> dir(test) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Tips#12. 简化if语句
要验证多个值,我们可以通过以下方式进行。
- Example
if m in [1,3,5,7]:
或者,我们可以使用{1,3,5,7}
而不是[1,3,5,7]
用于in
运算符,因为set
可以通过O(1)
访问每个元素
Tips#13. 在运行时检测Python版本
有时如果当前运行的Python
小于受支持的版本,我们可能不想执行我们的程序,要实现这一点,您可以使用下面的代码片段,它还以可读格式打印当前使用的Python版本。
- Example
>>> import sys >>> print("Current Python version: ", sys.version) Current Python version: 3.6.2 (default, Aug 25 2017, 09:52:18) [GCC 6.3.0 20170406]
>>> if not hasattr(sys, "hexversion") or sys.hexversion != 50660080: ... print("Sorry, you aren't running on Python 3.5\\n") ... print("Please upgrade to 3.5.\\n") ... sys.exit(1) ... Sorry, you aren't running on Python 3.5 Please upgrade to 3.5.
sys.version_info >= (3, 5) sys.hexversion!= 50660080
Tips#14. 组合多个字符串
如果要连接列表中可用的所有元素,请参见以下示例。
- Example
>>> test = ['I', 'Like', 'Python', 'automation']
现在,我们从上面给出的列表中的元素创建一个单一的字符串。
>>> ''.join(test) 'ILikePythonautomation'
Tips#15. 四种方式来反转字符串/列表
- Example
反转列表本身
>>> testList = [1, 3, 5] >>> testList.reverse() >>> testList [5, 3, 1]
再循环中反复循环
>>> for element in reversed([1,3,5]): print(element) ... 5 3 1
反转一行字符串
>>> "Test Python"[::-1] 'nohtyP tseT'
使用切片翻转列表
>>> [1, 3, 5][::-1] [5, 3, 1]