查找斐波那契数列:
生成达到给定数字的斐波那契数列。
示例:输入: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中文网其他相关文章!