后浪云Python教程:python怎么在一行上写代码?

后浪云Python教程:python怎么在一行上写代码?插图

python中可以使用分号“;”来分割代码,使所有代码可以写在同一行:

print "How can i write python in a line? \n"; print "You can use ';' follow the sentence. " ; print "Then run the script!"; print; print "Done."

在C、Java等语言的语法中规定,必须以分号作为语句结束的标识。Python也支持分号,同样用于一条语句的结束标识。但在Python中分号的作用已经不像C、Java中那么重要了,Python中的分号可以省略,主要通过换行来识别语句的结束。

THE END