search
HomeBackend DevelopmentPython TutorialThe difference between assignment in python and c language

The difference between assignment in python and c language

What is the difference between assignment in python and c language? Let’s first take a look at what a simple Python code looks like in memory:

b = 3
b = b + 5

It The operation diagram in memory is as follows:

The difference between assignment in python and c language

However, from the literal meaning of the code, "Assign 3 to b, add 5 to b and then assign Give it to b."

That is, the code looks like this:

b ← 3
b ← b + 5

So the following operation diagram in memory may be more in line with our intuition:

The difference between assignment in python and c language

That is, the value of b 5 is written back to b. This is what a typical C program looks like. Allocate a memory unit of type int for variable b, and then store the integer 3 in the memory unit. b represents the block of memory space and will no longer move. The value of b can be updated, but the address of b in the memory will no longer change. So we say b = b 5, which is equivalent to b ← b 5. After adding 5 to the value of b, it is still placed in b. Variable b is tightly bound to the memory space where it resides.

Related recommendations: "Python Video Tutorial"

Looking at the memory diagram in Python above, b 5 gets a new value, and then makes b point to this new value. In other words, what it does is this:

b → 3
b → b + 5

Make b point to 3, and then make b point to the new value of b 5.

The C program updates the value stored in the memory unit, while Python updates the pointer to the variable.

Variables in C programs store a value, while variables in Python point to a value.

If the C program indirectly manipulates data by manipulating memory addresses (each variable corresponds to a fixed memory address, so manipulating variables means manipulating memory addresses), and the data is in a passive position, then Python directly manipulates it. Data, data is in an active position, and variables only exist as a reference relationship and no longer have a storage function.

In Python, each data occupies a memory space. For example, the new data b 5 also occupies a brand new memory space.

This operation of Python makes data the subject, and data interacts directly with data.

Data is called an object in Python.

This sentence is not too rigorous. But it works in this simple example.

An integer 3 is an int type object, a 'hello' is a string object, and a [1, 2, 3] is a list object.

Python regards all data as "objects". It allocates a memory space for each object. After an object is created, its id no longer changes.

id is the abbreviation of identity. It means "identity; identification".

In Python, you can use id() to obtain the id of an object, which can be regarded as the address of the object in memory.

After an object is created, it cannot be destroyed directly. Therefore, in the previous example, variable b first points to object 3, and then continues to execute b 5. b 5 produces a new object 8. Since object 3 cannot be destroyed, let b point to the new object 8 instead of Use object 8 to overwrite object 3. After the code execution is completed, there is still object 3 and object 8 in the memory, and the variable b points to object 8.

If there is no variable pointing to object 3 (that is, it cannot be referenced), Python will use the garbage collection algorithm to decide whether to recycle it (this is automatic and does not require programmer to worry about it).

An old object cannot be overwritten. New data generated due to interaction with the old object will be placed in the new object. In other words, each object is an independent individual, and each object has its own "sovereignty". Therefore, the interaction of two objects can produce a new object without affecting the original object. In large programs, the interactions between objects are complex, and this independence makes these interactions safe.

The C program assigns a fixed memory address to each variable, which ensures the independence between C variables.

C language is the interaction between variables (that is, memory addresses), and Python is the interaction between objects (data). These are two different ways of interacting.

The above is the detailed content of The difference between assignment in python and c language. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Python's Hybrid Approach: Compilation and Interpretation CombinedPython's Hybrid Approach: Compilation and Interpretation CombinedMay 08, 2025 am 12:16 AM

Pythonusesahybridapproach,combiningcompilationtobytecodeandinterpretation.1)Codeiscompiledtoplatform-independentbytecode.2)BytecodeisinterpretedbythePythonVirtualMachine,enhancingefficiencyandportability.

Learn the Differences Between Python's 'for' and 'while' LoopsLearn the Differences Between Python's 'for' and 'while' LoopsMay 08, 2025 am 12:11 AM

ThekeydifferencesbetweenPython's"for"and"while"loopsare:1)"For"loopsareidealforiteratingoversequencesorknowniterations,while2)"while"loopsarebetterforcontinuinguntilaconditionismetwithoutpredefinediterations.Un

Python concatenate lists with duplicatesPython concatenate lists with duplicatesMay 08, 2025 am 12:09 AM

In Python, you can connect lists and manage duplicate elements through a variety of methods: 1) Use operators or extend() to retain all duplicate elements; 2) Convert to sets and then return to lists to remove all duplicate elements, but the original order will be lost; 3) Use loops or list comprehensions to combine sets to remove duplicate elements and maintain the original order.

Python List Concatenation Performance: Speed ComparisonPython List Concatenation Performance: Speed ComparisonMay 08, 2025 am 12:09 AM

ThefastestmethodforlistconcatenationinPythondependsonlistsize:1)Forsmalllists,the operatorisefficient.2)Forlargerlists,list.extend()orlistcomprehensionisfaster,withextend()beingmorememory-efficientbymodifyinglistsin-place.

How do you insert elements into a Python list?How do you insert elements into a Python list?May 08, 2025 am 12:07 AM

ToinsertelementsintoaPythonlist,useappend()toaddtotheend,insert()foraspecificposition,andextend()formultipleelements.1)Useappend()foraddingsingleitemstotheend.2)Useinsert()toaddataspecificindex,thoughit'sslowerforlargelists.3)Useextend()toaddmultiple

Are Python lists dynamic arrays or linked lists under the hood?Are Python lists dynamic arrays or linked lists under the hood?May 07, 2025 am 12:16 AM

Pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)Theyarestoredincontiguousmemoryblocks,whichmayrequirereallocationwhenappendingitems,impactingperformance.2)Linkedlistswouldofferefficientinsertions/deletionsbutslowerindexedaccess,leadingPytho

How do you remove elements from a Python list?How do you remove elements from a Python list?May 07, 2025 am 12:15 AM

Pythonoffersfourmainmethodstoremoveelementsfromalist:1)remove(value)removesthefirstoccurrenceofavalue,2)pop(index)removesandreturnsanelementataspecifiedindex,3)delstatementremoveselementsbyindexorslice,and4)clear()removesallitemsfromthelist.Eachmetho

What should you check if you get a 'Permission denied' error when trying to run a script?What should you check if you get a 'Permission denied' error when trying to run a script?May 07, 2025 am 12:12 AM

Toresolvea"Permissiondenied"errorwhenrunningascript,followthesesteps:1)Checkandadjustthescript'spermissionsusingchmod xmyscript.shtomakeitexecutable.2)Ensurethescriptislocatedinadirectorywhereyouhavewritepermissions,suchasyourhomedirectory.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools