Python Syntax#
Reserved keywords#
The Python language reserves a small set of keywords that cannot be used as variable names, function names, or any other identifiers:
Keyword |
Description |
---|---|
and |
A logical operator |
as |
To create an alias |
assert |
For debugging |
break |
To break out of a loop |
class |
To define a class |
continue |
To continue to the next iteration of a loop |
def |
To define a function |
del |
To delete an object |
elif |
Used in conditional statements, same as else if |
else |
Used in conditional statements |
except |
Used with exceptions, what to do when an exception occurs |
False |
Boolean value, the result of comparison operations |
finally |
Used with exceptions, a block of code that will be executed no matter if there is an exception or not |
for |
To create a for loop |
from |
To import specific parts of a module |
global |
To declare a global variable |
if |
To make a conditional statement |
import |
To import a module |
in |
To check if a value is present in a list, tuple, etc. |
is |
To test if two variables are equal |
lambda |
To create an anonymous function |
None |
Represents a null value |
nonlocal |
To declare a non-local variable |
not |
A logical operator |
or |
A logical operator |
pass |
A null statement, a statement that will do nothing |
raise |
To raise an exception |
return |
To exit a function and return a value |
True |
Boolean value, the result of comparison operations |
try |
To make a try…except statement |
while |
To create a while loop |
with |
Used to simplify exception handling |
yield |
To end a function returns a generator |
Indentation#
Python depends on indentation to follow the correct flow of execution. In the example below, we see that there is a possible error as Hello Jack. is being printed before anything else. Many will assume it will be printed after Hello John. as it is directly after it, but indentation wise it isn’t part of the main function.
1 #!/usr/bin/env python3
2
3 def main():
4 if 1 > 2:
5 print("Hello World.")
6 print("Hello John.")
7
8 print("Hello Jack.")
9
10
11 if __name__ == "__main__":
12 main()
1 Hello Jack.
2 Hello John.
If we try to correct this error by adding a space before the print statement, then Python still gives an error as the indentation doesn’t match all other levels. Python is very similar to YAML and all the indentations needs to be the same. See PEP 8#indentation for a complete set of rules about indentation.
1 #!/usr/bin/env python3
2
3 def main():
4 if 1 > 2:
5 print("Hello World.")
6 print("Hello John.")
7 print("Hello Jack.")
8
9
10 if __name__ == "__main__":
11 main()
1 File "/workspaces/mastering-python/hello.py", line 7
2 print("Hello Jack.")
3 ^
4 IndentationError: unindent does not match any outer indentation level
Note
Python has no hard guideline on indentation and while most developer choose to use 4 spaces, the requirements is to have at least 1 space for each level of indentation.
Comments#
Any line that starts with
#
will be ignored by Python.Multiline comments can be done by starting every line with the
#
-sign.For multiline comments Python also supports comments between triple quotes as in the example below.