搜尋
首頁後端開發Python教學Python日循環-使用範圍函數和索引、任務

Python Day-Loop-Using Range Function and Indexing,Tasks

斐波那契數列:
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中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python:編譯器還是解釋器?Python:編譯器還是解釋器?May 13, 2025 am 12:10 AM

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

python用於循環與循環時:何時使用哪個?python用於循環與循環時:何時使用哪個?May 13, 2025 am 12:07 AM

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

Python循環:最常見的錯誤Python循環:最常見的錯誤May 13, 2025 am 12:07 AM

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

對於循環和python中的循環時:每個循環的優點是什麼?對於循環和python中的循環時:每個循環的優點是什麼?May 13, 2025 am 12:01 AM

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

Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

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

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

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

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

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

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

熱工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SecLists

SecLists

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

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用