python冒泡循环示例

In this tutorial you are going to learn about Python Loop Techniques. In previous tutorial we discussed about Python pass statement. If you are interested, you can click this link to read that tutorial.

在本教程中,您将学习Python循环技术。 在先前的教程中,我们讨论了关于Python pass语句的问题。 如果您有兴趣,可以单击此链接阅读该教程。

Python循环 (Python Loop)

We learned about Python loop before. But Python’s loop is more flexible than that of other language. We can do more interesting things here. Python’s for loop is versatile. We are going to see some examples about this.

我们之前学习过Python循环 。 但是Python的循环比其他语言更灵活。 我们可以在这里做更多有趣的事情。 Python的for循环用途广泛。 我们将看到一些有关此的示例。

Python遍历序列 (Python Loop over a Sequence)

This is a very common example of Python for loop. Suppose we have a sequence of items and we need to traverse the sequence one by one. We can use for loop like this:

这是Python for loop的一个非常常见的例子。 假设我们有一个项目序列,我们需要一个遍历序列。 我们可以像这样使用for循环:

#initialize a list
items = ["apple", 1, 4, "exit", "321"]#for each item in the list traverse the list
for item in items:# print the itemprint (item),

The output of the following code will be

以下代码的输出将是

================== RESTART: /home/imtiaz/Desktop/ltech1.py ==================
apple 1 4 exit 321
>>>

Python以相反的顺序遍历序列 (Python Loop over a Sequence in Reverse Order)

You can also print the previous example int reverse order. To do so, you have to use reversed() function. reversed() function reverse the order of a sequence. Have a look over the following code.

您也可以按相反的顺序打印前面的示例。 为此,您必须使用reversed()函数。 reversed()函数反转序列的顺序。 看一下下面的代码。

#initialize a list
items = ["apple", 1, 4, "exit", "321"]#for each item in the list traverse the list
#before that reverse the order of the list
for item in reversed(items):# print the itemprint (item),

The output will be

输出将是

================== RESTART: /home/imtiaz/Desktop/ltech2.py ==================
321 exit 4 1 apple
>>>

Python按排序顺序遍历序列 (Python Loop over a Sequence in Sorted Order)

You can also print the previous example int sorted order. To do so, you have to use sorted() function. sorted() function sort the order of a sequence. Have a look over the following code.

您还可以打印前面的示例int排序顺序。 为此,您必须使用sorted()函数。 sorted()函数对序列的顺序进行排序。 看一下下面的代码。

#initialize a list
items = [7, 1, 4, 9, 3]#for each item in the sorted list, traverse the list
for item in sorted(items):# print the itemprint (item),

The output will be

输出将是

================== RESTART: /home/imtiaz/Desktop/ltech4.py ==================
1 3 4 7 9
>>>

枚举值和对应的索引 (Enumerate Values and Corresponding Index)

You can also enumerate values of a sequence along with their indexes. To do so, you have to use enumerate() function. The following code will help understand the thing.

您还可以枚举序列值及其索引。 为此,您必须使用enumerate()函数。 以下代码将帮助您理解。

#initialize a list
items = [7, 1, 4, 9, 3]#for each item in the list traverse the list
for index,value in enumerate(items):# print the index along with their valueprint ("value of "+str(index)+" is = "+str(value))

The output will be

输出将是

遍历两个或多个序列 (Traversing Two or More Sequences)

Using python for loop you can traverse two or more sequences at the same time. For example, in one sequence you have a list of name and in another sequence you have the list of hobbies of the corresponding persons. So you have to print persons’ name along with their hobbies. So the following example will guide you to do this.

使用python for循环,您可以同时遍历两个或多个序列。 例如,在一个序列中,您有一个姓名列表,在另一个序列中,您有相应人员的爱好列表。 因此,您必须打印人的名字以及他们的爱好。 因此,以下示例将指导您执行此操作。

names = [ 'Alice', 'Bob', 'Trudy' ]
hobbies = [ 'painting', 'singing', 'hacking']
ages = [ 21, 17, 22 ]#combine those list using zip() function
for person,age, hobby in zip(names,ages,hobbies):print (person+" is "+str(age)+"  years old and his/her hobby is "+hobby)

The output will be

输出将是

Alice is 21  years old and his/her hobby is painting
Bob is 17  years old and his/her hobby is singing
Trudy is 22  years old and his/her hobby is hacking
>>>

If you practice more, day by day you will learn many Interesting things about python. That’s all about Python loop example. Hope that you understood well. For any query, please comment below.
#HappyCoding

如果您每天练习更多,那么您将学到许多有关python的有趣知识。 这就是关于Python循环示例的全部内容。 希望你能理解。 如有任何查询,请在下面评论。
#HappyCoding

翻译自: https://www.journaldev.com/14245/python-loop-example

python冒泡循环示例

python冒泡循环示例_Python循环示例–循环在python中相关推荐

  1. python while循环和for循环转换_Python的While循环和for循环,python,while

    循环结构是编程的三大结构之一.python和其他语言一样,提供了非常多的关键字来支持for循环和while循环.下面整理一下: 1.while循环 1.1 while循环的基本格式 while循环的基 ...

  2. python for循环求和_python用for循环求和的方法总结

    Python中可以使用for循环实现累加求和 for循环语法: for 变量 in range(x): 循环需要执行的代码 如下实现1到n求和: def main(): sum = 0 # 定义变量做 ...

  3. python的两种循环结构_python分支和循环结构

    Python Python开发 Python语言 python分支和循环结构 1.分支结构 1.1应用场景 迄今为止,我们写的Python代码都是一条一条语句顺序执行,这种代码结构通常称之为顺序结构. ...

  4. python入门之控制结构循环结构_python入门教程04-05(python语法入门之流程控制)...

    本次课程介绍了流量的控制,介绍分支结构和循环结构的介绍和用法,实操介绍,我们一起来学习一下吧~~~ 一 引子: 流程控制即控制流程,具体指控制程序的执行流程,而程序的执行流程分为三种结构:顺序结构(之 ...

  5. python 运行电脑卡死_Python入门 5——循环语句及条件判断

    一.什么是语句 最基本的语句:赋值语句.运行函数/方法 条件语句:if 循环语句:for/while循环,遍历序列/字典 函数语句:def函数等 模块语句:import 二.条件判断:if语句 1.基 ...

  6. python遍历循环和无限循环结构_Python --- 程序的循环结构

    1.遍历循环 2.无限循环 3.循环控制保留字 4.循环的高级用法 1.遍历循环 遍历某个结构形成的循环运行方式 for in : - 从遍历结构中逐一提取元素,放在循环变量中 - 完整遍历所有元素后 ...

  7. python编写死循环语句_Python 全栈开发:python循环语句while

    while循环 为什么会有循环这种语句? 举一个简单的例子:我们想计算0-100以内偶数的和,这种时候就需要循环这种语句 那有人可能会说:这有什么 0+2+4+.......+98 一样可以计算出来啊 ...

  8. python 引用计数 循环引用_Python对象的循环引用问题

    Python对象循环引用 我们来介绍一下 Python 是采用何种途径解决循环引用问题的. 循环引用垃圾回收算法 上图中,表示的是对象之间的引用关系,从自对象指向他对象的引用用黑色箭头表示.每个对象里 ...

  9. list循环赋值_Python之 for循环

    list或tuple可以表示一个有序集合.如果我们想依次访问一个list中的每一个元素呢?比如 list: 如果list只包含几个元素,这样写还行,如果list包含1万个元素,我们就不可能写1万行pr ...

  10. pythonfor循环输入_python的for循环

    if else 多路分支 if elif else elif 可以有多个 else 视情况而定 例子: score=input("请输入你的成绩,必须是数字") #异常捕获 whi ...

最新文章

  1. 2018-8-22-粒子滤波
  2. 增强我们的Visual Studio(更新中)
  3. SpringBoot 之 MVC
  4. 【学习笔记】35、定义自己的异常类
  5. SAP配置webdynpro完全手册 .
  6. mysql 三主_MySQL主主复制3
  7. JDK 14 Rampdown:内部版本27
  8. Android生命周期函数执行顺序
  9. k-means均值向量
  10. 虚拟化精华问答 | 如何为虚拟机分配任务?
  11. OJ、ACM提交常见错误类型
  12. 主辅dns服务器的配置
  13. java1.8.0_java jdk官方下载|java jdk v1.8.0 官方免费版-520下载站
  14. AFDX(ARINC664)的网络协议——IP层
  15. 上网行为管理软件的主要功能、并简要说明上网行为管理类的软件的原理。
  16. 每日一道leetcode(python)347. 前 K 个高频元素
  17. npm/package.json/package-lock.json文件
  18. ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(三) 激动人心的时刻到啦,实现1v1聊天...
  19. 局域网对战平台 linux,在Linux下可用Wine安装和运行腾讯对战平台、5E对战平台
  20. ever 逾期_4ever的完整形式是什么?

热门文章

  1. ASP.NET MVC Html.BeginForm用法1
  2. Java零基础系列003——变量
  3. DbEntry 访问Access2010数据库
  4. Invalid format of Import utility nameVerify that ORACLE_HOME is properly oracle11.2g 无法imp,dmp
  5. java基础循环 for 、switch 、while 、do while、
  6. SAP B1 9.1 生产收货数量加已完成数量不能大于计划数量(存储过程控制代码)...
  7. 一大波Java来袭(四)String类、StringBuilder类、StringBuffer类对照
  8. erase() 返回的是删除此元素之后的下一个元素的迭代器 .xml
  9. Flume-NG源码阅读之Interceptor(原创)
  10. [妙味Ajax]第三课:AJAX跨域解决方案:JSONP