Is a number in python a variable type

This article uses Python3 as an example, including six data types:

1、 Number:
int integer
long
float
complex
Boolean (0 and 1)
2、 String
3、 List
4、 Tuple
5、 Dictionary
6、 Set

Use the id function to view the memory address of the variable i for verification (use hex(id(i)) to view the hexadecimal memory address)

Immutable types: Number, String, Tuple

Re-assigning an immutable type variable is actually re-creating an immutable type object and re-pointing the original variable to the newly created object (if no other variables refer to the original object (that is, the reference count is 0), The original object will be recycled).

For the immutable type int, no matter how many immutable types are created, as long as the values are the same, they all point to the same memory address.

For example Number:

 number1=123id(number1)8790928028112
 number2=123id(number2)8790928028112
 number3=321id(number3)49143760

It can be seen that when i += 1 is executed, the memory address will change because the int type is immutable.

 i=5id(i)8791205700368
 i+=1id(i)8791205700400
   
 j=i
 id(j)8791205700400
 j
6

Variable types: List, Dictionary, Set

The reassignment of the variable data type does not create a new object.

For example List:

 list1=[1,2,3,4,5]id(list1)49307976
 list2=[1,2,3,4,5]id(list2)49307784
 list1.append(123)
 list1
[1,2,3,4,5,123] id(list1)49307976

If you execute list1=list2, because list1 and list2 point to the same memory address, and the types of list1 and list2 are both List, and the variable type, modifying any one of list1 and list2 will affect the value of the other List.

 list1
[1,2,3,4,5,123] id(list1)49307976
   
 list1=list2
 id(list1)49307784id(list2)49307784
 list2
[1,2,3,4,5]
 list2.append(456)
 list1
[1,2,3,4,5,456]
 list2
[1,2,3,4,5,456]

all in all:

The immutable type refers to the value (Value), the value does not change, the object does not change; the value changes, the object changes.

The variable data type refers to the name (Name), the name does not change, the object does not change; the name changes, the object changes.

Content expansion:

**Variable and immutable data in Python? **

  1. Variable types include list and dict. Immutable types include string, number, tuple.
  2. When the modification operation is performed, the variable type transfers the address in the memory, that is, directly modifies the value in the memory without opening up new memory.
  3. When the immutable type is changed, the value in the original memory address is not changed, but a new memory is opened, the value in the original address is copied over, and the value in the newly opened memory is operated on.

So far, this article about whether numbers in python are variable types is introduced here. For more related content, are numbers in python immutable types, please search for ZaLou.Cn's previous articles or continue to browse related articles below. Hope everyone Support ZaLou.Cn a lot in the future!

Recommended Posts

Is a number in python a variable type
What is a sequence table in Python
Is there a helper function in python
What is introspection in python
What is object-oriented in python
What is Python variable scope
What is list comprehension in python
Is there function overloading in python
Python is a cross-platform language code
Everything in Python is an object
Is there an algorithm in python language
Write a Qixi confession artifact in Python
What is an anonymous function in Python
Can variable types be declared in python
How to sort a dictionary in python
Python is mainly used in which directions
What is the function of adb in python
Python 3.9 is here!
Functions in python
How to simulate gravity in a Python game
Install Python3 environment in a brand new Ubuntu
How to write a confession program in python
The best way to judge the type in Python
How to understand a list of numbers in python
How to create a Python virtual environment in Ubuntu 14.04
Python implements FTP to upload files in a loop
Python is short-crawling music
03. Operators in Python entry
Python is slowly fading
Join function in Python
12. Network Programming in Python3
print statement in python
Concurrent requests in Python
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
Write gui in python
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
How to find the area of a circle in python
A large inventory of commonly used third-party libraries in Python