非常有趣的Python的用法汇总!

作者: 14e61d025165 | 来源:发表于2019-07-12 15:26 被阅读5次
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1562916376211" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

Python资源共享群:484031800

使用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']

4.单行if-else

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">a = 1
b = 3
if a == 1
else 2
print('it is one' if a == 1 else 'no')
</pre>

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

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 一般是这样
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 spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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 spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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 spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 要用
if value:

不要用

if value == True:
</pre>

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

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

True:

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">' ' 'anything' [''] [0] (None, )
</pre>

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

10.下划线的特殊使用

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

11.文档字符串

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

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

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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 库的写法,可以看这里

除此之外,函数注释还有另一种方式,函数名可以直接调用某个参数的注释

有用的函数

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 spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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

其他

另外,就是一些基础的

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">map reduce filter
</pre>

阅读优秀的代码也是提高编程水平的好方法

相关文章

网友评论

    本文标题:非常有趣的Python的用法汇总!

    本文链接:https://www.haomeiwen.com/subject/fvtykctx.html