My friends, for reprint please indicate the source: blog.csdn.net/jiangjunsho…

Disclaimer: During the teaching of artificial intelligence technology, many students asked me some python related questions, so in order to let students master more extended knowledge and better understand AI technology, I asked my assistant to share this Python series of tutorials, hoping to help you! Since this Python tutorial is not written by me, it is not as funny and boring as my AI teaching. But its knowledge points or say in place, also worth reading! PS: if you don’t understand this article, please read the previous article first. Step by step, you won’t feel difficult to learn a little every day!

Let’s look at what happens when we assign a variable multiple times:

>>> a = 3      # It's an integer

>>> a = 'spam' 		# Now it's a string

>>> a = 1.23      # Now it's a floating point
Copy the code

A starts as an integer, becomes a string, and finally becomes a floating point number. This example may seem particularly strange to C programmers, because when we say a = ‘spam’, the type of A seems to change from an integer to a string. That’s not the case. In Python, the situation is simple: variable names have no type.

In the previous example, we just changed a to a reference to a different object. Since the variable has no type, we are not actually changing the type of variable A, just referring to a different type of object. In effect, a Python variable refers to a particular object at a particular time.

Objects, on the other hand, know their own type. Each object contains a header that marks the type of the object. For example, the integer object 3 contains the value 3 and a header that tells Python that this is an integer object. The identifier for the object of the ‘spam’ string refers to a string type. Because objects record their types, variables do not need to be recorded.

Note that types in Python are associated with objects, not variables. In typical code, a given variable will refer to only one type of object. This isn’t necessary, though, so you’ll find Python code more flexible than traditional C speech code: Python code can automatically work in multiple types.