Python Parentheses Cheat Sheet

Master how parentheses work in Python to learn to code faster.
By Boris Delovski • Updated on Nov 25, 2022

Beginner programmers tend to gloss over the key detail of what type of parentheses they should use when learning Python. Beginners usually focus on other aspects of programming in the excitement of learning something new, and don't think about the necessity of what type of parentheses they actually need in their code until they're used incorrectly and Python throws a syntax error. That’s why it's important to understand what each type of parentheses in Python represents and how to use each type of parentheses correctly in your Python code.

In this article, I'll cover what standard parentheses, square brackets, and curly braces represent to Python when it interprets the code you've written. Knowing these basic facts can help you choose the right type of parentheses when you start working on something new. Parentheses used incorrectly are the leading cause of syntax errors for beginners in their Python code, so mastering how parentheses work in Python will also make it easier for you to learn to code faster and with fewer frustrating moments.

 

 

Standard Parentheses - ( )

Aside from defining the order of operations in mathematical and boolean operations, standard parentheses are commonly used for a few different things:

• Invoking functions

• Creating instances of a class or instances of an object

• Generators

 

Broadly speaking, the primary use of parentheses in Python is to call an object. That is the reason why standard parentheses are sometimes called the "call operator." Aside from their main use, parentheses are also used to define generator expressions. The biggest misconception about standard parentheses is that they're necessary to create a tuple. Although you will often see people use parentheses when defining tuples, they are not necessary for the process of tuple creation.

 

Here is an example of the call operator in Jupyter notebook:

Call Operator in Python

 

Invoking Functions

Parentheses are necessary when you want to invoke functions. Calling on the name of a function without following it by parentheses will point towards the function object, but will not call the function itself. The code inside the body of the function will not get executed. 

Example function in Jupyter notebook:

Function in Jupyter notebook

Output shown in Jupyter notebook:

Output in Jupyter notebook

Output in Jupyter notebook continued

 

 

Creating Instances

Instance creation is the process of creating new objects from classes. In Python, all built-in data types have their instance creation methods, but if you want to create a custom object, you need to create a custom class. 

Here is an example of creating objects of in-built data types in Jupyter notebook:

creating an object of in-built data types

Here is an example of creating custom objects in Jupyter notebook:

Creating custom objects

 

Generators

Generators are a special kind of iterator that you use to avoid loading all elements of some of your data into memory. Using generators, you can render elements one-by-one. Generators are defined similarly to a function, with the addition of the yield keyword which prompts the construction of the next element. Typically, you define generators by creating an expression very similar to that of a list comprehension. The difference is that a generator is defined using parentheses, while list comprehensions are defined using square brackets.

Example of generators in Jupyter notebook: 

Example of generators in Jupyter notebook

 

Square brackets - [ ]

Square brackets are commonly used in Python for: 

• Lists 

• Retrieving items from collections

 

Lists

Lists, as mutable collections, are one of the basic data types inside Python. You use square brackets to create lists for both empty lists and those that have items inside them.

 Example of lists in Jupyter notebook: 

Example of lists in Jupyter notebooks with brackets

 

 

List Comprehensions

Put in simple terms, list comprehensions are an easy and elegant way of creating new lists from existing lists and are usually used to replace loops. An example of using a loop and a list comprehension to achieve the same result.

A loop in Jupyter notebook:

Loop with square brackets in Jupyter notebook

List comprehension example in Jupyter notebook:

List comprehension with square brackets in Jupyter notebook

 

Retrieving Items from Collections

Square brackets are also used to retrieve single items or multiple items from collections. When you want to retrieve a single item from a collection, you just need to specify the index of that item, or the key in case you are working with dictionaries, inside square brackets. 

Under the hood, square brackets invoke the __getitem__ method. Here's how to access single items from the following string, list, and dictionary.

Retrieving items in Jupyter notebook: 

Retrieving items in a list

Accessing the third character from the string in Jupyter notebook:

Example of accessing a third character in a string

Accessing the first item from the list in Jupyter notebook:

Example of accessing first item in a string

Accessing the value from the dictionary defined by the key "Sandra" in Jupyter notebook:

Accessing the value from the dictionary defined by the key "Sandra"

As you can see, using square brackets is a more elegant way of accessing items than using __getitem__. Therefore, it is best to avoid using __getitem__ for retrieving items. That doesn't mean that __getitem__ doesn't have its place, on the contrary, you'll often use it when writing custom classes. In custom classes, you need to define __getitem__ as a method if you want to access elements of the object created by the custom class using square brackets.

You can also use square brackets to retrieve so-called slices of data. Slices are retrieved very similarly to single items. The only difference is that you don't specify an index inside the square brackets, but you instead specify a range of indexes. This allows you to access a part of some collection of items easily.

The standard formulation when using square brackets to retrieve a slice of data is [start:end:step]. The step part is often omitted when wanting to retrieve a whole subset of a collection. When skipped, the step variable defaults to one. Here's how you can retrieve a slice of data:

Here's some example data I've created in Jupyter notebook:

Example data

Now, access all items between 10 and 18 (including 10 and 18), skipping every second item:

access all items in an interval, skipping every second item

It is important to mention that there is essentially no difference between retrieving a single item and retrieving a slice. Both processes use __getitem__ in the background.

Also, slices are treated by Python as classes, which means that you can achieve the same result I achieved with the code above by writing the following line of code in Jupyter notebook:

Code example in Jupyter notebook

 

Curly braces - { }

One of the biggest differences between Python and other popular programming languages is that in Python, curly braces are not used to create program blocks for flow control. In Python, indentation is used for flow control, which makes Python much easier to read than most other programming languages.

That being said, curly braces do have their uses in Python. Curly braces are commonly used to:

• Create dictionaries

• Create sets

• Format strings

 

Dictionaries

Dictionaries are created in Python using curly braces. You can use curly braces to create both empty dictionaries and dictionaries that contain key-value pairs. 

Example of curly braces to create dictionaries in Jupyter notebook:

Creating dictionaries with curly brackets

Of course, you can always create dictionaries using the dict() method, but that way of creating dictionaries is not used very often. Using curly braces is also faster than invoking dict(), because curly braces are a part of Python's syntax and do not require a function call.

Similarly to how you can use list comprehensions and square brackets to create lists, you can use curly braces and dict comprehensions to create dictionaries. 

Here's an example of creating dictionaries with curly brackets in Juptyer notebook:

Creating dictionaries with curly bracktes

 

 

Sets

Sets are collections of mutable, unique, hashable values. When working with sets, you can treat them as dictionaries that contain only keys and no values. They are not used as often as dictionaries and are usually used as an easy way to remove duplicates from a collection. A set is created by entering values instead of pairs inside curly braces.  

An example of creating sets in Juptyer notebook:

Creating sets in Jupyter notebook

However, creating empty sets is not done by using curly braces. If you try to just leave nothing between the curly braces, Python will automatically create a dictionary. Therefore, to create an empty set you must invoke set()

Example of a set() in Juptyer notebook:

Example of a set() in Jupyter notebook

 

Formatting Strings

The standard way to format strings in Python is to use a combination of curly braces and standard parenthesis, by inserting empty curly braces in the place where you want to add something to a string.

Example of formatting strings in Jupyter notebook: 

Formatting strings in Jupyter notebook

Of course, the same can be done using variables:

formatting strings in python using variables

You can also format strings by using keyword arguments:

Formatting strings using keyword arguments

 

However, as of Python 3.6, an alternative and more elegant way of formatting strings was introduced using f-strings. By using f-strings, you can completely avoid using standard parentheses and instead use only curly braces. You just need to add an f before the string to signal to Python that you are going to use that new functionality to format strings. 

Using f-strings is much simpler, as show in this example in Jupyter notebook:

Using f-strings to format strings in Jupyter notebook

As you can see, using f-strings, you can directly insert variables inside curly braces. This gives you an easy way to precisely assign variable values to certain spots in a string, and even to manipulate those same values. 

Example of assigning variable values using f-strings in Jupyter notebook: 

Assigning variable values using f-strings in Jupyter notebook

In this article, I've demonstrated some of the different uses for standard parentheses, square brackets, and curly braces in Python that you can use as a cheat sheet. 

Boris Delovski

Data Science Trainer

Boris Delovski

Boris is a data science trainer and consultant who is passionate about sharing his knowledge with others.

Before Edlitera, Boris applied his skills in several industries, including neuroimaging and metallurgy, using data science and deep learning to analyze images.