Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of curly braces? [closed]

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.

like image 892
JeanSibelius Avatar asked Feb 08 '12 16:02

JeanSibelius


People also ask

What is an open curly brace?

An open curly bracket is { and a close curly bracket is } Curly brackets are used to mark what code is in a certain block. To type a curly bracket, hold down the SHIFT key and press the key that has [ and { on it. You can find that key on your keyboard directly to the right of P.

What is a closed brace?

Sometimes referred to as square brackets, a bracket is a punctuation mark found in pairs. There are two types of brackets: the open bracket [ and the closed bracket ] . The open bracket has an open end towards the right, and the closed bracket has an open end towards the left.

What is the meaning of {} in Python?

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.

What are enclosed by curly braces?

Braces ("curly braces") Braces are used to group statements and declarations. The contents of a class or interface are enclosed in braces.


4 Answers

"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}
like image 83
Brenton Alker Avatar answered Oct 05 '22 22:10

Brenton Alker


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
like image 38
chrtan Avatar answered Oct 05 '22 23:10

chrtan


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.

like image 28
Ethan Furman Avatar answered Oct 06 '22 00:10

Ethan Furman


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

like image 23
Fred Larson Avatar answered Oct 06 '22 00:10

Fred Larson