python冒泡循环示例
Python programming language provides loops with for statement. It is similar to most of the mainstream programming languages like C, C++, Java, or C#. We will look to the for loop Python examples. for loop mainly used to iterate over a specified list or array.
Python编程语言提供带有for语句的循环。 它类似于大多数主流编程语言,例如C,C ++,Java或C#。 我们将查看for循环Python示例。 for循环主要用于遍历指定列表或数组。
For循环Python语法 (For Loop Python Syntax)
The syntax for python for loop is like below.
python for循环的语法如下。
for var in arr:
statement-1
statement-2
...
简单和基本的For循环 (Simple and Basic For Loop)
Here is a simple for loop example which enumerates over a list
这是一个简单的for循环示例,它列举了一个列表
#!/usr/bin/python3
a=['p','o','f','t','u','t']
for var in a:
print(var)
We loop over a list which is consist of characters. We can directly run the python script like below. Another way is to make the python script executable and then run.
我们遍历由字符组成的列表。 我们可以像下面这样直接运行python脚本。 另一种方法是使python脚本可执行,然后运行。
使用范围进行迭代(Using Range For Iteration)
In general popular programming languages gives the ability to specify start, step, and end definition for counting in for loop. At first, it may seem it does not exist in python. But naturally, it exists with range function. The range() function gives the ability to define the start and end numbers.
在一般情况下,流行的编程语言提供了指定开始,步骤和结束定义的功能,以便计入for循环。 起初,它似乎在python中不存在。 但是自然地,它具有范围功能。 使用range()函数可以定义开始和结束数字。
#!/usr/bin/python3
for var in range(5):
print(var)
Here range function will start from 0 and count 5 numbers up to 4 with 1 by 1
范围功能将从0开始,并以1乘1计数5个数字直至4
设置开始结束项目(Set Start an End Items)
In some cases, we also need to set the start and end numbers. We can set start and end numbers like below in range
function too.
在某些情况下,我们还需要设置开始和结束编号。 我们也可以在range
函数中设置如下的开始和结束编号。
#!/usr/bin/python3
for var in range(5,12):
print(var)
设置增量计数(Set Increment Count)
We can change the stepping function and increment numbers with 2. For the range function, we will provide (0,6,2) 0 is the start number, 6 is end number and increment value is 2
我们可以使用2更改步进函数和增量数。对于范围函数,我们将提供(0,6,2) 0是开始数,6是结束数,增量值为2
#!/usr/bin/python3
for var in range(0,12,2):
print(var)
xrange()函数可减少内存使用(Less Memory Usage With xrange() Function)
To make loops more memory efficient xrange() function can be used. xrange() function is an enumerator and creates new instances if it is needed. Range function creates all range at the start. xrange
can only be used with Python2 because the features of xrange
is implemented in Python3 range
already.
为了使循环更有效地使用内存,可以使用xrange ()函数。 xrange()函数是一个枚举器,并在需要时创建新实例。 范围功能会在开始时创建所有范围。 xrange
只能与Python2一起使用,因为xrange
的功能已在Python3 range
实现。
#!/usr/bin/python3
for var in xrange(1,6):
print var
遍历给定列表(Iterate Over Given List)
For loop can be used in all iterable types like a dictionary, list, etc. We can iterate over a list like below. Actually every list is an iterable struct in Python.
For循环可用于所有可迭代类型,如字典,列表等。我们可以遍历以下列表。 实际上,每个列表都是Python中的可迭代结构。
#!/usr/bin/python3
a=['p','o','f','t','u','t']
for var in a:
print(var)
翻译自: https://www.poftut.com/python-for-loop-tutorial-with-examples/
python冒泡循环示例