Before discussing Python syntax, we must cover how Python works. Python is an interpreted language, unlike languages like C, Java, or Assembly, which are compiled languages. Instead of compiling the whole program to machine code ahead of time, the Python interpreter reads your code and runs it statement by statement. This is certainly different and comes with a set of advantages and disadvantages:
This interpreted language feature yields two ways of writing Python code, as seen below.
A REPL is just a command-line environment. It takes one command at a time, processes its function, and yields an output. Terminals are prime examples of REPLs, but Python can also act as a REPL.
By running the python3 (or python on some older environments) command in the terminal, we open the Python REPL:
python3Python 3.10.5 (v3.10.5:f377153967, Jun 6 2022, 12:36:10) [Clang 13.0.0 (clang-1300.0.29.30)] on darwinType "help", "copyright", "credits" or "license" for more information.>>>From here, we can start writing Python! Let's write our Hello World program by writing the following line:
print("Hello World!")This responds by printing Hello World! to the console.
exit() or quit() command to leave the REPL.Writing one line at a time becomes inconvenient after a while. Instead, we opt for a more standard development environment of writing all our code in a file and then running the file. Like the REPL, the Python interpreter will read each line, run the command, and then move on.
Python files are suffixed with the .py extension. Let's write an equivalent hello.py file that contains Python's version of Hello World:
print("Hello World!")Then, we can run the program from the terminal:
python3 hello.pyHello World!Unlike languages like C or Java, Python does not use curly braces ({}) to identify the start and end of code blocks. Instead, Python uses indentation to show where blocks start and end. This makes code formatting essential to the interpretation of a Python program.
You can choose how far to indent, but you must be consistent. The standard is 4 spaces (PEP 8), and most editors insert them when you press Tab. Here is an example of good indentation vs bad:
1# Good indentation
2if True:
3 print("True")
4else:
5There are two exceptions to this rule, which both deal with multi-line statements. If you want to write a multi-line statement, you can use a backslash (\) to split the statement:
1total = item_one + \
2 item_two + \
3 item_threeThe other case is enumerating lists, tuples, and sets, which allows the continuation onto multiple lines without the use of the backslash:
1weekdays = ["Monday", "Tuesday",
2 "Wednesday",
3 "Thursday", "Friday"]The interpreter ignores lines containing only whitespace. Blank lines are required to end multi-line statements only when using the REPL.
Use a semicolon to add more than one command to a single line. Here is an entire program on one line:
from datetime import datetime; now = datetime.now(); print("The time is", now)Python allows the use of single ('), double ("), or triple (''' or """) quotes to denote string literals, so long as the same type of quote starts and ends the string.
1single = 'single quote'
2double = "double quote" # identical to single quotes
3triple = """triple quotes can
4go over multiple lines
5without a problem"""You can mix and match your quotation usage to easily put quotes inside your string literal without using escape characters.
1print('John said, "Where did he go?"')
2# Output: John said, "Where did he go?"Comments in Python are done using the hash sign (#) outside of a string literal. All characters from the hash sign until the end of the line are part of the comment.
Unlike C, Python has no multi-line comment syntax like /* ... */, and no way to end a comment before the end of its line.