python变量

In this tutorial we will know some basics about Python Variable. In our previous tutorial we discussed about Python print function.

在本教程中,我们将了解有关Python变量的一些基础知识。 在之前的教程中,我们讨论了有关Python打印功能的信息。

Python变量 (Python Variable)

In your mathematics books, you probably heard about variables. Python Variables are use to store values.

在您的数学书中,您可能听说过变量。 Python变量用于存储值。

When you declare a variable in python, some space regarding the variable is preserved in memory. Then you can access them. If you read our tutorial on Python data types, you should probably know about pythons data type.

当您在python中声明变量时,有关该变量的一些空间将保留在内存中。 然后,您可以访问它们。 如果您阅读了有关Python数据类型的教程,那么您可能应该了解pythons数据类型。

Python声明变量 (Python declare variable)

In most common programming language like c, c++, java etc, you have to set the datatype of the variable when you declare the variable.

在最常见的编程语言(如c,c ++,java等)中,声明变量时必须设置变量的数据类型。

Python python is flexible about this. You can declare a variable and then the data-type of the variable depends on the data you’re storing into it. See the following example.

Python python对此很灵活。 您可以声明一个变量,然后变量的数据类型取决于您存储在其中的数据。 请参见以下示例。

# declare a variable
var = 'new variable'print('The type of var is :',type(var))  # the type is strvar = 23.0
print('Now, the type of var is :', type(var))  # the type is float

So you will see the output like this.

这样您将看到这样的输出。

多变量分配 (Multiple Variable Assignment)

In our all these tutorials, you have never seen about these. Well, you saw about single variable value assignment.

在所有这些教程中,您从未见过这些。 好了,您看到了有关单个变量值分配的信息。

But you can also assign values to multiple values altogether too. You have to keep the value in the rightmost side.

但是您也可以将值分配给多个值。 您必须将值保留在最右边。

This idea is not used because we do not need to use this kind of assignment. But perhaps you project may need this idea and there is nothing wrong to learn new things. However, see the following code to understand multiple variable assignment.

之所以不使用此想法,是因为我们不需要使用这种分配。 但是也许您的项目可能需要这个想法,并且学习新事物没有错。 但是,请参阅以下代码以了解多变量分配。

# assign multiple variable with the same value
var1 = var2 = var3 = 'init'# print the value of each variable separately
print('Value of var1 :', var1)
print('Now, value of var1 :', var2)
print('Again, value of var1 :', var3)

So, the output of the following code will be

因此,以下代码的输出将是

Value of var1 : init
Now, value of var1 : init
Again, value of var1 : init

关于python变量的一些注意事项 (Some notes about python variable)

Some notes regarding creating a Python variable name is given below.

下面提供了有关创建Python变量名称的一些注意事项。

  • Python variable names cannot be start with numbersPython变量名称不能以数字开头
  • It cannot be start with special characters不能以特殊字符开头
  • Python variable names cannot be same as any python’s pre-defined keywords.Python变量名称不能与任何python的预定义关键字相同。
  • The variable names should be written in camelCase变量名称应使用camelCase编写

Python打印变量 (Python print variable)

We can use print() function to print the variable to console, as you have seen in above programs.

如上面的程序所示,我们可以使用print()函数将变量打印到控制台。

Python变量范围 (Python variable scope)

Python variable scope depends on where it’s declared in the program. We have explained python variable scope in more details in python namespace post.

Python变量作用域取决于在程序中声明的位置。 我们已经在python名称空间发布中更详细地解释了python变量作用域。

That’s all for Python variable. For any query please use the comment box below.

Python变量就这些了。 对于任何查询,请使用下面的评论框。

翻译自: https://www.journaldev.com/15186/python-variable

python变量

python变量_Python变量相关推荐

  1. 含类定义的完整python程序_Python——变量,运算,条件,循环

    Python Python开发 Python语言 Python--变量,运算,条件,循环 注释 1.注释是什么 注释就是给代码做的一些简短的说明,让我们更好的去理解代码,注意程序执行的时候,不会去执行 ...

  2. python的常量和变量_python变量和常量

    变量 什么是变量? 变量,是用于在内存中存放程序数据的容器 计算机的最核心功能就是"计算", 计算需要数据源,数据源要存在内存里,比如我要把小明的姓名.身高.年龄信息存下来,后面程 ...

  3. python保存变量_Python变量存储

    参考文献1:http://makaidong.com/maikerniuniu/1280_9073599.html 参考文献2:https://www.cnblogs.com/fandx/p/1046 ...

  4. python保存变量_python | 变量-保存与命名规则

    1 变量生成 python中生成变量无须事先声明,系统会根据赋值或表达式运算结果值,自动推断变量类型.在python中,变量生成的语法如下:变量名 = 数据或表达式 x = 123 type(x) # ...

  5. python怎么定义int变量_Python 变量类型 | 菜鸟教程

    Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...

  6. python定义一个整数变量_Python变量与常量

    1.什么是变量 a=1,其中 a 就是变量名称,1 就是它的值.在程序运行过程中,变量的值一般都会发生改变,内存中会专门开辟一段空间,用来存放变量的值,而变量名将指向这个值所在的内存空间.与变量相对的 ...

  7. python格式化输出多个变量_Python变量的格式化输出

    print() 函数使用以 % 开头的转换说明符对各种类型的数据进行格式化输出. 转换说明符(Conversion Specifier)只是一个占位符(也称为格式化操作符),它会被后面表达式(变量.常 ...

  8. python定义int变量_Python变量以及常用数字类型(上)

    好好学习,天天向上.又到了齐小猴写笔记的时间,今天的内容是python 变量以及常用数字类型,废话不多说,撸起袖子开始写 变量 1.说到变量,先回顾上一篇说过的标识符,自己定义,自己命名,由字母,下划 ...

  9. python怎样创建变量_Python变量的创建

    Python 是一种动态类型语言,因此变量不需要显式地声明其数据类型.在Python 中,所有的数据都被抽象为"对象",变量通过赋值语句来指向对象,变量赋值的过程就是将变量与对象关 ...

最新文章

  1. java swing图形界面开发 java.swing简介
  2. POJ 1279 Art Gallery 半平面交 多边形的核
  3. 结合随机微分方程,多大Duvenaud团队提出无限深度贝叶斯神经网络
  4. 苹果推送消息服务(转)
  5. 盘点Spring Boot最核心的27个注解
  6. 字符串之替换字符串中连续出现的指定字符串
  7. 【题解】Luogu P2347 砝码称重
  8. 面试pythone_python面试常见问题有哪些
  9. SqlServer-RBAC五表权限
  10. SQL Server 2012软件安装教程
  11. PHP微信h5棋牌程序制作session共享方案梳理
  12. 使用BarTender连接Excel文件批量打印图片
  13. Apollo无人驾驶系统基础入门(云服务+感知(一))
  14. HTML中长度和颜色的单位分别有哪些?
  15. python移位运算,python移位运算
  16. containers matlab,Matlab 中实用数据结构之 containers.Map
  17. 发一些收藏的收费MD5
  18. JFinal 极速开发框架--5.ActiveRecord
  19. SQL语句的基础教程(二)
  20. 使用计算机翻译的是,利用计算机进行中英文翻译的探讨

热门文章

  1. PHP面向对象学习五 类中接口的应用
  2. uml定义的使用的关系
  3. WinFrom 中 label背景透明
  4. [转载] 1006- Python 字典(Dictionary) items()方法
  5. Eclipse自动生成作者、日期注释等功能设置
  6. IDEA: 遇到问题Error during artifact deployment. See server log for details解决方法
  7. 【原创】使用Ultra Librarian为Altium Designer 09生成元器件库
  8. 截获3389远程登陆的密码
  9. OpenIN2 Linux 编译OpenCV 报错undefined reference to `xxx@xxx‘
  10. CRLF对GIT DIFF的影响