python3.6 编程技巧总结

27 篇文章 71 订阅 ¥49.90 ¥99.00
本文总结了Python3.6编程中的一些实用技巧,包括打印模块文件路径、交互环境下的_操作、检查对象属性、Python版本检测、字符串与列表组合、翻转操作、枚举的使用、解压参数、字典操作等,旨在帮助开发者提高编程效率。
摘要由CSDN通过智能技术生成

学习python 大半年了,发现有很多很实用却容易被忽视的编程技巧,总结如下。


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)

0
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]

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据AI笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值