python的30个编程技巧
来源:互联网 发布:淘宝刷人气软件 编辑:程序博客网 时间:2024/06/06 13:19
从公众号上看到了一篇文章《30个python编程技巧!》,觉得有些挺有用的,有的也一直在用,就挨个实现了一下。
In [1]:
x, y =10, 20print(x, y)y, x = x, yprint(x, y)
In [3]:
n = 10print(1 < n < 20)print(1 > n <= 9)
In [4]:
y = 20x = 9 if (y == 10) else 8print(x)
In [6]:
# 找abc中最小的数def small(a, b, c): return a if a<b and a<c else (b if b<a and b<c else c)print(small(1, 0, 1))print(small(1, 2, 2))print(small(2, 2, 3))print(small(5, 4, 3))
In [7]:
# 列表推导x = [m**2 if m>10 else m**4 for m in range(50)]print(x)
In [15]:
multistr = "select * from multi_row \where row_id < 5"print(multistr)
In [16]:
multistr = """select * from multi_row where row_id < 5"""print(multistr)
In [17]:
multistr = ("select * from multi_row""where row_id < 5""order by age")print(multistr)
In [18]:
testList = [1, 2, 3]x, y, z = testList # 变量个数应该和列表长度严格一致print(x, y, z)
In [19]:
import threadingimport socketprint(threading)print(socket)
In [1]:
testDic = {i: i * i for i in range(10)}testSet = {i * 2 for i in range(10)}print(testDic)print(testSet)
In [ ]:
import pdbpdb.ste_trace()
In [ ]:
python -m http.server
In [3]:
test = [1, 3, 5, 7]print(dir(test))
In [4]:
test = range(10)print(dir(test))
In [ ]:
# use following way to verify multi valuesif m in [1, 2, 3, 4]:# do not use following wayif m==1 or m==2 or m==3 or m==4:
In [12]:
import sysif 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)
In [19]:
test = ["I", "Like", "Python"]print(test)print("".join(test))
In [21]:
# 翻转列表本身testList = [1, 3, 5]testList.reverse()print(testList)
In [23]:
# 在一个循环中翻转并迭代输出for element in reversed([1, 3, 5]): print(element)
In [24]:
# 翻转字符串print("Test Python"[::-1])
In [25]:
# 用切片翻转列表print([1, 3, 5][::-1])
In [26]:
test = [10, 20, 30]for i, value in enumerate(test): print(i, ':', value)
In [27]:
class shapes: circle, square, triangle, quadrangle = range(4)print(shapes.circle)print(shapes.square)print(shapes.triangle)print(shapes.quadrangle)
In [28]:
def x(): return 1, 2, 3, 4a, b, c, d = x()print(a, b, c, d)
In [35]:
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)
In [37]:
stdcalc = { "sum": lambda x, y: x + y, "subtract": lambda x, y: x - y}print(stdcalc["sum"](9, 3))print(stdcalc["subtract"](9, 3))
In [38]:
import functoolsresult = (lambda k: functools.reduce(int.__mul__, range(1, k+1), 1))(3)print(result)
In [40]:
test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4, 4]print(max(set(test), key=test.count))
In [41]:
import sysx = 1200print(sys.getrecursionlimit())sys.setrecursionlimit(x)print(sys.getrecursionlimit())
In [42]:
import sysx = 1print(sys.getsizeof(x)) # python3.5中一个32比特的整数占用28字节
In [47]:
import sys# 原始类class FileSystem(object): def __init__(self, files, folders, devices): self.files = files self.folder = folders self.devices = devicesprint(sys.getsizeof(FileSystem))# 减少内存后class FileSystem(object): __slots__ = ['files', 'folders', 'devices'] def __init__(self, files, folders, devices): self.files = files self.folder = folders self.devices = devicesprint(sys.getsizeof(FileSystem))
In [49]:
import syslprint = lambda *args: sys.stdout.write(" ".join(map(str, args)))lprint("python", "tips", 1000, 1001)
In [50]:
t1 = (1, 2, 3)t2 = (10, 20, 30)print(dict(zip(t1, t2)))
In [52]:
print("http://localhost:8888/notebooks/Untitled6.ipynb".startswith(("http://", "https://")))print("http://localhost:8888/notebooks/Untitled6.ipynb".endswith((".ipynb", ".py")))
In [55]:
import itertoolsimport numpy as nptest = [[-1, -2], [30, 40], [25, 35]]print(list(itertools.chain.from_iterable(test)))
In [58]:
def xswitch(x): return xswitch._system_dict.get(x, None)xswitch._system_dict = {"files":10, "folders":5, "devices":2}print(xswitch("default"))print(xswitch("devices"))
阅读全文
0 0
- Python的30个编程技巧
- python的30个编程技巧
- Python的30个编程技巧
- Python高效编程的19个技巧
- Python高效编程的19个技巧
- Python 30 个技巧
- 30个有关Python的小技巧
- 30个有关Python的小技巧
- 30个有关Python的小技巧
- 30个有关Python的小技巧
- 30 个 Python 语言的特点技巧
- python的17个技巧
- Python编程的一些技巧
- Python 程序员需要知道的 30 个技巧(转载)
- Python程序员需要知道的30个技巧
- Python 程序员需要知道的 30 个技巧
- Python 程序员需要知道的 30 个技巧
- 30 个java编程技巧(最佳实践的初学者)
- html网页标题头图标
- SQL数据库面试题以及答案(50例题)
- HTTP常见状态码
- Linux常用服务配置(Java开发) 二
- C++库文件 <cmath> (math.h)
- python的30个编程技巧
- 170.Spring Boot WebSocket:概念篇
- C/C++实现三角函数的方法
- 教你在GitHub上搭建属于自己的博客
- eclipse中tomcat的配置Configure the Web Modules on this server,auto reloading enabled
- linux学习笔记(-)
- AVL CRUISE M 2015(车辆系统仿真软件) v2015.2官方版下载附安装教程
- SparkStreaming中SparkUI中显示stage skipped的原因
- VCSA开启ssh的方法