1、原地交换两个数字x, y =10, 20
print(x, y)
y, x = x, y
print(x, y)
10 20
20 10
2、链状比较操作符n = 10
print(1
print(1 > n <= 9)
True
False
3、使用三元操作符来实现条件赋值
[表达式为真的返回值] if [表达式] else [表达式为假的返回值]y = 20
x = 9 if (y == 10) else 8
print(x)
8
# 找abc中最小的数def small(a, b, c):
return a if a
print(small(1, 0, 1))
print(small(1, 2, 2))
print(small(2, 2, 3))
print(small(5, 4, 3))
0
1
3
3# 列表推导
x = [m**2 if m>10 else m**4 for m in range(50)]
print(x)
[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]
4、多行字符串multistr = "select * from multi_row
where row_id
print(multistr)
select * from multi_row where row_id
multistr = """select * from multi_row
where row_id
print(multistr)
select * from multi_row
where row_id
multistr = ("select * from multi_row"
"where row_id
"order by age")
print(multistr)
select * from multi_rowwhere row_id
5、存储列表元素到新的变量testList = [1, 2, 3]
x, y, z = testList # 变量个数应该和列表长度严格一致
print(x, y, z)
1 2 3
6、打印引入模块的绝对路径import threading
import socket
print(threading)
print(socket)
7、交互环境下的“_”操作符
在python控制台,不论我们测试一个表达式还是调用一个方法,结果都会分配给一个临时变量“_”
002pc.com从etf iopv python 代码30个Python常用小技巧分析来看,对etf iopv python 代码30个Python常用小技巧的结果。
8、字典/集合推导testDic = {i: i * i for i in range(10)}
testSet = {i * 2 for i in range(10)}
print(testDic)
print(testSet)
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
{0, 2, 4, 6, 8, 10, 12, 14, 16, 18}
9、调试脚本
用pdb模块设置断点import pdb
pdb.ste_trace()
10、开启文件分享
python允许开启一个HTTP服务器从根目录共享文件python -m http.server
11、检查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__', '__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']test = range(10)
print(dir(test))
['__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index', 'start', 'step', 'stop']
12、简化if语句# use following way to verify multi values
if m in [1, 2, 3, 4]:
# do not use following way
if m==1 or m==2 or m==3 or m==4:
更多: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 赞