Parentheses, Square Brackets and Curly Braces in Python
Introduction
Understanding the distinct roles of parentheses, square brackets, and curly braces is crucial for proper syntax and functionality in Python code.
Generally, parentheses ()
are used for grouping expressions, defining functions or passing arguments, and defining tuples. Square brackets []
are used for creating lists, indexing elements and slicing snippets. Curly braces {}
serve two main purposes: defining sets (unordered collections of unique elements) and dictionaries (key-value pairs).
Parentheses ()
- Grouping Expressions
Parentheses()
is often used to explicitly define the order of operations, where expressions within parentheses are evaluated first.
Below is an example of how adding parentheses changes the result of an arithmetic operation and a logic expression.
- Defining and Calling Functions
To define a function in Python, the keyworddef
is used, followed by the function name and a pair of round brackets or parentheses. These parentheses serve to declare input parameters for the function.
- Defining a Tuple
A tuple in Python is an ordered and immutable collection of elements, defined by enclosing values within parentheses. The elements in a tuple follows certain sequence while modifications such as appending, removal, alteration are not allowed. Moreover, it may contain heterogeneous elements, allowing for elements of different data types.Tuples are advantageous when a fixed and unchangeable sequence of elements is needed.
Square Brackets []
- Creating Lists
Square brackets can be used to create a list, an ordered and mutable collection of elements.
- Indexing Elements and Slicing Snippets
To retrieve an element from a collection such as a list or dictionary, square brackets[]
are used.
Square brackets are helpful in selecting out specific rows or columns from a dataframe.
import pandas as pd
# Creates a DataFrame with 'state', 'valuation', and 'championships' columns:
df = pd.DataFrame([
{'team': 'Chicago Bulls', 'state': 'Illinois', 'championships': 6},
{'team': 'Golden State Warriors', 'state': 'California', 'championships': 6},
{'team': 'Los Angeles Lakers', 'state': 'California', 'championships': 17},
{'team': 'Boston Celtics', 'state': 'Massachusetts', 'championships': 17},
{'team': 'Miami Heat', 'state': 'Florida', 'championships': 3}
])
df
team | state | championships |
---|---|---|
Chicago Bulls | Illinois | 6 |
Golden State Warriors | California | 6 |
Los Angeles Lakers | California | 17 |
Boston Celtics | Massachusetts | 17 |
Miami Heat | Florida | 3 |
df[["team"]]
team |
---|
Chicago Bulls |
Golden State Warriors |
Los Angeles Lakers |
Boston Celtics |
Miami Heat |
df[["team", "state"]]
team | state |
---|---|
Chicago Bulls | Illinois |
Golden State Warriors | California |
Los Angeles Lakers | California |
Boston Celtics | Massachusetts |
Miami Heat | Florida |
Square brackets can be used to select out certain rows.
df[df["championships"] > 10]
team | state | championships |
---|---|---|
Los Angeles Lakers | California | 17 |
Boston Celtics | Massachusetts | 17 |
Curly Braces {}
1.Creating Dictionaries
Curly Braces {}
are often used in dictionary creation. A dictionary in Python is an unordered collection of key-value pairs. Each key must be unique, and it is associated with a specific value. Dictionaries provide a flexible and efficient way to store and retrieve data using meaningful identifiers.
- Creating Sets
Curly braces{}
can also be used to define sets, which are unordered collections of unique elements. Sets are distinct from lists or tuples as they do not allow duplicate values. Sets support various mathematical operations like union, intersection, and difference, making them useful for tasks involving distinct elements and set operations in Python.