在python中 for循环是第二个主要的循环结构
The for loop within python is the second major loop
事实上对于其他大部分编程语言也是如此
within most coding languages as well.
呃 在我看来 慢慢的你就会发现
Um…in my opinion,
它就是最有活力 最有用的循环结构
this is, most dynamic, most useful loop that you’ll come across.
现在继续 这就是我们刚才得到的 就是这个数组
Um…So here is what we’ve got here. We’ve got this array here,
这数组里有1 2 3 4 四个不同的元素
That has 1, 2, 3, 4 different things within the array.
现在我们仅仅想得到单一的元素
We just got the pieces of strings here.
那么 接下来要做的就是用for循环来实现
And so what we’re gonna do is
for 循环的结构就是
for…the way this works is…the for loop is,
for 每一个元素 in 元素集合
for everything in something,
基本如此
basically, do this,
所以这里我们要说明下
so what we are going to say here is,
在这里你可以定义变量
for, and here you kind of define the variable.
任何名称都无关紧要 直接定义就可以了
Doesn’t matter what you call it, it just defines it.
嗯 那么这里的话 for item in
Um…so we’re gonna say, for item in
嗯 在这里 我们认为变量是x
Um…And in here, our variable that we would like to consider is x,
同样的 以冒号结尾 然后敲回车
same thing as well, end with a colon, ‘enter’,
那么 for item in x
Um…so for item in x,
也可以这样写 for word in x 无所谓的
you can also say, for word in x, whatever,
然后下面这里 使用word
then you come down here, and then you use ‘word’,
再然后 每次使用这个数组的不同部分
and then it’ll apply each time to a different part of this array.
所以这里 对于数组x里的每一个单词
So we’ll say, for every word that’s within our array x,
我们都想要输出 python is
we want to print out ‘python is’,
就像我们之前了解到的 逗号会自动帮我们添加一个空格
and as we learn before, comma will add a space automatically for us,
然后是’word’ 所以打上 for word in x
and ‘word’, so for word in x,
我们想要输出python is(加上单词)
we want to print out ‘python is’, word,
其实就是把单词尾加到它之后
and it’s gonna say, it’s going to add this to the end.
OK 保存
So let’s go ahead save it,
如你所见 python 如此美丽 如此俏皮
And as you can see, ‘python is nifty’,
python 强大有力 技惊四座
‘python is powerful’, ‘python is awesome’,
python是最好的语言
‘python is the best language’.
现在你都明白了吧
So there you have it.