Jea*_*ius 40 python curly-braces parentheses braces
刚开始想出Python.我已经阅读了这个问题及其回答:
我仍然无法理解大括号是如何工作的,特别是因为像简单程序这样的页面:
http://wiki.python.org/moin/SimplePrograms
在整个地方使用花括号.我理解方括号和常规弯曲括号,但我不知道"定义词典"是什么意思或它们应该代表什么.
Bre*_*ker 77
Python中使用"Curly Braces"来定义字典.字典是将一个值映射到另一个值的数据结构 - 有点像英语字典如何将单词映射到其定义.
蟒蛇:
dict = {
"a" : "Apple",
"b" : "Banana",
}
Run Code Online (Sandbox Code Playgroud)
它们也用于格式化字符串,而不是使用%的旧C样式,如:
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']
Run Code Online (Sandbox Code Playgroud)
它们不用于表示代码块,因为它们是许多"类C"语言.
C:
if (condition) {
// do this
}
Run Code Online (Sandbox Code Playgroud)
chr*_*tan 17
在Python中,花括号用于定义字典.
a={'one':1, 'two':2, 'three':3}
a['one']=1
a['three']=3
Run Code Online (Sandbox Code Playgroud)
在其他语言中,{}用作流控制的一部分.然而,Python使用缩进作为其流控制,因为它专注于可读代码.
for entry in entries:
code....
Run Code Online (Sandbox Code Playgroud)
当涉及到牙箍时,Python中有一个小复活节彩蛋.尝试在Python Shell上运行它并享受.
from __future__ import braces
Run Code Online (Sandbox Code Playgroud)
Python中的字典是存储键值对的数据结构.您可以像关联数组一样使用它们.声明字典时使用大括号:
d = {'One': 1, 'Two' : 2, 'Three' : 3 }
print d['Two'] # prints "2"
Run Code Online (Sandbox Code Playgroud)
大括号不用于表示Python中的控件级别.相反,Python为此目的使用缩进.
我认为你一般需要一些很好的资源来学习Python.请参阅/sf/ask/12250101/
归档时间: |
|
查看次数: |
109815 次 |
最近记录: |