python中的文字是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34189086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are literals in python?
提问by silenz
I am a new programmer currently studying the nuances of python. My question to you is, what exactly is a literal in python? I searched for the answer on google, and in the python docs, but google just references to string literals, and the python docs don't explicitly state what a literal is. I came across this page http://www.dalkescientific.com/writings/NBN/python_intro/literals.htmlthat provided the following answer:
我是一名新程序员,目前正在研究 Python 的细微差别。我的问题是,python 中的字面量到底是什么?我在 google 和 python 文档中搜索了答案,但 google 只是引用了字符串文字,而 python 文档没有明确说明文字是什么。我发现这个页面http://www.dalkescientific.com/writings/NBN/python_intro/literals.html提供了以下答案:
Objects are also called data structures. Python comes with some built-in objects. Some are used so often that Python has a quick way to make these objects, called literals. The literals include the string, unicode string, integer, float, long, list, tuple and dictionary types.
对象也称为数据结构。Python 带有一些内置对象。有些使用得非常频繁,以至于 Python 有一种快速的方法来创建这些对象,称为文字。文字包括字符串、Unicode 字符串、整数、浮点数、长整型、列表、元组和字典类型。
Is this correct? Can I assume that literals are just another term for python's built-in objects? Are there any more literals that were not covered in the description? I was under the impression that there was such a thing as a binary literal, and that booleans were also considered literals.
这样对吗?我可以假设文字只是 python 内置对象的另一个术语吗?是否还有更多文字未包含在说明中?我的印象是有一种二进制文字这样的东西,布尔值也被认为是文字。
回答by Tom Karzes
In this context, I think it just means that the Python parser accepts a built-in syntax for these entities. For instance, "abc"
is a str
, 123
is an int
, 1.2
is a float
, etc.
在这种情况下,我认为这只是意味着 Python 解析器接受这些实体的内置语法。例如,"abc"
是一个str
,123
是一个int
,1.2
是一个float
,等等。
回答by chepner
A literal is something that the parser recognizes as syntax for writing an object directly. Some examples in Python 2:
文字是解析器识别为直接写入对象的语法的东西。Python 2 中的一些示例:
0
,1
,2
,3
(int
literals)-1
,-2
, etc are not literals themselves, but expressions involving the unary-
operator.
5j
,3.14j
(non-negative pure imaginarycomplex
literals)- Other complex values, like
2+3j
,2-3j
, and-5j
, are expressions involving various operators.
- Other complex values, like
3.5
,-2.7
(float
literals)""
,"hello"
(str
literals)u""
,u"hello"
(unicode
literals)None
(I thinkthis is considered a literal, and not a keyword or simple name.)
0
,1
,2
,3
(int
字面量)-1
,-2
等不是文字本身,而是涉及一元运算-
符的表达式。
5j
,3.14j
(非负纯虚数complex
)- 其他复数值,如
2+3j
、2-3j
和-5j
,是涉及各种运算符的表达式。
- 其他复数值,如
3.5
,-2.7
(float
文字)""
,"hello"
(str
文字)u""
,u"hello"
(unicode
文字)None
(我认为这被视为文字,而不是关键字或简单名称。)
Other literal-like expressions called displays are used to create various containers. They aren't true literals because they can contain non-literal expressions (e.g., [x, y]
).
其他称为 display 的类似文字的表达式用于创建各种容器。它们不是真正的文字,因为它们可以包含非文字表达式(例如,[x, y]
)。
[]
,[1,2]
(list
displays)()
,(1,)
,(1,2)
(tuple
displays){}
,{'a': 2}
(dict
displays){1,2,3}
(set
display introduced in Python 2.7)
[]
,[1,2]
(list
显示)()
,(1,)
,(1,2)
(tuple
显示){}
,{'a': 2}
(dict
显示){1,2,3}
(set
显示在 Python 2.7 中引入)
There is no literal or display for an empty set, as the obvious notation {}
is
already a dict
display. Python 2 does not have true Boolean literals; True
and False
are simply built-in names for the Boolean objects. A tuple is technically created by the comma, and the parentheses are only necessary when you need to disambiguate the expression; the exception is the empty tuple ()
.
空集没有文字或显示,因为明显的符号{}
已经是dict
显示。Python 2 没有真正的布尔文字;True
和False
只是布尔对象的内置名称。元组在技术上是由逗号创建的,只有在需要消除表达式歧义时才需要括号;例外是空元组()
。
See phihag's answer for some differences in Python 3.
有关 Python 3 中的一些差异,请参阅 phihag 的回答。
回答by phihag
literal is a language-independent programming term. A literal is a succinct and easily visible way to write a value. For example, you could construct the value zero with int()
, but 0
is much more convenient.
文字是一个独立于语言的编程术语。文字是一种简洁且易于查看的写入值的方式。例如,您可以使用 构造零值int()
,但0
更方便。
In Python, the following literals are available:
在 Python 中,可以使用以下文字:
- Character string literals, such as
'Düsseldorf'
,u'K?ln'
(the first one is a byte string literal in Python 2.x) - Byte string literals such as
b'\x89PNG'
- Integer literals such as
42
,1_234
,0x2a
or0b101010
- Float literals such as
1.2e-19
- Imaginaryliterals such as
2.3j
- Boolean literals:
True
andFalse
(not literals in older (2.x) Python versions) None
- Tuples:
(1, 2, 3)
- Lists:
[1, 2, 3]
- Dicts:
{1: 'one', 2: 'two'}
- Sets:
{1, 2, 3}
- 字符串字面量,例如
'Düsseldorf'
,u'K?ln'
(第一个是 Python 2.x 中的字节字符串字面量) - 字节字符串文字,例如
b'\x89PNG'
- 整数文字,例如
42
,1_234
,0x2a
或0b101010
- 浮动文字,例如
1.2e-19
- 虚构的文字,例如
2.3j
- 布尔文字:
True
和False
(不是旧的 (2.x) Python 版本中的文字) None
- 元组:
(1, 2, 3)
- 列表:
[1, 2, 3]
- 字典:
{1: 'one', 2: 'two'}
- 套:
{1, 2, 3}
回答by kindall
Can I assume that literals are just another term for python's built-in objects?
我可以假设文字只是 python 内置对象的另一个术语吗?
Close. They are a way of writing(some of) Python's built-in objects. If you need, say, a particular number, and you are not reading it from a file or from the user or getting it from a database or calculating it or importing it from another module or or or... you can use a numeric literal to put it right there in your code.
关闭。它们是一种编写(一些)Python 内置对象的方式。如果您需要,比如说,一个特定的数字,并且您不是从文件或用户那里读取它,也不是从数据库中获取它或计算它或从另一个模块导入它,或者……你可以使用数字文字把它放在你的代码中。
number = 42
number
here is the integer 42, specified by using a literal.
number
这是整数 42,使用文字指定。
Python's True
and False
values (and None
) are not really literals, but predefined names for those values. But the concept is similar: Python is giving you a way to write those values directly into your code.
Python 的True
和False
值(和None
)并不是真正的文字,而是这些值的预定义名称。但概念是相似的:Python 为您提供了一种将这些值直接写入代码的方法。
Nearly all programming languages have literals for most of their frequently-used types.
几乎所有的编程语言都有用于大多数常用类型的文字。