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() 函数用于生成数字序列。它通常在循环中用于迭代特定次数。
语法:
范围(开始、停止、步长)
-->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日循环-使用范围函数和索引、任务》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于文章的相关知识,请关注golang学习网公众号!
电脑的输入法怎么更新?
- 上一篇
- 电脑的输入法怎么更新?
- 下一篇
- React 组件中如何显示 Tailwind CSS 提示?
-
- 文章 · python教程 | 3天前 | 线程池 · Python 线程池 concurrent.futures 任务取消 Future.cancel
- Python concurrent.futures.Future.cancel 怎样判断任务能否取消
- 143浏览 收藏
-
- 文章 · python教程 | 3天前 | 标准库 · HTTP服务 · 多线程 · Python教程 · Python 并发请求 http.server ThreadingHTTPServer ThreadingMixIn BaseHTTPRequestHandler
- Python http.server ThreadingHTTPServer 怎样处理并发请求
- 481浏览 收藏
-
- 文章 · python教程 | 3天前 |
- Python tarfile.TarInfo.replace 怎样复制归档成员属性
- 308浏览 收藏
-
- 文章 · python教程 | 3天前 | 并发编程 · Python教程 · Python 多进程 multiprocessing forkserver 启动上下文
- Python multiprocessing forkserver 怎样处理进程启动上下文
- 232浏览 收藏
-
- 文章 · python教程 | 3天前 | 标准库 · 异步编程 · Python教程 · Python asyncio StreamReader readexactly IncompleteReadError 异步流
- Python asyncio.StreamReader.readexactly 怎样处理不足长度的输入
- 427浏览 收藏
-
- 文章 · python教程 | 3天前 |
- Python operator.length_hint 怎样为未知长度迭代器设置估计值
- 336浏览 收藏
-
- 文章 · python教程 | 3天前 | 标准库 · Python教程 · Python warnings catch_warnings simplefilter filterwarnings 警告过滤
- Python warnings context manager 怎样临时调整警告过滤
- 255浏览 收藏
-
- 文章 · python教程 | 3天前 | 调试 · python · 异步编程 · Python asyncio introspection current_task all_tasks
- Python asyncio introspection 怎样定位当前任务
- 120浏览 收藏
-
- 文章 · python教程 | 3天前 | 标准库 · Python教程 · Python 类型注解 Python 3.14 annotationlib get_annotations ForwardRef 延迟注解
- Python 3.14 annotationlib.get_annotations 怎样读取延迟注解
- 423浏览 收藏
-
- 文章 · python教程 | 3天前 |
- Python 3.14 跨解释器 Queue.put 的阻塞与 QueueFull 处理方案
- 157浏览 收藏
-
- 文章 · python教程 | 4天前 | xml · python · Python xml xml.etree.ElementTree canonicalize C14N
- Python xml.etree.ElementTree.canonicalize 怎样生成稳定的 XML 文本
- 151浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 1211次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 1194次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 1385次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 1321次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 1172次使用
-
- Python TaskGroup 怎样让子任务失败统一收敛
- 2026-11-12 501浏览
-
- Python xml.dom.minidom.unlink 怎样释放不再使用的 DOM 树引用
- 2026-10-27 501浏览
-
- Python typing 怎样用 TypeIs 缩小类型
- 2026-10-24 501浏览
-
- Python sqlite3 Connection serialize 怎么导出数据库快照:备份窗口、内存占用与恢复校验
- 2026-08-26 501浏览
-
- Python监控网页状态:requests异常处理实战
- 2026-05-29 501浏览

