What is the difference between literal and variables in Python? [closed]

好久不见. 提交于 2019-11-27 15:38:07
timss

A literal is notation for representing a fixed (const) value.
A variable is storage location associated with a symbolic name (pointed to, if you'd like).

It's best explained in use:

foo = bar(42)
^     ^   ^
|     |   |--- literal, 42 is *literally* 42
|     |------- function, also represents "something" in memory
|------------- variable, named "foo", and the content may vary (is variable)

In any programming language a Literal is a constant value, where as identifiers can change their values. Identifiers can store literals and process them further. Identifiers are name given to variables.

1, 1.5, 'a', "abc", etc. are examples for literals. But in the statement x=123, x is a variable and 123 is a Literal.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!