Python basis -1 basic grammar

Basic grammar

Identifier
called an object identifier that variables, constants, functions, etc. from the name.

First, it must be said, Python language are strictly case-sensitive in any scene! That is the meaning of A and A represents a completely different

python denotes the identifier for the following provisions:

The first character must be an alphabet or underscore characters

For example, a, abc, _id, and the like are possible. However, for example, $ a (beginning with $ the PHP variable syntax), ~ abc, 123a are not allowed. We must pay attention.

Some may ask, Chinese can be used as an identifier thing? The answer is yes

我 = "json",打印出‘我’ 结果是json

虽然可以但是不建议大家这样做。

In addition, in order to underscore the beginning of the identifier usually have special significance. To underscore the beginning of the single variable, such as representatives of _foo prohibit external access to the class members, the class needs to be accessed through an interface provided, can not be used "from xxx import *" Import. And beginning with a double underscore, for example __foo, representatives of private members of the class to __foo__ double underscore is the beginning and end of the python in a dedicated special method to identify. As representatives of the class constructor __init__. These will be dedicated to discussion behind us here do not do too much explanation.

Other partial identifiers from the letters, numbers and underscores
identifier in addition to the first character is not a digit, the other portion may also comprise a number. It should be noted that special characters are not allowed. E.g:

a123b, bbc, a_b_c_1 these are possible. However, a & b, abc these are not allowed.

Also note that, due to l (lowercase L) and the number 1, and the numbers 0 o sensitive similarity in appearance, please try not to let them appear adjacent to maintain a clear semantic ensure not found error recognize a read operation.

Similarly, the English jams on Chinese grammar is also possible, but definitely do not do it!

a这都能行b就服你 = 100
a这都能行b就服你
100

Identifiers are case sensitive
just above also gives you said, the identifier ab and AB are two completely different identifiers

Variable names all lower case, all caps constant name
this can not be considered grammatical level requirements, but the code specifications.
PI can be represented by a variable, but usually we will think this is a constant representative of pi

Function and method names with lower case underlined
This is regarded as a specification of the code, we define a method of time. Try to use get_images, count_apple like are named. Of course be a small hump manner, getImages, countApple this.

Class name with a large hump
is also standardized code, e.g. ThreadMinxIn, ButtonClick this. It is the first letter of each word capitalized, grouped together like a hump arranged in the same level.

Module and package names with a lowercase
try and lowercase module package name, and not to the standard library and the famous third-party libraries of the same name. If the same name as the program runs error.

Finally remind you not to use the name of the variable named keywords and built-in functions! !

Reserved words python
Python reserved word, also known as keywords, is determined by the official Python language syntax as a function of the specific identifier, they can not be used for any custom identifier name. Keywords contain only lowercase letters. These keywords can be output through the library python provided

Python's standard library module provides a keyword, you can output the current version of all keywords

import keyword
keyword.kwlist

If you really use keyword as a variable, what will happen?

The system will be directly prompted syntax errors, so there must be careful not to use keywords as variable. In addition to not use keywords as identifiers, the built-in function is also not allowed. sum is a sum of functions. Here I define it as a string to see what happens?

Comments
we write program, not only have the code, but also a lot of comments. Annotated with the nature help nature, they do not exist corresponding to the code during execution, and transparent.

单行注释
Python中,以符号“#”为单行注释的开始,从它往后到本行的末尾,都是注释内容。

# 单行注释

多行注释
Python没有真正意义上的多行注释(块注释)语法

#第一行注释

#第二行注释

#第三行注释

注释文档
在某些特定的位置,用三引号包括起来的部分,也被当做注释。

"""
    这个是函数的说明文档。
    :param a: 加数
    :param b: 加数
    :return: 和
"""

代码头两行
很多时候,我们在一些py脚本文件的开头都能看到类似的以#开头的这样两行代码,它们不是注释,是一些设定

#!/usr/bin/env python
# -*- coding:utf-8 -*-

第一行:用于指定运行该脚本的Python解释器,Linux专用,windows不需要。env方式下,系统会自动使用环境变量里指向的Python。还有一种方式,#!/usr/bin/python3.6,这会强制要求使用系统中的python3.6解释器执行文件,这种方式不好,一旦你本地的Python3.6版本删除了,会出现找不到解释器的错误。无论两种方式的哪一种,都指的是在linux下使用

第二行:代码的编码方式。不是程序要处理的数据的编码方式,而是程序自己本身的字符编码。在Python3中,全面支持Unicode,默认以UTF-8编码,我们不用再纠结中文的问题,乱码的问题,所以本行其实可以不需要。但在Python2中,对字符的编码是个非常令人头疼的问题,通常都需要指定这么一行。如果要自定义别的编码类型的话,可以像这样:# -- coding: cp-1252 --,但如果没有强制需求的话,不要自己作死,请坚持使用utf-8编码。

这里的-*-是什么意思呢?没意思,装饰美观好看而已

语句与缩进
语句:在代码中,能够完整表达某个意思、操作或者逻辑的最短代码,被称为语句。

a = 321
a = 321
print("hello world")
list.append(item)

这里强调一下,python的标准语言不需要使用分号。简单的换行就表示语句已经结束。

代码块:为完成某一特定功能而联系在一起的一组语句构成一个代码块。有判断、循环、函数、类等各种代码块。代码块的首行通常以关键字开始,以冒号( : )结束。比如:

if expression:
    pass
elif expression :
    pass
else:
    pass

Python最具特色的语法就是使用缩进来表示代码块,不需要使用大括号
像PHP、JAVA等语言都是使用({})来表示代码块的。python一般用四个空格就是tab来缩进。在pycharm中tab自动回转成4个空格。在Linux环境中,如VIM编辑器,建议使用空格。

那么怎么才是正确的缩进方式呢?

1.所有的普通语句,顶左开始编写,不需要缩进

2.所有的语句块,首行不用缩进,从冒号结束后开始下一行,都要缩进

3.直到该语句块结束,就退回缩进,表示当前块已结束

4.语句块可以嵌套,所以缩进也可以嵌套

多行语句: 前面是多条语句在一行,但如果一条语句实在太长,也是可以占用多行的,可以使用反斜杠()来实现多行语句

string = "i love this country,"\
         +"because it is very beautiful!"\

不到迫不得已,不要使用这种,该换行就换行。

pass语句
pass语句是占位语句,它什么都不做,只是为了保证语法的正确性而写。以下场景中,可以使用pass语句:

当你不知道后面的代码怎么写的时候

当你不需要写代码细节的时候

当语法必须,又没有实际内容可写的时候

其它的一些你觉得需要的场景

def func(a,b):
    pass

字符串的表示形式
在后面的章节中,会进行更深入的介绍,这里作为一个前期的知识铺垫。

abc可能是个变量,但是"abc"肯定是个字符串了。这里强调一下在编程语言中,所有的符号都是英文状态下的。

在python中单引号和双引号的作用完全相同。在其他语言中,单双引号还是有一定区别的。

原生字符串: 通过在字符串前加r或R,如 r"this is test \n",表示这个字符串里的斜杠不需要转义,等同于自身

Guess you like

Origin www.cnblogs.com/sakura579/p/12243397.html