13、运行时检测python版本import sys
if not hasattr(sys, "hexversion") or sys.version_info != (2, 7):
print("sorry, you are not running on python 2.7")
print("current python version:", sys.version)
sorry, you are not running on python 2.7
current python version: 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)]
14、组合多个字符串test = ["I", "Like", "Python"]
print(test)
print("".join(test))
['I', 'Like', 'Python']
ILikePython
15、四种翻转字符串、列表的方式
5
3
1
16、用枚举在循环中找到索引test = [10, 20, 30]
for i, value in enumerate(test):
print(i, ':', value)
0 : 10
1 : 20
2 : 30
17、定义枚举量class shapes:
circle, square, triangle, quadrangle = range(4)
print(shapes.circle)
print(shapes.square)
print(shapes.triangle)
print(shapes.quadrangle)
0
1
2
3
18、从方法中返回多个值def x():
return 1, 2, 3, 4
a, b, c, d = x()
print(a, b, c, d)
1 2 3 4
19、使用*运算符unpack函数参数def test(x, y, z):
print(x, y, z)
testDic = {'x':1, 'y':2, 'z':3}
testList = [10, 20, 30]
test(*testDic)
test(**testDic)
test(*testList)
z x y
1 2 3
10 20 30
20、用字典来存储表达式stdcalc = {
"sum": lambda x, y: x + y,
"subtract": lambda x, y: x - y
}
print(stdcalc["sum"](9, 3))
print(stdcalc["subtract"](9, 3))
12
6
21、计算任何数的阶乘import functools
result = (lambda k: functools.reduce(int.__mul__, range(1, k+1), 1))(3)
print(result)
6
22、找到列表中出现次数最多的数test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4, 4]
print(max(set(test), key=test.count))
4
23、重置递归限制
python限制递归次数到1000,可以用下面方法重置import sys
x = 1200
print(sys.getrecursionlimit())
sys.setrecursionlimit(x)
print(sys.getrecursionlimit())
1000
1200
24、检查一个对象的内存使用import sys
x = 1
print(sys.getsizeof(x)) # python3.5中一个32比特的整数占用28字节
28
25、使用slots减少内存开支import sys
# 原始类
class FileSystem(object):
def __init__(self, files, folders, devices):
self.files = files
self.folder = folders
self.devices = devices
print(sys.getsizeof(FileSystem))
# 减少内存后
class FileSystem(object):
__slots__ = ['files', 'folders', 'devices']
def __init__(self, files, folders, devices):
self.files = files
self.folder = folders
self.devices = devices
print(sys.getsizeof(FileSystem))
1016
888
26、用lambda 来模仿输出方法import sys
lprint = lambda *args: sys.stdout.write(" ".join(map(str, args)))
lprint("python", "tips", 1000, 1001)
python tips 1000 1001
27、从两个相关序列构建一个字典t1 = (1, 2, 3)
t2 = (10, 20, 30)
print(dict(zip(t1, t2)))
{1: 10, 2: 20, 3: 30}
28、搜索字符串的多个前后缀print("http://localhost:8888/notebooks/Untitled6.ipynb".startswith(("http://", "https://")))
print("http://localhost:8888/notebooks/Untitled6.ipynb".endswith((".ipynb", ".py")))
True
True
29、不使用循环构造一个列表import itertools
import numpy as np
test = [[-1, -2], [30, 40], [25, 35]]
print(list(itertools.chain.from_iterable(test)))
[-1, -2, 30, 40, 25, 35]
更多:etf iopv python 代码30个Python常用小技巧
https://www.002pc.comhttps://www.002pc.com/python/2227.html
你可能感兴趣的Python,30,技巧,常用
win8 premiere pro ccWin RT系统的13个常用技巧
《win8 premiere pro ccWin RT系统的13个常用技巧》总结了关于win8控制面板在哪教程,对于我们来第二电脑网确实能学到不少知识。
win8魔兽世界c盘Win 8最常用的50个技巧
《win8魔兽世界c盘Win 8最常用的50个技巧》总结了关于电脑知识学习教程,对于我们来002pc.com确实能学到不少知识。
1、锁屏
启动之后,用户将首先看到Windows 8的锁屏界
linux 权限后面的数字vim常用命令之多行注释和多行删除
vim中多行注释和多行删除命令,这些命令也是经常用到的一些小技巧,可以大大提高工作效率。
1.多行注释:
1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式;
java js 速度慢jQuery常用知识点总结以及平时封装常用函数
本文为大家介绍了jQuery中常用知识点及函数,包含许多细节方面的知识,下面我们一起学习一下。
jQuery中为我们提供了很多有用的属性,自己总结的一些常用的函数。个人认为在在线
kali linux ubuntu常用的shell变量用法
常用的shell变量:$0 Shell本身的文件名, 带有相对路径。(常用于shell中脚本路径切换)$1~$n 添加到Shell的各参数值。
什么组装电脑关于Python在Linux、Mac和Windows上的安装方法总结
一.Linux下安装python
1.python源码安装包下载地址:
https://www.python.org/downloads/source/
2.下载完tar.xz压缩包以后,开始解压
mysql 查出数据一对多python使用unittest测试接口步奏详解
这次给大家带来python使用unittest测试接口步奏详解,python使用unittest测试接口的注意事项有哪些,下面就是实战案例,一起来看一下。
xp搜索助手如何撤销XP系统的不常用的功能
如何撤销XP系统的不常用的功能电脑知识网认为此文章对《xp搜索助手如何撤销XP系统的不常用的功能》说的很在理。
一、撤消对压缩文件的支持 从Windows Me开始,微软
0踩
赏
0 赞