花括号的含义是什么?

问题描述:

刚刚开始弄清楚Python。我已经阅读了这个问题及其答案:

Just starting to figure Python out. I've read this question and its responses:

Is it true that I can't use curly braces in Python?

仍然无法理解花括号的工作原理,尤其是由于诸如简单程序这样的页面:

and I still can't fathom how curly braces work, especially since pages like Simple Programs:

http://wiki.python.org/moin/SimplePrograms

在各处使用花括号。我了解方括号和规则的弯曲括号,但是我不知道定义字典的含义或它们应该代表的含义。

use curly braces all over the place. I understand square brackets and regular curved parentheses, but I don't know what's meant by "defining dictionaries" or what they're supposed to represent.

大括号在Python中用于定义字典。字典是一种将一个值映射到另一个值的数据结构,就像英语词典如何将单词映射到其定义一样。

"Curly Braces" are used in Python to define a dictionary. A dictionary is a data structure that maps one value to another - kind of like how an English dictionary maps a word to its definition.

Python:

dict = {
    "a" : "Apple",
    "b" : "Banana",
}

它们也用于格式化字符串,而不是使用%的旧C样式,例如:

They are also used to format strings, instead of the old C style using %, like:

ds = ['a', 'b', 'c', 'd']
x = ['has_{} 1'.format(d) for d in ds]

print x

['has_a 1', 'has_b 1', 'has_c 1', 'has_d 1']

它们不用于表示代码块,因为它们在许多类似于C的语言中。

They are not used to denote code blocks as they are in many "C-like" languages.

C:

if (condition) {
    // do this
}