python冒泡循环示例_带有示例的Python For循环教程

python冒泡循环示例

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脚本可执行,然后运行。

Simple For Loop
Simple For Loop
简单的循环

使用范围进行迭代(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

Using Range
Using Range
使用范围

设置开始结束项目(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 Start an End
Set Start an End
设置开始结束

设置增量计数(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)
Set Increment Count
Set Increment Count
设置增量计数

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
Less Memory Usage With Xrange
Less Memory Usage With Xrange
Xrange减少了内存使用

遍历给定列表(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)
Iterate Over List
Iterate Over List
遍历列表
LEARN MORE  Python For Loop Tutorial With Examples and Range/Xrange Functions
了解更多有关带示例和Range / Xrange函数的Python For Loop教程

翻译自: https://www.poftut.com/python-for-loop-tutorial-with-examples/

python冒泡循环示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值