1. Annotation
- Role: Annotate the code
1.1 Single-line comment: hash mark
# 单行注释 井号右边的内容为注释内容,起辅助说明的作用
1.2 Multi-line comments: single quotation mark or double quotation mark
'''
多行注释
由三对单引号或双引号组成 注释写在引号中间
'''
1.3 Chinese support
Python3 supports Chinese by default, but if you use Chinese directly in python2, it will cause program errors
- Solution: write # coding=utf-8 at the beginning of the program
# coding=utf-8
print('Hello')
- Note: The recommended method in the python grammar specification:
# -*- coding:utf-8 -*-
2. Variable
Role: store data
- Variables: store variable data during program operation
- Constant: data that does not change during the running of the program, usually use pure capital letters + underscores to set the variable name
2.1. Ways to set variables
- Python is a weakly typed language, so there is no need to specifically define variable types when creating variables
变量名 = 值
2.2. Variable types
Number (number type) |
---|
int (integer) |
long (long integer [can also represent octal and hexadecimal]) |
float (floating point) |
complex |
Boolean (Boolean type) |
---|
True |
False |
String |
---|
List |
---|
Tuple |
---|
Dictionary |
---|
2.3 View variable types
type(变量名)
3. Identifier
- User-defined name, used for variable names, function names, etc.
3.1 Identifier rules
- It is composed of letters, underscores and numbers, and the number cannot start
- case sensitive
A≠a
3.2 Naming rules
- Hump nomenclature
Lower camel case: the
first word starts with a lowercase letter; the first letter of the second word is capitalized, for example: myName, aDog
Upper camel case: The first letter of each word is capitalized, for example: FirstName, LastName
The underscore "_" connects words:
such as send_buf,
- Python's command rules follow the PEP8 standard
3.3 Keywords
- There are some identifiers with special functions in python, the so-called keywords
- Developers are not allowed to define an identifier with the same name as the keyword
and as assert break class continue def del
elif else except exec finally for from global
if in import is lambda not or pass
print raise return try while
View keywords
method 1
import keyword
print(keyword.kwlist)
Method 2
help('keywords')