twentyonepilots 2018-07-17
1. 打印引入模块的文件路径
import threading
import socket
print(threading)
print(socket)
module ‘threading’ from ‘/Users/taorui/anaconda3/lib/python3.6/threading.py’
module ‘socket’ from ‘/Users/taorui/anaconda3/lib/python3.6/socket.py’
2. 交互环境下的_操作符
2+1
3
_
3
print(_)
3
3. 检查python的对象
test=[1,3,5,7]
print(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’]
4. 检查python版本
import sys
print(sys.version)
3.6.3 |Anaconda, Inc.| (default, Oct 6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
5. 组合多个字符串
test=['I','Like','Python','automation']
print(test)
ss=""
print(ss.join( test))
[‘I’, ‘Like’, ‘Python’, ‘automation’]
ILikePythonautomation
5. 四种翻字符串/列表的方式
翻转列表本身
testList=[1,3,5]
testList.reverse()
print(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]
6. 玩转枚举
testlist=[10,20,30]
for i,value in enumerate(testlist):
print(i,':',value)
0 : 10
1 : 20
2 : 30
7. 在python中使用枚举量
class Shapes:
Circle,Square,Tringle,Quadrangle=range(4)
print(Shapes.Circle)
print(Shapes.Square)
print(Shapes.Tringle)
print(Shapes.Quadrangle)
1
2
3
8. 使用*运算符来unpack函数参数
def test(x,y,z):
print(x,y,z)
testDict={'x':1,'y':2,'z':3}
testList=[10,20,30]
test(*testDict)
test(**testDict)
test(*testList)
x y z
1 2 3
10 20 30
9. 使用字典来存储选择操作
stdcalc={
'sum':lambda x,y:x+y,
'subtract':lambda x,y:x-y
}
print(stacalc['sum'](9,3))
print(stacalc['subt'](9,3))
12
6
10. 字典集合推导
testSet={i*2 for i in range(10)}
testDict={i: i*i for i in range(10) }
print(testSet)
print(testDict)
{0, 2, 4, 6, 8, 10, 12, 14, 16, 18}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
11. 原地交换两个数字
x,y=10,20
print(x,y)
x,y=y,x
print(x,y)
10 20
20 10
12. 链状比较操作符
n=10
result=1<n<20
print(result)
result=1>n>20
print(result)
True
False
13. 使用三元操作符来进行条件赋值
out=[m**2 if m>10 else m**4 for m in range(50)]
print(out)
[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]
14. 存储列表元素到新的变量
testList=[1,2,3]
x,y,z=testList
print(x,y,z)
1 2 3
15. 一行代码计算任何数的阶乘
import functools
result=(lambda k:functools.reduce(int.__mul__,range(1,k+1),1))(3)
print(result)
6
16. 找出列表中出现最频繁的数
test=[1,2,3,4,2,2,3,4,1,4,4,4,4]
print(max(set(test),key=test.count))
4
17. 检查一个对象的内存使用
import sys
x=1
print(sys.getsizeof(x))
28
18. 从连个相关的序列构建一个字典
t1=(1,2,3)
t2=(10,20,30)
print(dict(zip(t1,t2)))
{1: 10, 2: 20, 3: 30}
19. 一行代码搜索字符串的多个前后缀
print("http://www.google.com".startswith(("http://","https://")))
print("http://www.google.co.uk".endswith((".com",".uk")))
True
True
20.不使用循环构造一个switch-case语句
import itertools
test=[[-1,-2],[30,40],[25,35]]
print(list(itertools.chain.from_iterable(test)))
[-1, -2, 30, 40, 25, 35]