斐波那契數列:
1)使用3個變數:
f, s = -1, 1 t = 0 while t <p>輸出:<br> </p> <pre class="brush:php;toolbar:false">0 1 1 2 3 5 8 13 21
2) 使用 2 個變數:
f, s = -1, 1 while f+s <p>輸出:<br> </p> <pre class="brush:php;toolbar:false">0 1 1 2 3 5 8 13
範圍函數:
range() 函數用於產生數字序列。它通常在循環中用於迭代特定次數。
文法:
範圍(開始、停止、步長)
-->start(可選):序列的起始編號。如果不指定則預設為 0。
-->stop(必需):序列結束的數字(獨佔,即不包含在輸出中)。
-->step(可選):遞增或遞減值。如果未指定,則預設為 1。
範例:
print("First Output") for no in range(10): print(no, end=' ') print("\nSecond Output") for no in range(1,10): print(no, end=' ') print("\nThird Output") for no in range(5,10): print(no, end=' ') print("\nFourth Output") for no in range(1,10,2): print(no, end=' ') print("\nFifth Output") for no in range(3,15,3): print(no, end=' ') print("\nSixth Output") for no in range(10,1): print(no, end=' ') print("\nSeventh Output") for no in range(10,1,-1): print(no, end=' ') print("\nEighth Output") for no in range(20,3,-1): print(no, end=' ') print("\nNineth Output") for no in range(20,2,-2): print(no, end=' ')
輸出:
First Output 0 1 2 3 4 5 6 7 8 9 Second Output 1 2 3 4 5 6 7 8 9 Third Output 5 6 7 8 9 Fourth Output 1 3 5 7 9 Fifth Output 3 6 9 12 Sixth Output Seventh Output 10 9 8 7 6 5 4 3 2 Eighth Output 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 Nineth Output 20 18 16 14 12 10 8 6 4
第六個輸出的解釋:
range()函數需要一個step參數來產生反向序列。當不指定步長時,預設為 1,這表示序列將嘗試從 10 增加到 1,但由於 10 大於 1,因此不會產生任何數字。
負索引:
通常索引從 0 開始,但也可以從 -1 開始,即負索引(從 -1 開始)。
範例:
name = 'ABCDEFGHI' for letter in name[0:5]: print(letter, end=' ') print() for letter in name[0:6:2]: print(letter, end=' ') print() for letter in name[8:0:-1]: print(letter, end=' ') print() for letter in name[8:2:-1]: print(letter, end=' ') print() for letter in name[8:-1:-1]: print(letter, end=' ') print() for letter in name[8:3:-2]: print(letter, end=' ') print() for letter in name[8::-1]: print(letter, end=' ') print() for letter in name[::]: print(letter, end=' ') print() for letter in name[6::]: print(letter, end=' ') print() for letter in name[2::2]: print(letter, end=' ')
輸出:
A B C D E A C E I H G F E D C B I H G F E D I G E I H G F E D C B A A B C D E F G H I G H I C E G I
解釋:第五個輸出()
名稱[8:-1:-1]
在此索引中,start 是 8,在上面的範例中是最後一個值,end -1 也表示最後一個值,因此輸出沒有傳回任何內容。
找出給定字串是否為回文:
name = input("Enter word: ") if name[::] == name[::-1]: print("Palindrome") else: print("Not Palindrome")
輸出:
Enter word: amma Palindrome
圖案形成:
例:1
for num in range(1,6): print("* " * num)
輸出:
* * * * * * * * * * * * * * *
範例:2
for num in range(5,0,-1): print("* " * num)
輸出:
* * * * * * * * * * * * * * *
注意:* 在 2 個字串之間起作用,但在 2 個字串之間不起作用。 (例如 - a*2-->aa,a 2-->a2)
範例:3
digit = "1" for num in range(5,0,-1): print(digit * num) digit = str(int(digit)+1) print()
輸出:
11111 2222 333 44 5
任務:
字 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1)ABCDEFGHI
2)XYZ
3)ZYXWV
4)ACEGI
5)伊格卡
6)ZXVTRPNLJHFDB
word = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' print("First Output") for letter in word[0:9]: print(letter , end=" ") print("\nSecond Output") for letter in word[23::]: print(letter , end=" ") print("\nThird Output") for letter in word[-1:-6:-1]: print(letter , end=" ") print("\nFouth Output") for letter in word[0:9:2]: print(letter , end=" ") print("\nFifth Output") for letter in word[8::-2]: print(letter , end=" ") print("\nSixth Output") for letter in word[-1::-2]: print(letter , end=" ")
輸出:
First Output A B C D E F G H I Second Output X Y Z Third Output Z Y X W V Fouth Output A C E G I Fifth Output I G E C A Sixth Output Z X V T R P N L J H F D B
以上是Python日循環-使用範圍函數和索引、任務的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3漢化版
中文版,非常好用