Advancing in Data Science, One Byte at a Time!

DataScience Ville Logo
Search
Close this search box.

Brackets vs Parentheses vs Braces in Python – What’s the Difference?

brackets vs parentheses vs braces in Python

It’s common to get confused by brackets, parentheses, and braces. Many times in writing, we often use them interchangeably. But do that in Python, and you’ll find your code returning errors.

That’s just how confusing programming can get. Before we delve into the “brackets vs parentheses vs braces in Python”, let’s first understand “brackets vs parentheses vs braces in the English language”.

What is the Difference Between Brackets, Parentheses, and Braces?

BRACKETS are the common term for “square brackets [ ]”

while

PARENTHESES are the common term we use to refer to “curly brackets ( )”

and

BRACES is the right term for the “squiggly brackets { }”

But they take on different meanings in Python:

Brackets vs Parentheses vs Braces in Python

BRACKETS = Lists

BRACES = Tuples

PARENTHESES = Dictionaries

So, what are lists, tuples, and dictionaries? And how are they different? To start with, let’s consider “Lists.”

LISTS

In Python, a collection of data separated by commas and enclosed in brackets is called a list. For instance, the following data is a Python list:

example_list = ['Conan','Sherlock','John','Holmes']

The great thing about lists is that they can be modified. Yes, using a set of different methods, you can remove, add, and insert more data to your list. For instance, say we want to insert the name “Watson” to the above list, all you need to do is use the “append” method:

example_list.append('Watson')

But when it comes to Tuples, things take a different turn.

TUPLES

This is a collection of data separated by commas and commonly enclosed in parentheses.

For instance, take the following example:

example_tuple = ('Conan','Sherlock','John','Holmes')

Now, here’s the thing about tuples: “they can’t be modified.” So unlike lists, you can’t add, remove from, or append to them.

Also, to write a tuple, the parentheses are optional. That means the following code is also a tuple:

example_tuple = 'Conan','Sherlock','John','Holmes'

So, we’ve got lists and tuples covered. What about dictionaries?

DICTIONARIES

Unlike lists and tuples, dictionaries are a set of data used to assign a value to a key and are often enclosed in curly braces.

Here’s an example:

example_dic = {'name':'Conan','job':'Author','character':'Sherlock Holmes'}

In the above example, the values “Conan, Author, and Sherlock Holmes” were attributed to their respective keys “name, job, and character”

You can access the values of each key with the following format:

print(name_of_dictionary['KEY'])

Wrapping Up

So, once again, here are the major differences between lists, tuples, and dictionaries in Python:

But you might ask when you should use each of these data types. Well, it all depends on what you want for your code.

If you need to modify your list of items, using a “List” will be easier. If not, you can make use of a “Tuple.”

And if you’re looking for a way to attribute values to a particular key or variable, a dictionary is the way to go.

Recent Posts

Subscribe for Regular Updates

 Get Insightful News on Data-related Topics

SUBSCRIBE FOR REGULAR UPDATES

Discover more from Kester R. on a wider range of topics here on: