1. swap two elements
a, b = b, a #fk, it is so easy.
2. Chained Logical
a < b <= c <d
same with:
a< b and b<=c and c<d
#lol, I love python, easy easy and easy
3. Indexing a sequence
x = [1,2,3,4]
x [1] #2
x[2:] #2,3,4
x[-1] #4
del x[1] #x = [2,3,4]
4. Iterators using "for"
for x in c:
statement(s)
5. List comprehensions
1: a = [1,2,3,4,5]
2: result1 = [x for x in a]
3: print result1
4: #print out [1,2,3,4,5]
5: #This is equals to
6: result1 = []
7: for x in a:
8: result1.append(x)
6. Parameters (* & **)
*identifier indicates that any call to the function may supply extra positional arguments
**identifier indicates that any call to the function may supply extra named agruments.
本文介绍了Python编程中的几个实用技巧,包括交换变量值、链式逻辑比较、序列索引操作、使用for循环迭代、列表推导式及函数参数的灵活运用。
754

被折叠的 条评论
为什么被折叠?



