What is the difference between a curly bracket and a square bracket in Python?

What is the difference between curly bracket and square bracket in python?

A ={1,2} B =[1,2] 

when I print A and B on my terminal, they have no meaning. It's real?

And sometimes I noticed that some code uses {} and [] to initialize different variables.

eg. A=[] , B={}

Is there any difference?

+47
python syntax curly-braces square-bracket
Mar 13 '14 at 21:33
source share
3 answers

Dictionaries or sets are created in braces. Square brackets create lists .

They are called literals; set of literals:

 aset = {'foo', 'bar'} 

or dictionary literal:

 adict = {'foo': 42, 'bar': 81} empty_dict = {} 

or list literal:

 alist = ['foo', 'bar', 'bar'] empty_list = [] 

To create an empty set, you can only use set() .

Sets are collections of unique items and you cannot order them. Lists are ordered sequences of items, and values ​​can be repeated. Dictionaries map keys to values; keys must be unique. Dial and dictionary keys must also meet other restrictions, so Python can effectively track them and know that they are and will remain unique.

There is also a tuple type, using a comma for 1 or more elements, with parentheses being optional in many contexts:

 atuple = ('foo', 'bar') another_tuple = 'spam', empty_tuple = () WARNING_not_a_tuple = ('eggs') 

Note the comma in the definition of another_tuple ; it is a comma that makes it tuple , not a bracket. WARNING_not_a_tuple not a tuple, it does not have a comma. Without parentheses, all you have left is a string.

See the Python textbook data structure chapter for more details; lists are presented in the chapter.

The literals for such containers are also called displays , and the syntax allows you to program the creation of content based on loops called understandings.

+71
Mar 13 '14 at 21:35
source share

They create different types.

 >>> type({}) <type 'dict'> >>> type([]) <type 'list'> >>> type({1, 2}) <type 'set'> >>> type({1: 2}) <type 'dict'> >>> type([1, 2]) <type 'list'> 
+9
Mar 13 '14 at 21:37
source share

These two braces are used for different purposes. If you just want the list to contain some elements and organize them by index numbers (starting at 0), just use [] and add elements as needed. {} are special in that you can provide a user identifier to values ​​such as a = {"John": 14} . Now, instead of making a list with age and remembering whose age you are, you can simply access John's age on a["John"] .

[] is called a list, and {} is called a dictionary (in Python). Dictionaries are basically a convenient list form that allows you to access data much easier.

However, there is a catch in the dictionaries. In many cases, the data you put in the dictionary does not remain in the same order as before. Therefore, when you go through each value one at a time, it will not be in the order that you expect. There is a special dictionary for this, but you must add this line from collections import OrderedDict and replace {} with OrderedDict() . But I do not think that this time you will need to worry.

0
Jul 18 '15 at 18:52
source share



All Articles