Python/互交模式

维基教科书,自由的教学读本

跳转到: 导航, 搜索
Python介绍:
概览 开发阶段:50%(截至{{{2}}})
下载和安装 开发阶段:50%(截至{{{2}}})
设置开发阶段:00%(截至{{{2}}})
互交模式 开发阶段:00%(截至{{{2}}})

Python 有两个编辑基本模式:"command line"模式,编译脚本和完成的 .py 文件;另一种是"GUI"模式,当您输入一行指令系统都将返回关于你这条信息是否合法的信息,这个是包括以前代码一同调试的结果。过一段时间,每过一会儿 Python 都会将您输入的内容自动保存到硬盘。

如果进入GUI模式,比如键入"python"就将没有返回的参数,这是一个很好的方式,您可以用这种方式尝试变化对语法。

 $ python
 Python 2.3.4 (#2, Aug 29 2004, 02:04:10) 
 [GCC 3.3.4 (Debian 1:3.3.4-9)] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>>

(如果 Python 没有正常运行,请您确认路径是否正确。参见 下载和安装 )

>>> 是 Python 告诉您该行字是表示您输入字符的符号。您将在系统提示语模块看到您输入字段程序运行的结果。尝试输入 1+1 . Python 将输出 2。如果您觉得您需要,请使用GUI模式。

例子:

 >>> 5
 5
 >>> print 5*7
 35
 >>> "hello" * 4
 'hellohellohellohello'
 >>> "hello".__class__
 <type 'str'>

不过,您必须小心使用GUI模式。如果您不小心,混乱可能会接踵而至。下面是一个有效的Python脚本:

 if 1:
   print "True"
print "Done"

如果您的输入出现错误,会出现以下结果:

 >>> if 1:
 ...   print "True"
 ... print "Done"
   File "<stdin>", line 3
     print "Done"
         ^
 SyntaxError: invalid syntax

What the interpreter is saying is that the indentation of the second print was unexpected. What you should have entered was a blank line, to end the first (i.e., "if") statement, before you started writing the next print statement. For example, you should have entered the statements as though they were written:

 if 1:
   print "True"
 
 print "Done"

Which would have resulted in the following:

 >>> if 1:
 ...   print "True"
 ...
 True
 >>> print "Done"
 Done
 >>>