Python complete tutorial
  • Python Complete Tutorial
  • About this book
  • What you need to prepare
  • 1️⃣Try python for the first time
    • Install python
    • Hello world!
    • Hello world in a nutshell
    • The first simple python project
    • most useful libraries
    • Recommended websites
  • 2️⃣Data structure and basic operations
    • Python data structure
    • Data structure without hash table
    • Data structure with hash table
    • Variability and address
    • basic python programming
    • basic python programming 2
    • basic python programming 3
    • some additions
    • Fibonacci sequence
    • Judging prime numbers
    • txt/csv file operation
  • 🐍Practice program
    • 🚩fancy print
    • 🚩Remove duplicate elements
    • 🚩Palindrome detection
  • 😎leetcode
    • what is leetcode
  • 3️⃣Data mining and machine learning
    • What is data mining
    • iris data set
    • Mean median mode
    • Harmonic mean
    • Histogram
    • Correlation algorithm
    • Gaussian distribution data set
    • projection
    • PCA
    • MDS
    • Bayesian and Frequentist
    • Data normalization
    • binary SVM
    • One Hot Encoding
    • Multi-class SVM
    • Accuracy and error rate
    • Confusion matrix & Accuracy, Precision, Recall
    • F1 score
    • ROC and AUC
  • 4️⃣big data and data visualization
    • line chart
    • Parallel coordinates
    • Histogram chart
  • 5️⃣Mathematical algorithm and digital signal processing
    • Mathematical constants and basic operations
    • Normal distribution
    • Permutation and combination
    • Bernoulli distribution
    • Chaotic system
  • 6️⃣Classes and design patterns
    • Classes and design patterns
  • 7️⃣Operate the database with python
    • MySQL
      • Install MySQL
      • First try MySQL
      • MySQL Architecture
      • database operations
      • database
  • 8️⃣Cryptography
    • beginning of Cryptography
  • 9️⃣deep learning
    • What is Deep Learning
    • basic
  • 💔algorithm
    • Algorithms and Data Structures
Powered by GitBook
On this page
  1. Data structure and basic operations

Variability and address

Quite important

PreviousData structure with hash tableNextbasic python programming

Last updated 3 years ago

Python is a label language, which is very different from the C language.

In python, each variable name is a pointer to the corresponding memory unit.

>>> a = 5
>>> a
5
>>> id(a)
140735234512784
>>> 

We can use the id() method to get the address of the memory unit pointed to by a variable.

In this example, the variable name a points to the memory address numbered 140735234512784, and the memory address stores an int type data, the value of this data is 5.

As shown in the figure below:

If I copy a variable in python, for example, as shown below:

>>> a = 5
>>> b = a

>>> a
5
>>> b
5

>>> id(a)
140735234512784
>>> id(b)
140735234512784
>>> 

You will find that the memory address units pointed to by a and b are the same! That is to say, when I tried to copy a variable, I didn't really clone the variable, but: I created another pointer points to the memory, and the two pointers point to the same memory space.

This means that when I try to change the content of a, the content of b will also change???

Not always, This is the real trouble!

If I change the value of a, but the address of a does not change, the value of b will also change accordingly.

Then we say that this is a variable data type.

We need to pay extra attention to this pattern!

But if I change the value of a, but a points to another address, then b still points to the original address, and the values of a and b are different.

Then we say that this is an immutable data type.

Numbers are immutable

>>> a = 5
>>> b = a

>>> id(a)
140735234512784
>>> id(b)
140735234512784

>>> a = 10
>>> b
5

>>> id(a)
140735234512944
>>> id(b)
140735234512784
>>> 

Number is an immutable data type. When I assign another number to a variable, the address of the variable changes.

Strings are immutable

>>> a = 'hello'
>>> a[2] = 'p'
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
    a[2] = 'p'
TypeError: 'str' object does not support item assignment
>>> 

The string itself cannot be modified, So when we modify the string, python will report an error

So when the string is updated, The original string is deleted, the pointer must point to another memory location, the previous address must be changed.

>>> a = 'hello'
>>> b = a

>>> id(a)
3074560303408
>>> id(b)
3074560303408

>>> a = 'world'
>>> b
'hello'

>>> id(a)
3074560304176
>>> id(b)
3074560303408
>>> 

String is an immutable data type. When I assign another string to a variable, the address of the variable changes as the old string is deleted.

Lists are Variable

>>> a = [1, 2, 3]
>>> b = a

>>> id(a)
3074560050560
>>> id(b)
3074560050560

>>> a[2] = 10
>>> b
[1, 2, 10]

>>> id(a)
3074560050560
>>> id(b)
3074560050560
>>> 
>>> a = [1, 2, 3]
>>> b = a

>>> id(a)
3074560050688
>>> id(b)
3074560050688

>>> a.append(4)
>>> b
[1, 2, 3, 4]

>>> id(a)
3074560050688
>>> id(b)
3074560050688
>>> 

The list is changeable, and every time the list is modified, the variable pointer of the list will not change, so when the list is copied, it cannot be simply done by assignment.

In python, we call this method of assignment shallow copy, which means that we just copy a label pointer, and the memory still shares a location with the previous variable.

We call a full copy of the memory a deep copy. In a deep copy, the data in the memory is completely copied. Of course, this will also consume more memory.

There are many ways to deep copy, for example, we can write a loop.

>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> b = []
>>> for i in a:
	b.append(i)
>>> b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> id(a)
1777018729856
>>> id(b)
1777019369664

>>> a[3] = 9999
>>> a
[0, 1, 2, 9999, 4, 5, 6, 7, 8, 9]
>>> b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 

You may find it a bit troublesome to do so, so we can complete the deep copy through list construction.

>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> b = [i for i in a]
>>> b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> id(a)
1777019294592
>>> id(b)
1777019369024
>>> 

b = [i for i in a] This is a very simple deep copy scheme. No need to introduce any libraries.

Of course, the problem is that we sometimes use high-dimensional data. If we still need to nest multiple loops in this way, it would be a little troublesome.

So we can use python library to complete deep copy.

>>> import copy
>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> b = copy.deepcopy(a)
>>> b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> id(a)
1777019283520
>>> id(b)
1777019368832
>>> 

b = copy.deepcopy(a)

This is a deep copy done using a copy library. If your data is high-dimensional data, then this is a good choice (this library python is already installed by default, and you don’t need to install it yourself)

Tuples are immutable

>>> a = tuple(range(5))
>>> a
(0, 1, 2, 3, 4)

>>> a[2] = 4
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
    a[2] = 4
TypeError: 'tuple' object does not support item assignment
>>> 

Tuples are unchangeable variables, so this problem does not exist with tuples.

Sets are Variable

>>> a = set(range(5))
>>> b = a

>>> id(a)
1777019500128
>>> id(b)
1777019500128

>>> a.add(10)
>>> b
{0, 1, 2, 3, 4, 10}

>>> id(a)
1777019500128
>>> id(b)
1777019500128
>>> 

Tuples are changeable, so a deep copy must also be performed.

Dictionary are Variable

>>> a = {'name': 'tom', 'age': 24}
>>> b = a

>>> id(a)
1777019370816
>>> id(b)
1777019370816

>>> a['job'] = 'doctor'
>>> b
{'name': 'tom', 'age': 24, 'job': 'doctor'}

>>> id(a)
1777019370816
>>> id(b)
1777019370816
>>> 

Dictionary is changeable, so a deep copy must also be performed.

Statistics

Start time of this page: December 19, 2021

Completion time of this page: December 19, 2021

2️⃣
Page cover image
Variables and memory
Shared memory
Scenario 1
Scenario 2