首頁 >後端開發 >Python教學 >Day - for 迴圈與索引

Day - for 迴圈與索引

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-12-08 06:03:09517瀏覽

Day - for loop and Indexing

找斐波那契數列:

產生達到給定數字的斐波那契數列。
範例:輸入:10 → 輸出:0, 1, 1, 2, 3, 5, 8.

f, s = -1, 1
t = 0
while t<=13:
    t= f + s
    print(t,end= ' ')
    f,s = s, t
0 1 1 2 3 5 8 13 21 

不使用第三個變數來找出斐波那契數列:

f, s = -1, 1 
while f+s<=13: 
    print(f + s,end= ' ')  
    f,s = s, f + s
0 1 1 2 3 5 8 13 

for 循環:

for 迴圈是程式設計中使用的控制流語句,用於將程式碼區塊重複特定次數或迭代序列。

文法:

for variable in iterable:

步驟運算子:

步驟運算子是指為循環中的迭代指定增量(或步驟)的能力。在 Python 中,這通常與 range() 函數一起使用,它允許指定一個步驟來控制循環變數在每次迭代後如何變化。

文法:

range(start, stop, step)

start:序列的起始值(含)。
stop:序列的停止值(不包括)。
步驟:序列在每次迭代中增加(或減少,如果為負)的量。

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

索引:

索引是指使用元素的位置或索引來存取序列(如列表、元組或字串)中的元素。

索引類型:

1.正索引:
第一個元素從 0 開始。

2.負索引:
最後一個元素從 -1 開始。

name = 'ABCDEFGHI'
print("First output")
for letter in name[0:5]:  
    print(letter, end=' ')
print("\nSecond output")
for letter in name[0:6:2]:
    print(letter, end=' ')
print("\nThird output")
for letter in name[8:0:-1]:
    print(letter, end=' ')
print("\nFourth output")
for letter in name[8:2:-1]:
    print(letter, end=' ')
print("\nFifth output")
for letter in name[8:-1:-1]:
    print(letter, end=' ')
print("\nSixth output")
for letter in name[8:3:-2]:
    print(letter, end=' ')
print("\nSeventh output")
for letter in name[8::-1]:
    print(letter, end=' ')
print("\nNinth output")
for letter in name[::]:
    print(letter, end=' ')
print("\nTenth output")
for letter in name[6::]:
    print(letter, end=' ')
print("\nEleventh output")
for letter in name[2::2]:
    print(letter, end=' ')

First output
A B C D E 
Second output
A C E 
Third output
I H G F E D C B 
Fourth output
I H G F E D 
Fifth output

Sixth output
I G E 
Seventh output
I H G F E D C B A 
Ninth output
A B C D E F G H I 
Tenth output
G H I 
Eleventh output
C E G I

name = 'ABCDEFGHI'
print(name[0])
print(name[-1])
print(name[-2])
print(name[-3])
print(name[-1::-1])

A
I
H
G
IHGFEDCBA

寫一個程式來檢查給定的字串是否是回文

name = input("Enter word: ")
if name[::] == name[::-1]:
    print("Palindrome")
else:
    print("Not Palindrome")
Enter word: amma
Palindrome
Enter word: ggfhyjdr
Not Palindrome
name = 'abcd'
print(name * 3)
abcdabcdabcd

name = 'abcd'
print(name + 3)
TypeError: can only concatenate str (not "int") to str
This error occurs because you're trying to concatenate a string (name) with an integer (3) using the + operator. In Python, the + operator for strings is used for concatenation, but both operands must be strings.

for num in range(5):
    print("* " * num)

* 
* * 
* * * 
* * * * 
for num in range(1,6):
    print("* " * num)
* 
* * 
* * * 
* * * * 
* * * * * 
for num in range(5,0,-1):
    print("* " * num)

* * * * * 
* * * * 
* * * 
* * 
* 
digit = "1"
for num in range(5,0,-1): 
    print(digit * num)
    digit = str(int(digit)+1) 

11111
2222
333
44
5

任務:

ABCDEFGHI
XYZ
ZYXWV
ACEGI
伊格卡
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

以上是Day - for 迴圈與索引的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn