python basis - variables and constants

What is variable

Variables can be understood from the literal meaning: is the amount of change. But this understanding and not understanding seemingly no different, we see the variable word split

Capacity: recording status in the real world, so that the computer can like people to identify things in the world.

Change: the state of the real world will change happen.

Why should there be variable

For the real world, it must be to have a variable to describe things in the world. But why your computer must also have the concept of variable it? In fact, the operation of a computer program is a series of changes in the state.

Define the variable

How to define variables defined in python?
on the code

name = 'nash'


Compositional variables

Compositional variables divided into three sections:
 the variable name: variable name used to reference variable values, variable values whenever required, variable names are required.
 Assignment symbol: Big Brother is this assignment in python -> =
 variable value: storing data used to record a certain state in the real world.

Naming the variable name

Always remember, the definition of a variable is actually a state of the real world in the record, and keep forever is not an end, to take is the goal. So named variables should meet the following three specifications:
named variables should reflect the values of the state variables as described, remember not to use Chinese
variable names must underscore combination of alphanumeric and the first character of a variable name can not be a number.
Keywords can not be declared as a variable name
>> keyword as follows

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']


Two styles of variable names

Hump ​​body (between words capitalized)

NameOfNash = 'jinpan'
print(NameOfNash)

Underline (all lowercase, separated _ between words)

name_of_nash = 'jinpan'
print(name_of_nash)


constant

Variable is the amount of change, constants are the same amount. python syntax is not used arbitrarily defined constants, that is to say, python variable is defined in the constant nature. If you have to define constants, variable names must be all uppercase.
Vernacular: the program in the same amount is variable, python are they not constant, convention variable names in all capital called constant
application scenarios such as IP configuration file, port, database location information is not constant change and inconvenience dynamic variable names, etc.

NAME_OF_NASH = 'jinpan'
AGEOFNASH = 18
print(NAME_OF_NASH)
print(AGEOFNASH)

If it is constant, then we need to change, so it is only python developed a specification, but did not specify the syntax of constants, so constants also can be modified, but not recommended.

There are special constants defined in c language syntax, const int age = 19 ;, Once defined age is a constant, change the age that is being given.

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374623.html