0

0

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

DDD

DDD

发布时间:2024-12-04 08:12:44

|

894人浏览过

|

来源于dev.to

转载

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

第六个输出的解释:

viable
viable

基于GPT-4的AI非结构化数据分析平台

下载

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开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

707

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

624

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

734

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

616

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1234

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

547

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

573

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

695

2023.08.11

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

10

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 0.6万人学习

Django 教程
Django 教程

共28课时 | 2.4万人学习

SciPy 教程
SciPy 教程

共10课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号