72

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
JeanSibelius
  • 1,529
  • 3
  • 14
  • 27
  • 23
    `from __future__ import braces` – robert Feb 08 '12 at 16:28
  • 3
    You should really go through the [tutorial](http://docs.python.org/py3k/tutorial/). – Michael J. Barber Feb 08 '12 at 16:33
  • 7
    Curly braces are used for [empty/non-empty dictionary](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) as well as [non-empty sets](https://docs.python.org/3/tutorial/datastructures.html#sets) _both_. For initializing _empty_ sets `set()` statement is used instead. – RBT Aug 01 '18 at 03:23
  • Related post - [Is it true that I can't use curly braces in Python?](https://stackoverflow.com/q/1936190/465053) – RBT Aug 01 '18 at 03:28

5 Answers5

109

"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",
}

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']

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

C:

if (condition) {
    // do this
}

Update: In addition to Python's dict data types Python has (since Python 2.7) set as well, which uses curly braces too and are declared as follows:

my_set = {1, 2, 3, 4}
Iguananaut
  • 21,810
  • 5
  • 50
  • 63
Brenton Alker
  • 8,947
  • 3
  • 36
  • 37
  • 3
    Is there any way to get Python to use them for code blocks? Seemingly mad question, I know, but I'm helping a local blind kid transition from [this](https://blogs.microsoft.com/ai/project-torino-microsoft-creates-physical-programming-language-inclusive-visually-impaired-children/) to the language his friends use at school (Python) and white-space indentation on a screen reader is just horrible – dumbledad Apr 27 '18 at 10:29
  • @dumbledad Did you find anything? It seems like a Python-aware screen-reader is called for here - but can it be found? – Josiah Yoder Jun 25 '18 at 18:28
  • 11
    Curly-braces are also used to denote [set literals](https://docs.python.org/3/reference/expressions.html#set-displays), which are like dictionary literals, only without any keys or colons (:). – Josiah Yoder Jun 25 '18 at 18:31
  • @dumbledad you can try this: https://github.com/ofajardo/pytwister – Otto Fajardo May 07 '19 at 08:57
  • @dumbledad: https://python-with-braces.appspot.com/ – naught101 Jun 19 '20 at 05:13
  • Python 3.6 introduced the use of formatted string('f') with curly braces, like so: varName = "Python" print (f"Hello, {varName}") – Harry Jul 15 '23 at 12:16
23

In Python, curly braces are used to define a dictionary.

a={'one':1, 'two':2, 'three':3}
a['one']=1
a['three']=3

In other languages, { } are used as part of the flow control. Python however used indentation as its flow control because of its focus on readable code.

for entry in entries:
     code....

There's a little easter egg in Python when it comes to braces. Try running this on the Python Shell and enjoy.

from __future__ import braces
chrtan
  • 1,704
  • 1
  • 13
  • 17
10

In languages like C curly braces ({}) are used to create program blocks used in flow control. In Python, curly braces are used to define a data structure called a dictionary (a key/value mapping), while white space indentation is used to define program blocks.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
5

Dictionaries in Python are data structures that store key-value pairs. You can use them like associative arrays. Curly braces are used when declaring dictionaries:

d = {'One': 1, 'Two' : 2, 'Three' : 3 }
print d['Two'] # prints "2"

Curly braces are not used to denote control levels in Python. Instead, Python uses indentation for this purpose.

I think you really need some good resources for learning Python in general. See https://stackoverflow.com/q/175001/10077

Community
  • 1
  • 1
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • I've been working my way through Learn Python the Hard Way (which is apparently a bit controversial), and have reached the chapter where I'm supposed to download, print and just read code. I keep running across things I don't know, and curly braces are the only thing I can't wrap my head around when I look them up... hence the question. – JeanSibelius Feb 08 '12 at 16:46
  • @mattshepherd: Maybe it lives up to its name. 8v) Seriously, though, the [Python Tutorial](http://docs.python.org/py3k/tutorial/) (as Michael J. Barber linked) is very good. You might give that a try. – Fred Larson Feb 08 '12 at 17:22
0

A dictionary is something like an array that's accessed by keys (e.g. strings,...) rather than just plain sequential numbers. It contains key/value pairs, you can look up values using a key like using a phone book: key=name, number=value.

For defining such a dictionary, you use this syntax using curly braces, see also: http://wiki.python.org/moin/SimplePrograms

user1034081
  • 618
  • 5
  • 21