The Python Oracle

What is the meaning of curly braces?

This video explains
What is the meaning of curly braces?

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3

--

Chapters
00:00 Question
00:40 Accepted answer (Score 106)
01:42 Answer 2 (Score 23)
02:18 Answer 3 (Score 9)
02:44 Answer 4 (Score 5)
03:21 Thank you

--

Full question
https://stackoverflow.com/questions/9197...

Question links:
[Is it true that I can't use curly braces in Python?]: https://stackoverflow.com/questions/1936...
[http://wiki.python.org/moin/SimpleProgra...]: http://wiki.python.org/moin/SimpleProgra...

Accepted answer links:
[set]: https://docs.python.org/3/tutorial/datas...

Answer 4 links:
[https://stackoverflow.com/q/175001/10077]: https://stackoverflow.com/q/175001/10077

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #curlybraces #parentheses #braces

#avk47



ACCEPTED ANSWER

Score 111


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



ANSWER 2

Score 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.




ANSWER 3

Score 6


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




ANSWER 4

Score 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