What Is the Basic Structure of a Python Program?

To give you a feel for the Python language, let’s go over its syntax.


The word syntax comes from the Greek and means "sentence structure." The syntax of a programming language is the complete description of permitted and forbidden constructions. The syntax is defined by a grammar that must be adhered to by the programmer. If he or she doesn’t do that, the well-known syntax error will be triggered.


Python gives a programmer very precise instructions on how to structure the source code. Although experienced programmers may see this as a limitation, this feature benefits novice programmers in particular, because unstructured and cluttered code is one of the biggest sources of errors in programming.


Basically, a Python program consists of individual statements, which in the simplest case take up exactly one line in the source code. For example, the following statement prints text on the screen:


print("Hello World")


Some statements can be divided into a statement header and a statement body, where the body can contain further statements:


Statement header:




In a real Python program, this may look something like this:


if x > 10:

   print("x is greater than 10")

   print("Second line!")


The affiliation of the body to the header is specified in Python by a colon at the end of the statement header and by a deeper indentation of the statement body. Indentation can be done using both tabs and spaces, though you are well advised not to mix the two. We recommend an indentation depth of four spaces each.


Python differs here from many common programming languages, where the mapping of the statement header and statement body is achieved by curly brackets or keywords like begin and end.


Note: A program in which both spaces and tabs have been used can be compiled by the Python compiler without difficulty as each tab is internally replaced by eight spaces. However, this can cause hard-to-find errors because many editors use a tab width of four spaces by default. This makes certain sections of source code appear to be equally indented when in fact they are not. Please set your editor to automatically replace each tab with spaces, or use only spaces to indent your code.


You may wonder now how statements that run over several lines are compatible with the interactive mode, in which only one line can be edited at a time. Well, in general, we’ll try to avoid the interactive mode when a code sample is several lines long. Nevertheless, the question is justified. The answer is that the statements are entered quite intuitively line by line. When the interpreter detects that an instruction isn’t yet complete, it changes the prompt from >>> to .... Let's enter the previous example into the interactive mode:


>>> x = 123

>>> if x > 10:

... print("The interpreter is doing a good job")

... print("Second line!")


The interpreter is doing a good job

Second line!



Note that you have to consider the current indentation depth, even if a line starts with .... Furthermore, the interpreter can’t automatically detect the end of the statement body as it can contain any number of statements. For this reason, a statement body in interactive mode must be terminated by pressing the (Enter) key.


Wrapping Long Lines

Basically, source code lines can have any length. However, many programmers limit the length of their source code lines so that, for example, several source code files fit side by side on the screen or the code can be read comfortably on devices with a fixed line width. Common maximum line lengths are 80 or 120 characters. Within parentheses, you may wrap source code any way you like:


>>> var = (

... 10

... +

... 10

... )

>>> var



However, in many other places where parentheses are not permitted you are bound by Python's strict syntactic rules. By using the backslash notation, it's possible to break source code into a new line at almost any position:


>>> var \

... = \

... 10

>>> var



In general, a backslash can be placed anywhere a space could have been. Therefore, a backslash within a string is also possible:


>>> "Hello \

... World"

'Hello World'


Note, however, that indenting the wrapped part of the string will write spaces into the string. For this reason, you should prefer the following variant of writing a string across multiple lines:


>>> "Hello " \

... "World"

'Hello World'


Joining Multiple Lines

Just as you wrap a single-line statement to multiple lines using the backslash, you can combine multiple single-line statements into one line. For this purpose, the statements are separated from each other by a semicolon:


>>> print("Hello"); print("World")




Statements consisting of a statement header and a statement body can also be put on one line without using a semicolon, provided that the statement body itself doesn’t consist of more than one line:


>>> x = True

>>> if x: print("Hello World")


Hello World


If the statement body is several lines long, they can be combined by a semicolon:


>>> x = True

>>> if x: print("Hello"); print("World")





All statements joined by a semicolon are treated as if they were equally indented. A colon alone increases the indentation depth. For this reason, in the preceding example, there’s no way to write a statement on the same line that’s no longer in the body of the if statement.


Note: Using the backslash and especially the semicolon quickly results in unreadable code. Therefore, you should use both notations only if you think it’s conducive to readability and clarity.


Editor’s note: This post has been adapted from a section of the book Python 3: The Comprehensive Guide by Johannes Ernesti and Peter Kaiser.


Python 3: The Comprehensive Guide
Python 3: The Comprehensive Guide

Ready to master Python? Learn to write effective code, whether you’re a beginner or a professional programmer. Review core Python concepts, including functions, modularization, and object orientation, and walk through the available data types. Then dive into more advanced topics, such as using Django and working with GUIs. With plenty of code examples throughout, this hands-on reference guide has everything you need to become proficient in Python!

Learn More
Rheinwerk Computing
by Rheinwerk Computing

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes books by leading experts in the fields of programming, administration, security, analytics, and more.