Python 1-04 variables and constants

Variables and constants

1. Variables

Variables in Python do not need to be declared, and each variable must be assigned a value before it is used.

1. Assignment operator =

Note: The meaning is different from the equal sign in mathematics. It is not equal here.

E.g:

counter = 100   # 整型变量 
miles = 100.0  # 浮点型变量 
name = "Python"  # 字符串 

2. Multiple variable assignment

Python allows multiple variables to be assigned at the same time. E.g:

a = b = c = 1

The above example creates an integer object with a value of 1, assigning values ​​from back to front, and three variables are assigned the same value.

a, b, c = 1, 2, "Python"

In the above example, two integer objects 1 and 2 are assigned to variables a and b, and the string object "Python" is assigned to variable c.

a,b = b,a  # 交换 a,b 所指对象 

Variables are assigned to create and open up memory space. If it is not assigned and used directly, it will throw the exception referenced before the assignment or an unnamed exception.

NameError: name 'a' is not defined

3. Dynamic language

You can also re-assign after assignment

>>> a = 1
>>> a = "Python"
>>> a &#

Guess you like

Origin blog.csdn.net/weixin_43955170/article/details/112476676