斐波那契数列:
1)使用3个变量:
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
2) 使用 2 个变量:
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
范围函数:
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中文网其他相关文章!