table of Contents
Variables, constants, and comments
variable
Introduce a print
function to print (output) data.
>>> print(111)
111
The official definition of variables: the program running in the middle value, temporarily stored to be used again.
Popular terms, is to give the variable data from a nickname, easy call.
The following is a typical variable assignment statement:
name = "alex"
Where alex
is the value assigned to a variable, which is the data; =
represent assignment; name
is a variable name. By print(name)
use variable names defined actions:
>>> name = "alex"
>>> print(name)
alex
Variable name (in other Python identifiers such as function names, class names, module names, also have to follow this rule) naming convention:
- Variable names can only numbers, letters and underscores
- You can not begin with a number
- Prohibit the use of python keywords
- Variable names may have to be descriptive
- Variable names are case-sensitive
- You can not use Chinese and Pinyin
- Recommended wording:
- Hump body
- Underline (the official recommended)
To start with a number of named variables will complain:
>>> 1a = 'alex'
File "<stdin>", line 1
1a = 'alex'
^
SyntaxError: invalid syntax
And underline hump body named variables example, it is clear that the nomenclature underlined more intuitive manner:
AlexOfOldboy = 89 # 这是驼峰体
alex_of_oldboy = 89 # 这是下划线
Python variable names are case sensitive:
>>> name = 'alex'
>>> Name = 'leo'
>>> print(name)
alex
>>> print(Name)
leo
Python keywords:
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or','pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
print
You can print multiple contents, separated by commas:
>>> age1 = 19
>>> print(age, age1)
18 19
Consider the following period of assignment:
age = 18
age1 = age
age2 = age1
age = 20
age1 = 19
print(age, age1, age2)
The final printed result is:
20 19 18
The specific assignment shown below:
The first step, to open up a memory in memory, to 18
store in memory. Then the variable names age
point to 18
the corresponding memory address. Second and third step, age1
and age2
also point to 18
the memory address. The fourth step, variable names age
pointing to 20
the corresponding memory address, but no longer points 18
. The fifth step, variable names age1
pointing to 19
the corresponding memory address, but no longer points 18
. The end result is, age2
point 18
, age1
point 19
, age
point 20
.
constant
In Python, there is no constant strict sense. Everyone convention is variable variable name in uppercase is considered constant, generally can not be easily modified during program execution. E.g:
ID = 110120130140150
ID = "123123213" # 不建议在程序执行过程中随意修改
Variable and constant application scenarios:
- We variable for use later in the development
- Constants for the configuration file
Note
The role of the comment is to give some of the obscure code annotation or explanation. The annotated code will not be executed.
Comments in Python divided into two types:
- Single-line comments (comment when the line): The
#
beginning of the representation - Multi-line comments: The three pairs
“ ”
or‘ ’
package, you can wrap
Specific examples are:
# 这个是单行注释的示例
# 换行之后要在开头加一个#
"""
窗前明月光,
玻璃好上霜.
要不及时擦,
整不好就脏.
"""