Python syntax – using parentheses with indentation
Question:
I’m taking a course on Polars and found the syntax I don’t fully understand.
Here’s sample from the course:
csvFile = "..\data\example.csv"
(
pl.read_csv(csvFile)
.filter(pl.col("Col").is_null())
)
this is working.
When I try to modify the line to
csvFile = "..\data\example.csv"
ex=(
pl.read_csv(csvFile)
.filter(pl.col("Col").is_null())
)
this is also working. But when I to make further changes to:
csvFile = "..\data\example.csv"
(
ex=pl.read_csv(csvFile)
.filter(pl.col("Col").is_null())
)
I get syntax error ex=pl.read_csv(csvFile)
^ SyntaxError: invalid syntax. Maybe you meant ‘==’ or ‘:=’ instead of ‘=’?
What is the meaning of these parentheses? I really like this kind of syntax but can’t find reference on how to use it properly. I’m using python 3.11 in VSCode with Jupyter extension.
Artur
Answers:
A container enclosed by parenthesis is named a tuple
. Tuples contain non-mutable order of items inside.
Tuple is similar to a List, where a Tuple is enclosed by ()
and a List is enclosed by []
.
For example:
tuple_1 = (item1, item2, item3)
list_1 = [item1, item2, item3]
That is just a way to put on several lines what should go on a single line. It’s just formatting stuff.
Both
(
expression on
several lines
)
and
variable = (
expression on
several lines
)
works. In the first case your expression got evaluated but its output is not saved to any variable, in the second it does.
As a general rule, everything that is inside brackets (both round and squared ones) can be safely written on multiple lines for readability purposes, without needing to escape newline with a
at the end of the lines. PEP8 contains all rules related to a good formatting of python code.
I’m taking a course on Polars and found the syntax I don’t fully understand.
Here’s sample from the course:
csvFile = "..\data\example.csv"
(
pl.read_csv(csvFile)
.filter(pl.col("Col").is_null())
)
this is working.
When I try to modify the line to
csvFile = "..\data\example.csv"
ex=(
pl.read_csv(csvFile)
.filter(pl.col("Col").is_null())
)
this is also working. But when I to make further changes to:
csvFile = "..\data\example.csv"
(
ex=pl.read_csv(csvFile)
.filter(pl.col("Col").is_null())
)
I get syntax error ex=pl.read_csv(csvFile)
^ SyntaxError: invalid syntax. Maybe you meant ‘==’ or ‘:=’ instead of ‘=’?
What is the meaning of these parentheses? I really like this kind of syntax but can’t find reference on how to use it properly. I’m using python 3.11 in VSCode with Jupyter extension.
Artur
A container enclosed by parenthesis is named a tuple
. Tuples contain non-mutable order of items inside.
Tuple is similar to a List, where a Tuple is enclosed by ()
and a List is enclosed by []
.
For example:
tuple_1 = (item1, item2, item3)
list_1 = [item1, item2, item3]
That is just a way to put on several lines what should go on a single line. It’s just formatting stuff.
Both
(
expression on
several lines
)
and
variable = (
expression on
several lines
)
works. In the first case your expression got evaluated but its output is not saved to any variable, in the second it does.
As a general rule, everything that is inside brackets (both round and squared ones) can be safely written on multiple lines for readability purposes, without needing to escape newline with a at the end of the lines. PEP8 contains all rules related to a good formatting of python code.