正如后面一篇学到的,Python 语法是能够间接在 命令行 中被执行,如下代码:
>>> print("Hello, World!")Hello, World!
或者在 server 端创立一个以.py
为后缀名的 python 文件,而后在命令行中执行它。
C:\Users\Your Name>python myfile.py
Python 缩进
缩进
指的是代码行结尾处的空格,在其余编程语言中应用的 缩进
仅仅是为了进步可读性,而在 python 中这个缩进却是十分重要的,它决定了你的语法是否正确。
Python 应用缩进来示意一段代码块,如下代码所示:
if 5 > 2: print("Five is greater than two!")
如果没有缩进,python 将会抛出一个谬误,如下代码所示:
if 5 > 2:print("Five is greater than two!")
那到底代码行之前的空格留多少个适合呢? 这取决于你啦,至多保留一个空格即可。
if 5 > 2: print("Five is greater than two!") if 5 > 2: print("Five is greater than two!")
一旦预置好了几个空格,那后续代码块的语句都要保持一致的空格数,否则会报错。
if 5 > 2: print("Five is greater than two!") print("Five is greater than two!")
Python 变量
在 Python 中,变量创立的机会是你赋值的时候,听不懂吧,如下代码所示:
x = 5y = "Hello, World!"
Python 中并没有一种形式能够独自申明变量,要想学习变量的更多常识,能够参考这一章: https://www.w3schools.com/pyt...
Python 正文
Python 反对正文性能,目标都是将代码文档化,语法格局就是在命令行开始处应用 #
,Python 将会把整行作为一个正文。
#This is a comment.print("Hello, World!")
Python 正文
- 正文能够用于解释 Python 代码
- 正文能够用于使代码的可读性更高
- 正文也能够屏蔽掉一些测试代码
增加正文
正文应用 #
结尾,这样 Python 就能够疏忽它。
#This is a commentprint("Hello, World!")
正文也能够加到行的开端,这样 Python 就能够疏忽行的后续局部。
print("Hello, World!") #This is a comment
正文也不肯定非的是解释代码的用处,它也能够正文掉一些 python 语句。
#print("Hello, World!")print("Cheers, Mate!")
多行正文
Python 并没有一个独自的语法能够实现 多行正文
,要实现这样的性能,只能在多行中增加 #
来实现这个成果。
#This is a comment#written in#more than just one lineprint("Hello, World!")
哈哈,是不是很麻烦? 但这里有一个变通的做法,应用 多行string
的语法格局来代替,是这样的,当一个 多行String常量 没有赋值给一个变量的时候,Python 在解析时会主动疏忽,演示代码如下:
"""This is a commentwritten inmore than just one line"""print("Hello, World!")
译文链接: https://www.w3schools.com/pyt...
更多高质量干货:参见我的 GitHub: python