---
title: What Is the Basic Structure of a Python Program?
description: Become familiar with the structure of Python programming code so you'll be able to read and understand code as you learn Python.
image: https://blog.rheinwerk-computing.com/hubfs/What%20Is%20the%20Basic%20Structure%20of%20a%20Python%20Program.png
---

[![Rheinwerk Computing Logo](https://blog.rheinwerk-computing.com/hubfs/computing_logo-header.svg)](https://blog.rheinwerk-computing.com/) [Blog](https://blog.rheinwerk-computing.com)

- Books 
    - Get an overview of our wide selection of books on every relevant computing topic. 
          - [General Computing](https://www.sap-press.com/rheinwerk-computing/general-computing/)
          - [DevOps](https://www.sap-press.com/rheinwerk-computing/devops/)
          - [Programming Languages](https://www.sap-press.com/rheinwerk-computing/programming-languages/)
          - [Security](https://www.sap-press.com/rheinwerk-computing/security/)
          - [Software Development](https://www.sap-press.com/rheinwerk-computing/software-development/)
- Book Subscription 
    - Get unlimited access to all Rheinwerk Computing books! 
          - [Programming Subscription](https://sap-press.com/subscriptions/)

[What Is?](https://blog.rheinwerk-computing.com/tag/what-is)

# What Is the Basic Structure of a Python Program?

![Rheinwerk Computing](https://blog.rheinwerk-computing.com/hubfs/RW-logo-300x300.png)  by [Rheinwerk Computing](https://blog.rheinwerk-computing.com/author/rheinwerk-computing)

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.

**4**

[Python](https://learning.rheinwerk-computing.com/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:`

`   Statement`

`…`

`   Statement`

 

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`

`20`

 

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`

`10`

**4**

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")`

`Hello`

`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")`

`...`

`Hello`

`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.

 

Now that you know the basic structure of Python programs, [learn how to create your first Python program in this post](https://blog.rheinwerk-computing.com/how-to-create-your-first-python-program).

 

*Editor’s note*: This post has been adapted from a section of the book [*Python 3: The Comprehensive Guide*](https://www.amazon.com/Python-Comprehensive-Hands-Programming-Rheinwerk/dp/1493228005?maas=maas_adg_3257CC065CA6532EA40D4F757AAF33F9_afap_abs&ref_=aa_maas&tag=maas)by [Johannes Ernesti](https://www.linkedin.com/in/johannes-ernesti-775442176/) and [Peter Kaiser](https://www.linkedin.com/in/peter-kaiser-3a940a152/). Johannes is a research scientist at DeepL. He is a graduate mathematician and received his doctorate in applied mathematics from the Karlsruhe Institute of Technology (KIT). Peter is a research scientist at DeepL. He has a degree in computer science and received a doctorate in humanoid robotics from the Karlsruhe Institute of Technology (KIT).

 

This post was originally published 9/2022.

## Recommendation

[![Your All-in-One Guide to Python!](https://blog.rheinwerk-computing.com/hs-fs/hubfs/2800_2d.png?width=170&name=2800_2d.png)](https://www.amazon.com/Python-Comprehensive-Hands-Programming-Rheinwerk/dp/1493228005?maas=maas_adg_3257CC065CA6532EA40D4F757AAF33F9_afap_abs&ref_=aa_maas&tag=maas)

**Your All-in-One Guide to Python!**

This award-winning guide covers everything you need to write effective Python — from core concepts like functions, data types, and object orientation to advanced topics like GUIs, Django, network programming, and scientific computing. Whether you're picking up Python for the first time or leveling up your skills, it's a hands-on reference you'll keep coming back to.

[Learn More](https://www.amazon.com/Python-Comprehensive-Hands-Programming-Rheinwerk/dp/1493228005?maas=maas_adg_3257CC065CA6532EA40D4F757AAF33F9_afap_abs&ref_=aa_maas&tag=maas)

![Rheinwerk Computing](https://blog.rheinwerk-computing.com/hubfs/RW-logo-300x300.png)

**by [Rheinwerk Computing](https://blog.rheinwerk-computing.com/author/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.

[What Is?](https://blog.rheinwerk-computing.com/tag/what-is) [Video](https://blog.rheinwerk-computing.com/tag/video) [Python](https://blog.rheinwerk-computing.com/tag/python) [Programming Languages](https://blog.rheinwerk-computing.com/tag/programming-languages)

[![Share on facebook](https://7528309.fs1.hubspotusercontent-na1.net/hub/7528309/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/facebook-color.png?width=24&name=facebook-color.png)](https://www.facebook.com/share.php?u=https%3A%2F%2Fblog.rheinwerk-computing.com%2Fwhat-is-the-basic-structure-of-a-python-program%3Futm_medium%3Dsocial%26utm_source%3Dfacebook) [![Share on linkedin](https://7528302.fs1.hubspotusercontent-na1.net/hub/7528302/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/linkedin-color.png?width=24&name=linkedin-color.png)](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fblog.rheinwerk-computing.com%2Fwhat-is-the-basic-structure-of-a-python-program%3Futm_medium%3Dsocial%26utm_source%3Dlinkedin) [![Share on twitter](https://7528304.fs1.hubspotusercontent-na1.net/hub/7528304/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/twitter-color.png?width=24&name=twitter-color.png)](https://twitter.com/intent/tweet?original_referer=https%3A%2F%2Fblog.rheinwerk-computing.com%2Fwhat-is-the-basic-structure-of-a-python-program%3Futm_medium%3Dsocial%26utm_source%3Dtwitter&url=https%3A%2F%2Fblog.rheinwerk-computing.com%2Fwhat-is-the-basic-structure-of-a-python-program%3Futm_medium%3Dsocial%26utm_source%3Dtwitter&source=tweetbutton&text=) [![Share on email](https://7528311.fs1.hubspotusercontent-na1.net/hub/7528311/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/email-color.png?width=24&name=email-color.png)](mailto:?subject=Check+out+https%3A%2F%2Fblog.rheinwerk-computing.com%2Fwhat-is-the-basic-structure-of-a-python-program%3Futm_medium%3Dsocial%26utm_source%3Demail&body=Check+out+https%3A%2F%2Fblog.rheinwerk-computing.com%2Fwhat-is-the-basic-structure-of-a-python-program%3Futm_medium%3Dsocial%26utm_source%3Demail)

### Comments

### Latest Blog Posts

[![What Is a Tuple in Python Programming?](https://blog.rheinwerk-computing.com/hs-fs/hubfs/What%20Is%20a%20Tuple%20in%20Python%20Programming.png?height=600&name=What%20Is%20a%20Tuple%20in%20Python%20Programming.png)](https://blog.rheinwerk-computing.com/what-is-a-tuple-in-python-programming)

[What Is?](https://blog.rheinwerk-computing.com/tag/what-is)

## [What Is a Tuple in Python Programming?](https://blog.rheinwerk-computing.com/what-is-a-tuple-in-python-programming)

[Read More](https://blog.rheinwerk-computing.com/what-is-a-tuple-in-python-programming)

[![How to Create Your First Python Program](https://blog.rheinwerk-computing.com/hs-fs/hubfs/How%20to%20Create%20Your%20First%20Python%20Program.png?height=600&name=How%20to%20Create%20Your%20First%20Python%20Program.png)](https://blog.rheinwerk-computing.com/how-to-create-your-first-python-program)

[Python](https://blog.rheinwerk-computing.com/tag/python)

## [How to Create Your First Python Program](https://blog.rheinwerk-computing.com/how-to-create-your-first-python-program)

[Read More](https://blog.rheinwerk-computing.com/how-to-create-your-first-python-program)

**Subscribe to our blog!**Get notified about future blog updates.

- <https://www.linkedin.com/showcase/rheinwerk-computing/>
- <https://www.youtube.com/channel/UChj-U-uWGM7qRlHXSFU93ew>
- <https://blog.rheinwerk-computing.com/rss.xml>

### The official Rheinwerk Computing Blog

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes resources that will help you accelerate your computing journey. The Rheinwerk Computing Blog is designed to provide helpful, actionable information on a variety of topics, including programming, administration, security, and analytics!

### Blog Topics

- [All Topics](https://blog.rheinwerk-computing.com)
- [Programming Languages](https://blog.rheinwerk-computing.com/tag/programming-languages)
- [How To](https://blog.rheinwerk-computing.com/tag/how-to)
- [JavaScript](https://blog.rheinwerk-computing.com/tag/javascript)
- [DevOps](https://blog.rheinwerk-computing.com/tag/devops)
- [Featured](https://blog.rheinwerk-computing.com/tag/featured)
- [Web Development](https://blog.rheinwerk-computing.com/tag/web-development)
- [What Is?](https://blog.rheinwerk-computing.com/tag/what-is)
- [Cybersecurity](https://blog.rheinwerk-computing.com/tag/cybersecurity)
- [Artificial Intelligence](https://blog.rheinwerk-computing.com/tag/artificial-intelligence)
- [Software Development](https://blog.rheinwerk-computing.com/tag/software-development)

### Blog curated by

[![Rheinwerk Computing Logo](https://blog.rheinwerk-computing.com/hubfs/computing_logo-header.svg)](https://www.sap-press.com/rheinwerk-computing/) [Visit Rheinwerk Computing Store](https://www.sap-press.com/rheinwerk-computing/)

### About

- [Home](https://sap-press.com/)
- [About Us](https://sap-press.com/the-publisher/)
- [Contact](mailto:info@rheinwerk-publishing.com)
- [Legal Notes](https://sap-press.com/legal-notes/)
- [Privacy Policy](https://sap-press.com/privacy/)
- [Terms of Use](https://sap-press.com/terms/)

© 2026 Rheinwerk Publishing, Inc. | Change Privacy Options

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Rheinwerk Computing",
    "url" : "https://blog.rheinwerk-computing.com/author/rheinwerk-computing"
  },
  "dateModified" : "2026-08-28T14:11:09.539Z",
  "datePublished" : "2022-09-26T13:00:00.000Z",
  "headline" : "What Is the Basic Structure of a Python Program?",
  "image" : [ "https://blog.rheinwerk-computing.com/hubfs/What%20Is%20the%20Basic%20Structure%20of%20a%20Python%20Program.png" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.rheinwerk-computing.com/what-is-the-basic-structure-of-a-python-program",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.rheinwerk-computing.com/hubfs/Logo-1.jpg"
    },
    "name" : "Rheinwerk Publishing, Inc."
  }
}
```