python3.6语法教程
PYthon is practical and readable programming language. Python have different syntax from popular programming languages like C, C++, C#, Java etc. Python syntax makes it easy to learn. In this tutorial we will look some aspect of Python programming language syntax.
PYthon是实用且可读的编程语言。 Python具有与流行的编程语言(如C,C ++,C#,Java等)不同的语法。Python语法易于学习。 在本教程中,我们将介绍Python编程语言语法的某些方面。
识别码 (Identifier)
Identifier is the one of the main topics of all programming languages. Identifiers are used by programmer to specify some programming language structs like variable, class, function etc. Identifiers can start with lowercase and uppercase letters and underscore.
标识符是所有编程语言的主要主题之一。 程序员使用标识符来指定一些编程语言结构,例如变量,类,函数等。标识符可以以小写和大写字母以及下划线开头。
a to z
A to Z
_
Numbers can be used in identifiers except first character
可以在标识符中使用数字,但第一个字符除外
有效标识符(Valid Identifiers)
a
a9
_a
myname
my9name
MYNAME
无效的标识符 (Invalid Identifiers)
9
@name
$myname
.
=
保留字 (Reserved Words)
Python have already uses some words. These words provides programming language functionality to the programmer. We call this words reserved words. Reserved words can not be used as identifiers. Reserved words also called Python keywords. All reserved words are lowercase letters only. Here list of Python reserved words.
Python已经使用了一些单词。 这些单词为程序员提供了编程语言功能。 我们称此字为保留字。 保留字不能用作标识符。 保留字也称为Python关键字。 所有保留字仅是小写字母。 这里是Python保留字的列表。
and | exec | not |
assert | finally | or |
break | for | pass |
class | from | |
continue | global | raise |
def | if | return |
del | import | try |
elif | in | while |
else | is | with |
except | lambda | yield |
和 | 执行 | 不 |
断言 | 最后 | 要么 |
打破 | 对于 | 通过 |
类 | 从 | 打印 |
继续 | 全球 | 提高 |
定义 | 如果 | 返回 |
德尔 | 进口 | 尝试 |
小精灵 | 在 | 而 |
其他 | 是 | 与 |
除了 | 拉姆达 | 让 |
评论(Comments)
While writing python applications we may want to take notes on the code or we simply need to explain what the code, function, class, variable do. This notes are called Comments and can be written with #
sign. Comment lines are not interpreted or used by python they are just text not a instruction.
在编写python应用程序时,我们可能要记下代码,或者只需要解释代码,函数,类,变量的作用。 该注释称为注释,可以用#
号书写。 注释行不被python解释或使用,它们只是文本而不是指令。
#Print the name of the user
print(name)
Comments can be also start after an instruction line like below.
注释也可以在如下所示的指令行之后开始。
print(name) #Print the name of the user
线和缩进 (Lines and Indentation)
One of the pythons most interesting feature is indentation. While developing application we need to group instructions and create blocks. In python we use indentation in order to group or create block. The number of spaces in indentation is variable but must be the same in all of the file. If it is not the same we will get error and code will no run.
python最有趣的功能之一是缩进。 在开发应用程序时,我们需要对指令进行分组并创建块。 在python中,我们使用缩进来分组或创建块。 缩进中的空格数是可变的,但在所有文件中必须相同。 如果不一样,我们将得到错误,并且代码将无法运行。
Following is an example where we used 3 spaces as indentation and will work perfectly.
以下是我们使用3个空格作为缩进并可以完美工作的示例。
def myfunc():
print("Hi")
if True:
print("True")
空行 (Blank Line)
There is no meaning blank lines in Python programming language.
Python编程语言中没有空白行。
多线 (Multi Line)
Normally each line is used for new instruction. So we can not use multiple lines for the same instruction this is the nature of the Python. But \
can be use to provide multi line instructions which makes given lines like a single line.
通常每行用于新指令。 因此,我们不能对同一条指令使用多行代码,这就是Python的本质。 但是\
可以用来提供多行指令,使给定的行像单行一样。
text="this" + \
"single" + \
"line"
But one more exception is [],{},()
can be used in multiple lines without \
.
但是还有一个例外是[],{},()
可以在不带\
多行中使用。
name=['pof'
,'tut'
,'com']
name
#['pof', 'tut', 'com']
python3.6语法教程