Variables (variable) are a very important concept in the Python language. The main function of a variable is to give a name to a value in a Python program. Similar to "Zhang San", "John Doe", "King two leper" the same name, easy to remember.
?? In the Python language, the declaration of a variable needs to be assigned a value, after all, a variable that does not represent any value is meaningless, and the Python language does not allow such a variable.
?? Declaring a variable is also very simple, with the syntax structured as follows:
variable_name = variable_value
?? The left side of the equals sign (=) is the variable name, the right side is the variable value, and the Python compiler automatically recognizes the type of the variable when the value is assigned.
?? Note: Variables cannot be named arbitrarily and must conform to certain rules. Variable names usually contain letters, numbers, and underscores (_), and variable names cannot begin with a number. For example, value315 is a valid variable name, and 315value is the wrong variable name.
?? The following code declares multiple variables with data types including integers, strings, booleans, and floating-point numbers. Finally, the values of these variables are output.
x = 20 # 声明整数类型变量y = 40 # 声明整数类型变量s = "I love python" # 声明字符串类型变量flag = True # 声明布尔类型变量u = 30.4 # 声明浮点类型变量print(flag) # 输出flag变量的值print(x + y) # 输出x和y的和print(s) # 输出s变量的值print(u) # 输出u变量的值
?? The program runs as shown in the results.
"Python from rookie to Master" has been published, buy send video lessons
Python from rookie to expert (3): declaring variables