Variables, constants, and comments

Variables, constants, and comments

variable

Introduce a printfunction 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 alexis the value assigned to a variable, which is the data; =represent assignment; nameis 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:

  1. Variable names can only numbers, letters and underscores
  2. You can not begin with a number
  3. Prohibit the use of python keywords
  4. Variable names may have to be descriptive
  5. Variable names are case-sensitive
  6. You can not use Chinese and Pinyin
  7. 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:

QQ picture 20190905213207

The first step, to open up a memory in memory, to 18store in memory. Then the variable names agepoint to 18the corresponding memory address. Second and third step, age1and age2also point to 18the memory address. The fourth step, variable names agepointing to 20the corresponding memory address, but no longer points 18. The fifth step, variable names age1pointing to 19the corresponding memory address, but no longer points 18. The end result is, age2point 18, age1point 19, agepoint 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:

# 这个是单行注释的示例
# 换行之后要在开头加一个#

"""
窗前明月光,
玻璃好上霜.
要不及时擦,
整不好就脏.
"""

Guess you like

Origin www.cnblogs.com/shuoliuchina/p/12426528.html