php中文网

Day - for 循环和索引

php中文网

查找斐波那契数列:

生成达到给定数字的斐波那契数列。
示例:输入: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("
second output")
for no in range(1,10):
    print(no, end=' ')

print("
third output")
for no in range(5,10):
    print(no, end=' ')

print("
fourth output")
for no in range(1,10,2):
    print(no, end=' ')

print("
fifth output")
for no in range(3,15,3):
    print(no, end=' ')

print("
sixth output")
for no in range(10,1):
    print(no, end=' ')

print("
seventh output")
for no in range(10,1,-1):
    print(no, end=' ')

print("
eighth output")
for no in range(20,3,-1):
    print(no, end=' ')

print("
nineth 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("
second output")
for letter in name[0:6:2]:
    print(letter, end=' ')
print("
third output")
for letter in name[8:0:-1]:
    print(letter, end=' ')
print("
fourth output")
for letter in name[8:2:-1]:
    print(letter, end=' ')
print("
fifth output")
for letter in name[8:-1:-1]:
    print(letter, end=' ')
print("
sixth output")
for letter in name[8:3:-2]:
    print(letter, end=' ')
print("
seventh output")
for letter in name[8::-1]:
    print(letter, end=' ')
print("
ninth output")
for letter in name[::]:
    print(letter, end=' ')
print("
tenth output")
for letter in name[6::]:
    print(letter, end=' ')
print("
eleventh 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("
second output")
for letter in word[23::]:
    print(letter , end=" ")
print("
third output")
for letter in word[-1:-6:-1]:
    print(letter , end=" ")
print("
fouth output")
for letter in word[0:9:2]:
    print(letter , end=" ")
print("
fifth output")
for letter in word[8::-2]:
    print(letter , end=" ")
print("
sixth 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中文网其它相关文章!

上一篇:AWS 数据库服务:概述

下一篇:返回列表