python有趣用法汇总(持续更新)

使用python过程当中常常会不经意间遇到很是有趣的用法,因而特地搜集了一些html

有趣的用法

1.for-else用法python

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

详见Python中循环语句中的else用法github

《Effictive Python》一书中对for-else用法提出了质疑,主要观点是能够经过封装成函数来取代这一用法,而封装成函数是更加通用易懂的作法,因此通常不会使用for-else用法。为了避免影响文章的紧凑,我把评论区对书上内容的引用放在文末“更新补充”部分,有兴趣的读者能够去看一下。express

2.try-else用法编程

若是没有触发异常就执行elsesegmentfault

参考这里app

3.解包用法ide

相似这样a,b,c = ['a', 'b', 'c']函数

python有趣的解包用法

4.单行if-else

a = 1
b = 3 
if a == 1 
else 2
print('it is one' if a == 1 else 'no')
 #加群:725479218 获取更多的学习资料

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

# 通常是这样
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))
#加群:725479218 获取更多的学习资料

6.对生成器进行筛选

一个素数生成器的例子

7.or的用法

python中x or y表示若是x为真就是x的值,不然为y的值

咱们会常常看到相似这样的用法(好比函数的一个value参数没有设置默认值,这样使用就容许它不赋值)

value = value or {}
# 至关于
value = value if value else {}

8.and的用法

python中x and y表示若是x是假,结果就是x的值,不然就是y的值

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

举一个例子

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()
#加群:725479218 获取更多的学习资料

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

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

9.if value:

# 要用
if value:
# 不要用
if value == True:

这里总结一下这种状况下何时是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模块的写法)

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

其实参数还有其余的写法,如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循环索引

for index, item in enumerate(['a', 'b', 'c']):
    print(index, item)
输出:
0 a
1 b
2 c

4.管道操做

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

python自己不带有这样的用法,只是一些库提供了这样的用法,好比pandas和syntax_sugar

参考stackoverflow上的这个回答

其余

另外,就是一些基础的

上面不少在廖雪峰python教程中都能找到

阅读优秀的代码也是提升编程水平的好方法,参考下面这个问题

初学 Python,有哪些 Pythonic 的源码推荐阅读?

学习代码规范能够参考下面资料

更新补充

  1. for-else的更多讨论

下面引用《Effictive Python》一书中内容 `` a = 4 b = 9 

for i in range(2, min(a, b) + 1): print('Testing', i) if a % i == 0 and b % i == 0: print('Not coprime') break else: print('Coprime') `` 随后做者写到:

In practice, you wouldn’t write the code this way. Instead, you’d write a helper function to do the calculation. Such a helper function is written in two common styles. The first approach is to return early when you find the condition you’re looking for. You return the default outcome if you fall through the loop.

def coprime(a, b): for i in range(2, min(a, b) + 1): if a % i == 0 and b % i == 0: return False return True

The second way is to have a result variable that indicates whether you’ve found what you’re looking for in the loop. You break out of the loop as soon as you find something.

def coprime2(a, b): is_coprime = True for i in range(2, min(a, b) + 1): if a % i == 0 and b % i == 0: is_coprime = False break return is_coprime

结尾:

Both of these approaches are so much clearer to readers of unfamiliar code. The expressivity you gain from the else block doesn’t outweigh the burden you put on people (including yourself) who want to understand your code in the future. Simple constructs like loops should be self-evident in Python. You should avoid using else blocks after loops entirely.

总结起来就是for-else的优点是能够被写函数的方式替代的

相关文章
相关标签/搜索