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我们有两个标签(引用) xy

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. While i_am_variable, variable, variable1, _variable, are all valid names.

    变量名称中的第一个字符不能为数字。 因此, i-am-variablevariable!1variable#variable variable都是无效的变量名称。 尽管i_am_variablevariablevariable1_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处理两个变量xy ,它们的值分别为1125 。 如果要检查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(右手边)上有xy ,因此,正如我们之前所解释的,右边的值将分配给左边的变量。 由于y中存储的值为25 ,因此此语句会将x内的值从11修改为25 。 因此,如果您再次要求x的值,则现在为25 。 在这种情况下,我们只重写了x变量中的值。

>>> x
25

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'

Live Example →

现场示例→

Both are fine.

两者都很好。

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 ,这将成为它的新价值。

翻译自: https://www.studytonight.com/python/variables-in-python

python中的变量

python中的变量_Python中的变量相关推荐

  1. python中定义数据结构_Python中的数据结构—简介

    python中定义数据结构 You have multiples algorithms, the steps of which require fetching the smallest value ...

  2. python算法和数据结构_Python中的数据结构和算法

    python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...

  3. python函数的定义域_python中多线程的变量定义域问题

    最近遇到了一个还算常见的错误提示:local variable 'xxx' referenced before assignment 字面上的意思就是该变量在引用前还未定义,于是我去代码里找到了这个变 ...

  4. python中变量类型在程序中可以改变_Python中的变量和数据类型,python,及

    变量 变量的定义: 在程序中,有时我们需要对2个数据进行求和,那么该怎样做呢? 大家类比一下现实生活中,比如去超市买东西,往往咱们需要一个菜篮子,用来进行存储物品,等到所有的物品都购买完成后,在收银台 ...

  5. python 类函数 成员函数_python中的类函数、静态函数、成员函数以及类变量、成员变量...

    1 classMethodDemo():2 classVar = 'This is a class variable' #类变量,可在类函数或者成员函数中引用,为所有实例共有的 3 def __ini ...

  6. python如何复制一个变量_Python中变量、赋值、浅拷贝、深拷贝

    在理解浅拷贝和深拷贝之前,首先要理解学习一下变量在Python中是怎样存储的: 变量的类型是分值引用与地址引用两种. python的一切变量都是对象,变量的存储,采用了地址引用的方式,存储的只是一个变 ...

  7. python的常量和变量_python中的常量和变量代码详解

    局部和全局变量: # name='lhf' # def change_name(): # # global name # name='帅了一比' # print('change_name',name) ...

  8. python 获取值类型用于定义变量_Python中的变量和简单数据类型

    变量: 变量是存储在内存中的值,在创建变量时会在内存中开辟一个空间. 变量可以指定不同的数据类型,这些变量可以存储整数,小数或字符串. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存 ...

  9. python特殊方法大全_python中星号变量的几种特殊用法

    在Python中星号除了用于乘法数值运算和幂运算外,还有一种特殊的用法"在变量前添加单个星号或两个星号",实现多参数的传入或变量的拆解,本文将详细介绍"星号参数" ...

最新文章

  1. 2019春季学期进度报告(十六)
  2. Test on 11/14/2016
  3. Linux7改运行级别,Centos7 修改运行级别
  4. setiosflags(ios::fixed)和setprecision()
  5. 设计模式学习笔记 1.介绍
  6. Firefox 3中的快捷键!
  7. Ubuntu源码安装Tomcat7
  8. Jquery表单验证 只能输入数字,
  9. 【转载】 C#中decimal.TryParse方法和decimal.Parse方法的异同之处
  10. 秘笈——掌控时间管理的工具
  11. HTML请选择编程,html – 设计选择标记
  12. Linux设备驱动模型-Device
  13. 《终极算法》阅读笔记与摘要(1)-序和第1-2章
  14. ISO27000系列标准
  15. MySQL--Select条件判断、in、not in、and、or、以及like匹配字符语句查询
  16. 在线上传图片二维码识别解析
  17. 国内外优秀的设计素材网站推荐
  18. 自上而下 or 自下而上?企业部署RPA的2种策略
  19. 在ios上运行安卓计算机软件,安卓竟然也能运行iOS,苹果怒了!
  20. 中国蒸汽眼罩市场销售趋势及营销渠道策略报告(新版)2022-2027年

热门文章

  1. 蛋疼--吐槽一下 从魅族、小米和山寨说开去
  2. 关于阵列发射端的波束形成(相控阵)研究与仿真实践
  3. python爬虫,爬取下载图片
  4. 从0到1搭建Nacos2.0.2集群(centos7)
  5. 在photoshop中如何设置一张a4纸上打印多个照片,如小二寸图片排版
  6. 推荐一个不错的英文IT网站
  7. C++11 lambda表达式与函数对象
  8. RecycleView小结
  9. win7+Ubuntu16.04双系统安装的那些事!
  10. java 实现杂志订阅管理系统设计