Python/基本语法

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

Hello, World![编辑]

(谨记:以下涉及到的符号均为英文符号)

下面是一个在标准输出设备上输出Hello World的简单程序,这种程序通常作为开始学习编程语言时的第一个程序:

print("Hello, world!")
#Hello, world!

Python 也可以单步直译执行。执行 Python 直译器进入互动式命令列的环境,你可以在提示符号 >>> 旁输入 print "Hello, world!",按 Enter 键输出结果:

>>> print("Hello, world!")
#Hello, world!

print是最简单,同时是最常用的输出代码,因为字句和绝大部分的变量都可以使用print输出。

输出字句时,需在头尾加上'"""",变量时则不用,在变量之间,或者字句和变量之间需要,+作间隔。

print在每次输出值后,都会开新行,如果想在上一行继续输出,可以加上,end=

>>>print('This function does not give a new line. ',end='')
>>>print('See?')
#This function does not give a new line. See?

python一行一句代码,每句代码结束后需要开新行。

代码之间亦需要对齐,代表层级关系。

代码前如果加上#,该行代码就不会被执行,可以在测试单独某个程式时使用,或留下备注给自己或其他人。

>>>print('This line will be shown.') #You can add comments here as well!
>>>#print('This line will not be shown.') This whole line will not be executed.
This line will be shown.