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 item
print (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 item
print (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 item
print (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 value
print ("value of "+str(index)+" is = "+str(value))
遍历两个或多个序列 (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
python冒泡循环示例