= 2.0
a print(a)
2.0
Marie-Hélène Burle
First, let’s cover some basics of the Python syntax.
Short commands are usually written one per line:
but you can write multiple commands on the same line with the semi-colon separator:
Some commands (e.g. function definitions, for loops, if else statements) span over multiple lines. The first line starts normally, but subsequent lines are indented to mark that they are part of the same command.
This indentation—one tab or a series of spaces (often 4 spaces, but the number can be customized in many IDEs)—has a syntactic meaning in Python and is not just for human readability:
Cell In[3], line 3 print(i) ^ IndentationError: expected an indented block after 'for' statement on line 2
IDEs and good text editors indent code automatically.
Notice how the result can be of a different type.
Variables can be used in operations:
a = a + 10
can be replaced by the more elegant:
Comments
Comments (snippets of text for human consumption and ignored by the Python interpreter) are marked by the hashtag:
PEP 8—the style guide for Python code—suggests a maximum of 72 characters per line for comments. Try to keep comments to the point and spread them over multiple lines if they are too long.