python有趣用法汇总

使用python过程中经常会不经意间遇到非常有趣的用法,于是特意搜集了一些

有趣的用法

1.for-else用法

循环正常结束则执行else语句。一般用于循环找符合条件的元素,如果找到则break调出循环,不会触发else;如果没有找到(完整运行循环)则print not found

《Effictive Python》一书中对for-else用法提出了质疑,主要观点是可以通过封装成函数来取代这一用法,而封装成函数是更加通用易懂的做法,所以一般不会使用for-else用法。

2.try-else用法

如果没有触发异常就执行else

参考这里

3.解包用法

类似这样a,b,c = ['a', 'b', 'c']

python有趣的解包用法

4.单行if-else

<pre class=” hljs bash language-python” style=”border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, “andale mono”, “ubuntu mono”, monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;”>a = 1
b = 3
if a == 1
else 2
print(‘it is one’ if a == 1 else ‘no’)

</pre>

5.迭代器传入函数中不用加括号

<pre class=” hljs bash language-python” style=”border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, “andale mono”, “ubuntu mono”, monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;”># 一般是这样
a = (i for i in range(10))
sum(a)

我们可以这样

sum((i for i in range(10)))

但我们还可以这样

sum(i for i in range(10))

类似的有

‘ ‘.join(str(i) for i in range(10))

</pre>

7.or的用法

python中x or y表示如果x为真就是x的值,否则为y的值

我们会经常看到类似这样的用法(比如函数的一个value参数没有设置默认值,这样使用就允许它不赋值)

<pre class=” hljs php language-python” style=”border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, “andale mono”, “ubuntu mono”, monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;”>value = value or {}

相当于

value = value if value else {}

</pre>

8.and的用法

python中x and y表示如果x是假,结果就是x的值,否则就是y的值

x and y and z多个and连接时,如果全是真结果就是最后一个的值;如果中间有假的值,结果就是第一个假的值

举一个例子

<pre class=” hljs python language-python” style=”border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, “andale mono”, “ubuntu mono”, monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;”>def not_empty(a):
return a and a.strip()

not_empty(‘ a ‘)

值为 ‘a’

not_empty(None)

不会报错(如果 return a.strip() 就会报错)

在处理None的问题上相当于

def not_empty(a):
if a is None:
return None
else:
return a.strip()

</pre>

细细品味and和or的差别,他们逻辑类似,但是实现的功能是不可以相互替代的

  • or 是结果如果不满意有个善后工作
  • and是要做一件事之前先检验一下,不能做就不让它做

9.if value:

<pre class=” hljs php language-python” style=”border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, “andale mono”, “ubuntu mono”, monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;”># 要用
if value:

不要用

if value == True:

</pre>

这里总结一下这种情况下什么时候是True,什么时候是False

False: 0 0.0 '' [] {} () set() None False

True:

  • ' ' 'anything' [''] [0] (None, )
  • 没有内容的可迭代对象

另外要注意一点,我们用if判断一个对象是不是None的时候,要if a is None而不要直接if a,因为如果是后者,有非常多不是None的情况也会判定为False,比如空字符串、空列表等,为了精确指定None还是要用前者,这也是一种规范。

10.下划线的特殊使用

python中下划线是一种特殊的变量和符号,有一些特殊的用途

详见python中下划线的使用

11.文档字符串

python有一种独一无二的注释方式,在包、模块、函数、类中第一句,使用'''doc'''这样三引号注释,就可以在对象中用__doc__的方式提取

比较规范的写法是这样的(这里参考grequests模块的写法)

<pre class=” hljs python language-python” style=”border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, “andale mono”, “ubuntu mono”, monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;”>def myfun(a, b):
”’add two numbers
:param a: one number
:param b: another number
:returns: a number
”’
print(a + b)

print(myfun.doc)

结果为

add two numbers
:param a: one number
:param b: another number
:returns: a number

</pre>

其实参数还有其他的写法,如numpy库的写法,可以看这里

除此之外,函数注释还有另一种方式,函数名可以直接调用某个参数的注释,详见Python 的函数注释。还可以参考PEP 257

有用的函数

1.sum的本质

本质:sum(iterable, start=0)将可迭代对象使用+连接

所以sum([[1,2],[3,4]], [])返回结果为[1, 2, 3, 4]

2.range(start, stop[, step])

可以直接用for i in range(10, 0, -1)降序循环

3.enumerate循环索引

<pre class=” hljs bash language-python” style=”border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, “andale mono”, “ubuntu mono”, monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;”>for index, item in enumerate([‘a’, ‘b’, ‘c’]):
print(index, item)
输出:
0 a
1 b
2 c

</pre>

4.管道操作

func1(func2(func3(a)))写成类似a %>% func3 %>% func2 %>% func1,清晰展示函数执行的顺序,增强可读性

python本身不带有这样的用法,只是一些库提供了这样的用法,比如pandas和syntax_sugar

参考stackoverflow上的这个回答

其他

另外,就是一些基础的

    原文作者:Python小老弟
    原文地址: https://www.jianshu.com/p/13145dce6161
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞