In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what are the variables and constants in Python. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
I. the difference between variables and constants
Variable: the amount by which the value changes while the program is running.
Constant: the amount by which the value does not change while the program is running.
Whether it is a variable or a constant, a space is created in memory to hold its value.
Variable 1. Variables in Python do not need to declare types
This is based on the dynamic language features of Python. Variables can be used directly without the need to declare the type in advance. For example, four variables, such as int a, str b, list c, int d, are not declared before use, which must be carried in C language and JAVA programming language.
For example:
A = 4b = "" c = [] d = 9-52. Use the "=" sign to assign a = 100 to the variable
The equal sign here should be understood and read as "assignment", not "equal". "assignment" is the operation of variables, and "equal" is the comparison of two variables. The four variables above, a _ d are assigned different values by the sign "=".
3. Assignment
Each variable must be assigned before it is used, and the variable assignment will not be created until the variable is assigned.
New variables create and open up memory space and preserve values through the act of assigning values. If you use it directly without an assignment, the exception or unnamed exception referenced before the assignment will be thrown.
For example:
A # alone a, there is nothing but an error a = 1 # so no problem. The interpreter knows that an is a new variable. What the heck is c.append (1) # c? NameError: name 'c'is not defined
Results:
4. Variable
In Python, variables themselves have no concept of data types
The so-called "variable type" is the type of object referenced by the variable, or the type of the value of the variable.
A = 1a = "" a = [1,2,3] a = {"K1": "v1"}
When the variable an is created, it is given an integer type with a value of 1, then it is changed to the string "", then a list, and finally a dictionary. The variable an is changing dynamically, and its values are different data types, which is the characteristic of dynamic language.
5. "="
The "=" assignment operator is calculated from right to left.
A = 1 b = 2 c = aqb # calculate the value of aqb before assigning it to c print (c)
6. Python allows you to assign values to multiple variables simultaneously
(for example: a = b = c = 1, in the end, everyone is 1). You can also assign values to multiple variables at the same time, separated by commas, one by one.
For example: a, b, c = 1, 2, 3, and finally an is 1, and b is 2, and c is 3.
Do not equate the equal sign of an assignment statement with the equal sign of mathematics.
X = 1 x = x + 2
If it is mathematically understood that x = x + 2 is not true anyway, in the program, the assignment statement first evaluates the expression x + 2 on the right, gets the result 3, and then assigns it to the variable x. Since the previous value of x is 1, after reassigning, the value of x becomes 3.
When a = 'ABC', the Python interpreter does two things:
A string object of 'ABC'' is created in memory
Create a variable named an in memory and point it to 'ABC'.
You can also assign one variable a to another variable b, which actually points variable b to the data pointed to by variable a
For example, the following code:
A = 'Jack' b = an a =' Tom' print (b) print (a)
Is the content of the final variable b 'Jack' or' Tom'?'? If you understand it mathematically, you will mistakenly conclude that the same as an is' Tom', 'but in fact the value of b is still' Jack'!
Keep in mind that everything in Python is an object, and variables are references to objects!
Figure:
Execute a = 'Jack', interpreter to create the string' Jack' object and variable a, and point a to the 'Jack' object
Execute b = a, and the interpreter creates the variable b and points it to the string 'Jack' object that the variable a points to
Execute the a = 'Tom', interpreter to create the string' Tom' object and change a to point to the 'Tom' object, regardless of b.
Third, constant
Constants are invariant variables. For example, the commonly used mathematical constant, pi, is a constant. In Python, constants are usually represented by all uppercase variable names:
PI = 3.14159265359
But in fact, from the perspective of Python syntax, PI is still a variable, because Python does not have any mechanism to guarantee that PI will not be changed. You can assign PI a value of 10 without popping up any errors. Therefore, it is only customary to use all uppercase variable names to represent constants.
Constants are usually placed at the top of the code and are used globally.
About what the variables and constants in Python are shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.