Introduction to Python variable types and naming rules

  • 2020-04-02 13:10:30
  • OfStack

The first letters are English and underlined, the rest can be English, numeric and underlined (i.e. : _), and the names of variables are case-sensitive, that is, temp and temp are different variables. The basic usage of variables is as follows:


#  Example: use variables 
a = 10
b = 20
print a + b
>>> 30   #  The output a add b The value of the 
a = 'hello'
b = 'python'
print a + ' ' + b
>>> hello python  #  The output a add b The value of the 

The above examples use variables, which can be divided into Numbers, characters, and objects.

Number: the number that can be used for mathematical operations, and the types of Numbers are divided into integer, floating point and complex Numbers. Integer refers to a number without a decimal point, while floating point refers to a number with a decimal point. The complex number is the complex number in mathematics, in which floating point Numbers can be represented by scientific counting method, the specific differences are as follows:


#  Example: use variables 
a = 10
print a / 3
>>> 3   #  The output variable a Divided by an integer 3 The value of the 
print a / 3.0 
>>> 3.33333333333  #  The output a Divide by floating point 3.0 The value of the 
b = 1e-2   #  Scientific notation 
print b
>>> 0.01   #  The output b The value of the 
print b*10
>>> 0.1   #  The output b*10 The value of the 
f1 = (1+2j)
f2 = (5+3j)
>>> (6+5j)  #  Output the plural f1+f2 The value of the 

In the above example, the numeric variable a is defined as the integer, and when divided by the integer, the value is considered to be the integer, so the value of the output is integer, and when the divisor is floating point, the value of the division is considered to be floating point. Number operation symbols have + (plus), - (minus), * (multiply), / (divide), % (remainder), but do not support ++, -, such as self - increase and self - decrease operators.

Character: refers to the string of contents represented by different characters. The string should be enclosed by single quotation mark or double quotation mark. The specific usage is as follows:

Example: define a character type variable.


s = 'Python'  #  Variable assignment string Python
s = "17jo.com"  #  Variable assignment string 17jo.com    
s = '''hello world!
hello Python!'''  #  Variable assignment two lines: hello world!hello python!
s = """hello world!
hello Python!"""  #  Variable assignment two lines: hello world!hello python!
s = ' It's Python' #  Variable assignment: It's Python!
s = ""Python""  #  Variable assignment: "Python"
s = '"Python"'  #  Variable assignment: "Python"
s = 'hello n python' # n Is an escape character for a newline 
print s   #  The output s The value of the 
>>> hello    #  Output in two lines 
>>>  python

The above example is an example of defining a string variable, where the ''/"" triple quotation marks can define a multi-line string, if you need to use single or double quotation marks in the string you can escape with \'/\", but you can use double quotation marks in single quotation marks, and you can use single quotation marks in double quotation marks without escaping.

The scope of a variable is the valid scope of a variable. In python, except for variables defined in functions or classes, variables defined in a program will remain valid the first time they appear, which means that the same name will be considered the same variable in later programs.


Related articles: