python中的变量
This lesson deals with variables. Those who already know some programming must be familiar with the concept of variable and its importance. Variable are nameplates that we put on empty boxes(memory locations) in which we can store any value(data) and once we are done using a variable the nameplate is removed(going out of scope) from the box(memory location) which then can have a new nameplate. Also, in python one box can have multiple nameplates.
本课涉及变量。 那些已经知道一些编程的人必须熟悉变量的概念及其重要性。 变量是放置在空盒子(内存位置)上的铭牌,我们可以在其中存储任何值(数据),一旦使用了变量,便从盒子(内存位置)中移除(超出范围)铭牌,然后可以有一个新的铭牌。 另外,在python中,一个盒子可以有多个铭牌。
In python everything is an object and variables are just names given to identify these objects.
在python中,一切都是对象, 变量只是用来标识这些对象的名称。
For example:
例如:
x = 10
In the code above, x
is a variable or a label or a name for the object 10
.
在上面的代码中, x
是对象10
的变量或标签或名称。
If we have the following code:
如果我们有以下代码:
x = 10
y = 10
Then for the object 10
we have two labels(references), x
and y
.
然后对于对象10
我们有两个标签(引用) x
和y
。
Hence, we can say that variables provide a way of labelling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as nameplates for containers that hold information. Their sole purpose is to label the data stored in the memory. This data can then be used throughout your program.
因此,可以说变量提供了一种使用描述性名称标记数据的方式,因此读者和我们自己都可以更清楚地理解我们的程序。 将变量视为保存信息的容器的铭牌会很有帮助。 它们的唯一目的是标记存储在内存中的数据。 然后可以在整个程序中使用此数据。
Let's see some examples. A variable is supposed to have a name. There are some rules to assign a name to the variable.
让我们看一些例子。 变量应该有一个名字 。 有一些规则为变量分配名称。
The variable name can consist of alphabet(s), number(s) and underscore(s) only.
变量名称只能包含字母,数字和下划线。
The first character in variable's name cannot be a number. Hence
i-am-variable
,variable!
,1variable
,#variable
, are all invalid variable names. Whilei_am_variable
,variable
,variable1
,_variable
, are all valid names.变量名称中的第一个字符不能为数字。 因此,
i-am-variable
,variable!
,1variable
,#variable
variable都是无效的变量名称。 尽管i_am_variable
,variable
,variable1
,_variable
均为有效名称。
将值存储在变量中 (Storing value in a variable)
Time to see how we can store a value in our variable. Consider a variable named x
, we want this variable to store a numeric value or a number, say 11
. Then in order to do that just go to IDLE and type:
是时候看看我们如何在变量中存储值了。 考虑一个名为x
的变量,我们希望该变量存储一个数值或数字,例如11
。 然后,要执行此操作,只需转到IDLE并输入:
>>> x = 11
And press Enter key. With that our variable gets created, named x
, and with a default value 11
stored in it, using the equal to =
operator. Remember, Equal to operator is always used to assign a specific value to any variable. Variable's name will always be on the left side and it's value will be on the right. Let's create another variable, say y
and assign it value 25
.
然后按Enter键。 这样,我们便创建了名为x
变量,并使用等于=
运算符存储了默认值11
。 请记住, 等于运算符始终用于为任何变量分配特定值。 变量的名称将始终在左侧 ,其值将在右侧 。 让我们创建另一个变量y
并将其赋值为25
。
>>> y = 25
Now in the current IDLE session, python is dealing with two variables, x
and y
, which have values 11
and 25
respectively. If you want to check the value of any variable in IDLE, simply type the name of that variable in a new line and press the Enter key. The value stored in it will be printed on the IDLE screen in a new line.
现在在当前的IDLE会话中,python处理两个变量x
和y
,它们的值分别为11
和25
。 如果要检查IDLE中任何变量的值,只需在新行中键入该变量的名称,然后按Enter键。 存储在其中的值将在新行中显示在IDLE屏幕上。
>>> x
11
>>> y
25
Now try to look at the below code, what do you think this will do?
现在尝试看下面的代码,您认为这会做什么?
>>> x = y
As you can see on the LHS(Left Hand Side) we have x
and y
on the RHS(Right Hand Side), hence as we explained before, the value on the right will get assigned to the variable on the left. Since y
has a value 25
stored in it, this statement will modify the value inside x
from 11
to 25
. And hence if you ask again for the value of x
, it'll be 25
now. In this case, we just over-wrote the value inside x
variable.
正如您在LHS(左手边)上看到的那样,我们在RHS(右手边)上有x
和y
,因此,正如我们之前所解释的,右边的值将分配给左边的变量。 由于y
中存储的值为25
,因此此语句会将x
内的值从11
修改为25
。 因此,如果您再次要求x
的值,则现在为25
。 在这种情况下,我们只重写了x
变量中的值。
>>> x
25
![Variables in Python](https://i-blog.csdnimg.cn/blog_migrate/db3aa8e5655fa601997f5fe05cacec85.png)
Once you are done with the above example, let's be more creative this time while naming our variables. Let's create a variable and assign your name as its value. So here we are having a box named name
, which is capable of storing any word. The process is quite similar to what we did above but with just a tiny change. Watch carefully,
完成上述示例后,这次让我们在命名变量时更具创造力。 让我们创建一个变量并将其名称分配为其值。 因此,这里有一个名为name
的框,它可以存储任何单词。 该过程与我们上面所做的非常相似,但仅有很小的变化。 小心点
>>> name = "Sudytonight"
As you can see, we quoted our website's name within the double quotation marks. This is because we don't want python compiler to get confused. Since Studytonight is a word (or more precisely, string
in the programming world), we will have to surround it with quotation marks. By doing so we tell python that it is a word. But, what is supposed to happen if we write Studytonight without the quotation marks? Like this,
如您所见,我们在双引号中引用了我们网站的名称。 这是因为我们不想让python编译器感到困惑。 由于Studytonight是一个单词(或更准确地说,是编程世界中的string
),因此我们将不得不用引号将其引起来。 通过这样做,我们告诉python这是一个词。 但是,如果我们在不带引号的情况下写Studytonight会发生什么? 像这样,
>>> name = Studytonight
Since there is no quotation marks, python will consider Studytonight
as another variable and will try to find the value stored within it so that it can further assign it to the variable name
. But since we never declared any variable with the name Studytonight
, python won't be able to find any value for it and in the end, it will throw an error saying that the variable with name Studytonight
is not defined.
由于没有引号,因此python会将Studytonight
视为另一个变量,并尝试查找存储在其中的值,以便进一步将其分配给变量name
。 但是,因为我们从未声明过任何名称为Studytonight
变量,所以python将无法为其找到任何值,最后,它将引发错误,指出未定义名称为Studytonight
的变量。
Also, you can use both single quotation as well as double quotation in order to represent a word (or string
).
另外,您可以同时使用单引号和双引号来表示一个单词(或string
)。
>>> name = "Studytonight.com"
>>> name = 'Studytonight'
Both are fine.
两者都很好。
![Variables in Python](https://i-blog.csdnimg.cn/blog_migrate/953f394572572d922dd30b6efc8503a9.png)
Now, you can also try to use variables with math functions like we learned in the last tutorial. Try to use the variables as the arguments for the function. For example,
现在,您也可以尝试将变量与数学函数一起使用,就像我们在上一教程中学到的那样。 尝试将变量用作函数的参数。 例如,
>>> x = 3
>>> y = 2
>>> pow(x, y)
9
>>> z = -7
>>> abs(z)
7
Next, you can try to save the answer of any mathematical function in a variable. Like,
接下来,您可以尝试将任何数学函数的答案保存在变量中。 喜欢,
>>> p = pow(5, 2)
>>> import math
>>> q = math.log(x)
Try using mathematical operators with these variables. Like,
尝试对这些变量使用数学运算符 。 喜欢,
>>> x+y
5
>>> x-y
1
Try using these variables in a mathematical expression including some mathematical function and operators, all together, like,
尝试在数学表达式中使用这些变量,包括一些数学函数和运算符 ,例如,
>>> x = pow(x**y, p+2)
In the above code, python will first calculate the expression on the RHS(Right Hand Side) first, and will use the old value for variable x
, which is 3
and once the expression is solved the answer will be stored in the variable x
, which will become its new value.
在上面的代码中,python首先将在RHS(Right Hand Side)上首先计算表达式,并将变量x
的旧值使用3
,一旦表达式被解决,答案将存储在变量x
,这将成为它的新价值。
![Variables in Python](https://i-blog.csdnimg.cn/blog_migrate/dae069730e34dcac52637b2663d7124e.png)
翻译自: https://www.studytonight.com/python/variables-in-python
python中的变量