pythonencoding etf-8_etf iopv python 代码30个Python常用小技巧
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 贊
總結
以上是生活随笔為你收集整理的pythonencoding etf-8_etf iopv python 代码30个Python常用小技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 又一日系品牌拥抱比亚迪 铃木EVX概念车
- 下一篇: websocket python爬虫_p