This article will help you understand variables and constants in Python

Release: 2023-07-25 14:15:50
forward
2090 people have browsed it

1. The difference between variables and constants

Variable: The amount that changes in value during the running of the program.

Constant: A quantity whose value will not change during the running of the program.

Whether it is a variable or a constant, a space will be opened in the memory when it is created to save its value.


2. Variables

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 declaring the type in advance. For example, the following four variables a, b, c, and d do not have any type declarations such as int a, str b, list c, int d before use, which is required in C language and JAVA programming language. portable.

For example:

a = 4 b = "haha" c = [] d = 9-5
Copy after login

2. Use the "=" sign to assign a value to the variable

a = 100
Copy after login

The equal sign here should be understood and read as "assignment", not "equal to". "Assignment" is an operation on variables, while "equal to" is a comparison of two variables. The four variables a, b, c, and d above are assigned different values through the "=" sign.

3. Each variable must be assigned a value before use, and the variable will not be created until it is assigned a value.

New variables create and open up memory space and save values through the action of assignment. If used directly without assignment, the exception referenced before assignment or an unnamed exception will be thrown.

For example:

a # 孤单单一个a,什么也表示不了,只能报错 a = 1 # 这样就没问题了,解释器知道a是个新变量了 c.append(1) # c是个什么鬼? NameError: name 'c' is not defined
Copy after login

Result:

This article will help you understand variables and constants in Python

This article will help you understand variables and constants in Python

##4. In Python, the variable itself has no concept of data type

The commonly referred to as "variable type" is the type of the object referenced by the variable , or the type of the variable's value.

a = 1 a = "haha" a = [1, 2, 3] a = { "k1":"v1"}
Copy after login

变量a在创建的时候,赋予了值为1的整数类型,然后又被改成字符串“haha”,再又变成一个列表,最后是个字典。变量a在动态的改变,它的值分别是不同的数据类型,这是动态语言的特点。

5. “=”号这个赋值运算符是从右往左的计算顺序

a = 1 b = 2 c = a + b # 先计算a+b的值,再赋给c print(c)
Copy after login

This article will help you understand variables and constants in Python

6. Python允许同时为多个变量赋值

(例如:a = b = c = 1,最终大家都是1)。也可以同时为多个变量赋值,用逗号分隔,逐一对应。

例如:a, b, c = 1, 2, 3,最后a是1,b是2,c是3。

不要把赋值语句的等号等同于数学的等号。

x = 1 x = x + 2
Copy after login

如果从数学上理解x = x + 2那无论如何是不成立的,在程序中,赋值语句先计算右侧的表达式x + 2,得到结果3,再赋给变量x。由于x之前的值是1,重新赋值后,x的值变成3。

a = 'ABC'时,Python解释器干了两件事情:

1. 在内存中创建了一个‘ABC’的字符串对象;

2. 在内存中创建了一个名为a的变量,并把它指向'ABC'。

也可以把一个变量a赋值给另一个变量b,这个操作实际上是将变量b指向变量a所指向的数据,

例如下面的代码:

a = 'Jack' b = a a = 'Tom' print(b) print(a)
Copy after login

最后变量b的内容到底是'Jack'还是'Tom'?如果从数学意义上理解,就会错误地得出b和a相同是‘Tom’,但实际上b的值还是'Jack ‘!

请牢记:Python中的一切都是对象,变量是对象的引用!

图:

This article will help you understand variables and constants in Python

执行a = ‘Jack’,解释器创建字符串‘Jack’对象和变量a,并把a指向‘Jack’对象;

执行b = a,解释器创建变量b,并且将其指向变量a指向的字符串‘Jack’对象;

执行a = ‘Tom’,解释器创建字符串‘Tom’对象,并把a改为指向‘Tom’对象,与b无关。


三、常量

常量就是不变的变量,比如常用的数学常数圆周率就是一个常量。在Python中,通常用全部大写的变量名表示常量:

PI = 3.14159265359
Copy after login

但事实上,从Python语法角度看,PI仍然是一个变量,因为Python根本没有任何机制保证PI不会被改变。你完全可以给PI赋值为10,不会弹出任何错误。所以,用全部大写的变量名表示常量只是一个习惯上的用法。

常量通常放置在代码的最上部,并作为全局使用。


四、总结

本文基于Python基础,主要介绍了Python基础中变量和常量的区别,对于变量的用法做了详细的讲解,用丰富的案例 ,代码效果图的展示帮助大家更好理解 。

The above is the detailed content of This article will help you understand variables and constants in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!