Hello! 欢迎来到小浪资源网!


Python日循环-使用范围函数和索引、任务


Python日循环-使用范围函数和索引、任务

斐波那契数列
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() 函数用于生成数字序列。它通常在循环中用于迭代特定次数。

语法:

范围(开始、停止、步长)

立即学习Python免费学习笔记(深入)”;

–>start (可选): 序列的起始编号。如果不指定则默认为 0。

–>stop(必需):序列结束的数字(独占,即不包含在输出中)。

–>step(可选):递增或递减值。如果未指定,则默认为 1。

示例:

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 

第六个输出的解释:

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(" 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 

相关阅读