当我给变量赋值时到底会发生什么?

发布于 2025-01-10 11:08:27 字数 322 浏览 3 评论 0原文

我心里一直有一个问题,看起来有点傻!我想了解当我们用 Python 等编程语言为变量赋值时到底会发生什么(例如 a=2)。所有老师都说'a'存储在RAM中,然后将值'2'分配给变量。但如何呢?当我编写此代码然后在计算机键盘上按“Enter”时会发生什么(我的意思是在执行这行代码之后)? 这是我的猜测! ‘a’的ascii码是97,97的二进制表示是(01100001)。 '2' 也是 (00000010)。我认为 RAM 由数十亿个晶体管组成,这些晶体管以矩阵形状粘在一起,关闭和打开的晶体管显示“0”和“1”。这是真的吗?我们的 RAM 内存空间中是否只有 10 亿个晶体管而没有其他东西?
这个假设与现实有多接近?

I have a question in my mind for a while and it looks a little bit silly! I would like to understand what really happen when we assign a value to a variable in a programming language like python (for example a=2). All teacher say 'a' stores in RAM and then the value '2' is assigned to the variable. But how? What happen when I write this code and then push 'Enter' on my computer keyboard(I mean after execute this line of the code)?
This is my guess! the ascii code of 'a' is 97 and the binary representation of 97 is (01100001). and '2' also is (00000010). I think RAM made up billion of transistors stuck together in a matrix shape and the turned off and turned on transistor show '0' and '1'. Is it true? Do we have only billion transistors in RAM memory space and nothing else?
How close is this assumption to reality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2025-01-17 11:08:27

Python 是一种动态类型语言。

因此,Python 不需要显式变量声明来保留内存。

当我们给变量赋值时,变量声明会自动发生。

因此,当您为变量赋值时,如示例所示: a =2

值 2 不会直接赋值给变量“a”。相反,值为 2 的整数对象将被分配给变量“a”。

我相信此链接将有利于您获得更清晰的想法。

希望我能够提供帮助。

Python is a dynamically typed language.

So, Python doesn't need an explicit variable declaration to reserve memory.

The variable declaration will happen automatically when we assign value to it.

So when you assign value to a variable like in your example: a =2

Value 2 will not be directly assigned to the variable 'a'. Instead, an integer object with a value of 2 will be assigned to the variable 'a'.

I believe this link will benefit you in getting a more clarified idea.

Hopefully, I was able to help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文