Python : 使用函数

Python : using function

如何在像 IDLE 或 linux 终端这样的程序中运行函数? 以及如何使用循环?当我尝试使用它时发生了:

>>>for i in numbers:  # when i am push enter its make ... 
...    print packet[i]  # when i am pushing on the enter its create new line 

一个函数是这样定义的:

def my_func(arg_one):
    return arg_one * 2

print my_func(2)

4

我强烈推荐 Python tutorial here 作为开始学习 Python 的好地方。

你可以像这样在你的解释器中模拟一个循环:

>>> for i in range(5): # press enter
...    print i         # press enter again
...                    # press enter again
...
0
1
2
3
4
>>>

请注意 # 的使用,这也是在 Python 中开始行注释的正确方法。