Python中的縮排(Indentation)決定了程式碼的作用域範圍。這點和傳統的c/c 有很大的不同(傳統的c/c 使用花括號花括號{}符決定作用域的範圍;python使用縮排空格來表示作用域的範圍,相同縮進行的程式碼是處於同一範圍)。
每行程式碼中開頭的空格數(whitespace)用來計算該行程式碼的縮排等級(Indentation level),注意一個Tab會被替換為1~8個Space(具體的空格數量,不同的編譯器有不同的數量),縮排等級為0表示無縮排空格。
在一個原始檔案不建議同時使用空格和製表縮排符,當使用別人程式碼的時候幾乎是不可能知道別人使用的是空格還是製表符,這時最好統一縮排,在IDEL編輯器中可以使用Edit > Untabify Region 將任何製表符轉換為空格。
Python中的每一語句都有一個縮排等級,並且縮排等級會使用堆疊的資料結構進行儲存。在開始讀取檔案之前,0(表示縮排等級為0,無縮排)會被先壓入堆疊中。然後從檔案開頭到末尾,依序讀取每行邏輯程式碼,每行邏輯程式碼的縮排等級都會和棧頂值進行比較,如果相等,那麼什麼都不會發生;如果比棧頂值大的話,那麼該行邏輯程式碼的縮排等級就會被壓入堆疊中,同時會產生一個縮排標記(INDENT TOKEN);如果比棧頂值小的話,那麼棧中所有比該行邏輯程式碼縮排等級大的值都會從堆疊中移除,也會產生一個擴展標記(DEDENT TOKEN)。
下面是一個正確的縮排案例:
def perm(l):#0 # Compute the list of all permutations of l if len(l) <= 1:# 1 return [l]# 2 r = []# 3 for i in range(len(l)):# 4 s = l[:i] + l[i+1:]# 5 p = perm(s)# 6 for x in p:# 7 r.append(l[i:i+1] + x)# 8 return r# 9
下面是一個錯誤的案例
def perm(l): #1 error: first line indented for i in range(len(l)): #2 error: not indented s = l[:i] + l[i+1:] p = perm(l[:i] + l[i+1:]) #3 error: unexpected indent for x in p: r.append(l[i:i+1] + x)#4 return r #5 error: inconsistent dedent
以上是python縮排規則叫什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpythonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,with withOverHeadeBheadaroundAroundaround64byty64-bitsysysysysysysysysyssyssyssyssysssyssys2)

toensurepythonscriptsbehavecorrectlyacrycrosdevelvermations,分期和生產,USETHESTERTATE:1)Environment varriablesForsimplesettings,2)configurationfilesfilesForcomPlexSetups,3)dynamiCofforComplexSetups,dynamiqualloadingForaptaptibality.eachmethodoffersuniquebeneiquebeneqeniquebenefitsandrefitsandrequiresandrequiresandrequiresca

Python列表切片的基本語法是list[start:stop:step]。 1.start是包含的第一個元素索引,2.stop是排除的第一個元素索引,3.step決定元素之間的步長。切片不僅用於提取數據,還可以修改和反轉列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/刪除,2)儲存的二聚體和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,請考慮performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Atom編輯器mac版下載
最受歡迎的的開源編輯器

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!