Home Backend Development Python Tutorial The difference between assignment in python and c language

The difference between assignment in python and c language

Jun 24, 2019 pm 01:09 PM
c python Assignment

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
Copy after login

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
Copy after login

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
Copy after login

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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles