基本的Python循环 [英] Basic Python loop

查看:61
本文介绍了基本的Python循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python在未定义时如何知道"i"是什么,是否应该没有错误?可能是一个简单的解释,但是我是学习Python的新手.

How does Python know what "i" is when it is not defined, shouldn't there be an error? Probably a simple explanation, but I am new to learning Python.

def doubles (sum):
    return sum * 2
myNum = 2
for i in range (0,3):
    myNum = doubles(myNum)
    print (myNum)

推荐答案

哈哈:-)人们正在记下您的问题,但我知道这是每个人都必须想到的一个问题.特别是那些通过在线课程而不是亲自授课来学习Python的人.

Haha :-) People are marking down your question, but I know that is one question must have came in every person's mind. Specially those who learned Python through Online courses and not through a teacher in person.

让我用外行的话解释一下,

Well let me explain that in layman's term,

您使用的方法专门用于1)列表和2)列表中的列表. 例如,

The method that you used is specially used for 1) lists and 2) lists within lists. For eg,

example1= ['a','b','c'] # This is a simple list
example2 = [['a','b','c'],['a','b','c'],['a','b','c']] # This is list within lists.

现在,"a","b"和"c"是列表中的项目.

Now, 'a','b' & 'c' are items in list.

所以说,

for i in example1:
    print i

我们实际上是在说

for item in the list(example1):
    print item

-------------------------

人们使用"i"(可能是item的缩写)或其他名称. 我不知道历史.

-------------------------

People use 'i', probably taken as abbreviation to item, or something else. I don't know the history.

但是,事实是,我们可以使用任何东西代替或使用'i',Python仍将其视为列表中的一项.

But, the fact is that, we can use anything instead or 'i' and Python will still consider it as an item in list.

让我再次给您示例. example1 = ['a','b','c']#这是一个简单的列表 example2 = [['a','b','c'],['a','b','c'],['a','b','c']]#这是列表列表.

Let me give you examples again. example1= ['a','b','c'] # This is a simple list example2 = [['a','b','c'],['a','b','c'],['a','b','c']] # This is list within lists.

for i in example1:
    print i

[out]: a
       b
       c

现在在example2中,项目是列表中的列表. ---而且,现在我将使用单词"item"而不是"i" ---结果无论两者都相同.

now in example2, items are lists within lists. --- also, now i will use the word 'item' instead of 'i' --- the results regardless would be the same for both.

for item in example2:
    print item

[out]: ['a','b','c']
       ['a','b','c']
       ['a','b','c']

人们还使用单数和复数形式来记住事物, 因此,让我们有一个字母列表.

people also use singulars and plurals to remember things, so lets we have a list of alphabet.

letters=['a','b','c','d']
for letter in letters:
    print letter

[out]: a
       b
       c
       d

希望有帮助.还有很多要解释的. 继续研究并继续学习.

Hope that helps. There is much more to explain. Keep researching and keep learning.

关于, 莫欣山

这篇关于基本的Python循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆