Any programming language needs to process data, such as numbers, strings, characters, etc. we can use the data directly or save the data to variables for future use.
Variable can be regarded as a small box, which is specially used to "hold" the data in the program. Each variable has a unique name. The data in the variable can be found by the name of the variable.
From the bottom, the data in the program will eventually be put into memory (memory module). Variables are actually the name of this memory.
Corresponding to variables are constants, which are small boxes used to "hold" data. The difference is that the data saved by variables can be modified many times, while constants cannot be modified once a certain data is saved.
Assignment of Python variables
In programming languages, the process of putting data into variables is called Assignment. Python uses the equal sign = as the Assignment operator. The specific format is:
ame = value
Name indicates variable name; Value represents the value, that is, the data to be stored. Finally, if your time is not very tight and you want to improve python quickly, the most important thing is not afraid of hardship. It is suggested that you can pay the price: 762459510. That's really good. Many people make rapid progress and need you not to be afraid of hardship! You can add it and have a look~
Note that a variable is a kind of identifier, and its name cannot be given casually. It is necessary to follow the python identifier naming standard, and avoid duplicate names with Python built-in functions and python reserved words.
For example, the following statement assigns the integer 10 to the variable n:
n=10
Since then, n represents the integer 10, and using N means using 10.
More examples of assignment:
pi = 3.1415926 #Assign pi to variable piurl =“ https://jq.qq.com/?_wv=1027&k=D6twYIX2 " #Assign the address of the Python tutorial to the variable strreal = True #Assign a Boolean value to the variable real
The value of a variable is not immutable. It can be modified at any time as long as it is re assigned; In addition, you don't have to care about the type of data. You can assign different types of data to the same variable. See the following Demo:
n = 10 #Assign 10 to the variable nn = 95 #Assign 95 to the variable nn = 200 #Assign 200 to the variable nabc = 12.5 #Assign decimal to variable abcabc = 85 #Assign integer to variable abcabc =“ https://jq.qq.com/?_wv=1027&k=D6twYIX2 " #Assign a string to the variable abc
Note that once the value of a variable is modified, the previous value is overwritten, no longer exists, and can no longer be found. In other words, a variable can hold only one value.
In addition to assigning a single data, you can also assign the running result of the expression to a variable, for example:
sum = 100 + 20 #Assign the result of addition to the variable REM = 25 * 30% 7 #Assign the remainder to the variable str = "python AC circle" +“ https://jq.qq.com/?_wv=1027&k=D6twYIX2 " #Assign the result of string splicing to a variable
Use of Python variables
When using Python variables, you only need to know the name of the variable.
Variables can be used almost anywhere in Python code. See the following demonstration:
>>> n = 10>>> print(n) #Pass variable to function 10 > > > m = n * 10 + 5 #Take variables as part of four operations > > > print(m)105>>> p rint(m-30) #An expression consisting of variables is passed as an argument to function 75 >>> m = m * 2 #Double the value of the variable itself > > > print(m)210 >>> url = "https://jq.qq.com/?_wv=1027&k=D6twYIX2" >>> str = "Python Communication group:" + url #String splicing > > > print(str)Python Communication group: https://jq.qq.com/?_wv=1027&k=D6twYIX2
Python is a weakly typed language. In a strongly typed programming language, when defining variables, the type of variables must be specified, and the assigned data must be of the same type. C language, C + +, Java are the representatives of strongly typed languages. Let's take C + + as an example to demonstrate the use of variables in strongly typed languages:
Python is a weakly typed language
In strongly typed programming languages, the type of variables should be specified when defining variables, and the assigned data must also be of the same type. C language, C + + and Java are the representatives of strongly typed languages.
Let's take C + + as an example to demonstrate the use of variables in strongly typed languages:
int n = 10; //int indicates integer type n = 100 ;n = "https://jq.qq.com/?_wv=1027&k=D6twYIX2"; //Error: cannot assign string to integer type url = "https://jq.qq.com/?_wv=1027&k=pDEhzaCh"; //Error: variables without specified type are undefined and cannot be used.
Corresponding to strongly typed languages are weakly typed languages. Python, JavaScript, PHP and other scripting languages are generally weakly typed.
Weakly typed languages have two characteristics:
- Variables can be assigned directly without declaration. Assigning a value to a non-existent variable is equivalent to defining a new variable.
- The data type of a variable can be changed at any time. For example, the same variable can be assigned as an integer and a string.
Note that a weak type does not mean no type! Weak typing means that you don't have to pay attention to types when writing code, but there are still types inside the programming language. We can use the type() built-in function class to detect the type of a variable or expression, for example:
>>> num = 10 >>> type(num)<class 'int'> >>> num = 15.8 >>> type(num) <class 'float'> >>> num = 20 + 15j >>> type(num) <class 'complex'> >>> type(3*15.6) <class 'float'>
This is the end of this article on Python variables. Finally, Xiaobian is a python development engineer. Here I have sorted out a set of the latest Python system learning tutorials, including basic Python scripts, web development, crawlers, data analysis, data visualization, machine learning, etc. If you want these materials, you can pay attention to the Xiaobian and get them in the background private letter Xiaobian: "01".
The text and pictures of this article come from the Internet and their own ideas. They are only for learning and communication. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.