Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

what is the difference between curly brace and square bracket in python?

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

when I print A and B on my terminal, they made no difference. Is it real?

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

E.g. A=[], B={}

Is there any difference there?

like image 486
Haoyu Chen Avatar asked Mar 13 '14 21:03

Haoyu Chen


People also ask

What does the {} mean 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 does {} brackets mean in Python?

[] brackets are used for lists. List contents can be changed, unlike tuple content. {} are used to define a dictionary in a "list" called a literal.

What is the difference between parentheses brackets and braces in Python?

What is the difference between parentheses (), brackets [], and braces {} in a python code? Brackets are used to make lists Braces are used to make dictionary Parenthesis are used to make tuple But for indexing in all of those, only brackets are used.

What are {} brackets used for?

Brackets: In a paper, use brackets to signify important information added to direct quotes. The brackets tell the reader that the information is added to further explain the quote.


2 Answers

Curly braces create dictionaries or sets. Square brackets create lists.

They are called literals; a set literal:

aset = {'foo', 'bar'} 

or a dictionary literal:

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

or a list literal:

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

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

Sets are collections of unique elements and you cannot order them. Lists are ordered sequences of elements, and values can be repeated. Dictionaries map keys to values, keys must be unique. Set and dictionary keys must meet other restrictions as well, so that Python can actually keep track of them efficiently and know they are and will remain unique.

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

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

Note the comma in the another_tuple definition; it is that comma that makes it a tuple, not the parenthesis. WARNING_not_a_tuple is not a tuple, it has no comma. Without the parentheses all you have left is a string, instead.

See the data structures chapter of the Python tutorial for more details; lists are introduced in the introduction chapter.

Literals for containers such as these are also called displays and the syntax allows for procedural creation of the contents based of looping, called comprehensions.

like image 109
Martijn Pieters Avatar answered Sep 21 '22 08:09

Martijn Pieters


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'> 
like image 27
Daenyth Avatar answered Sep 18 '22 08:09

Daenyth