Program Structure
Understanding the basic structure of a program
Python Program Structure
In this section, we will study the basic structure of a Python program and get familiar with the rules that every Python program follows.
Basic Program Structure
A Python program is simply a sequence of statements that the interpreter executes from top to bottom. Here is an example of a small but complete Python program:
There are no required boilerplate lines — just your code.
Importing Modules
When you need functionality beyond the built-in functions, you import a module:
You will use math frequently for calculations involving constants like math.pi or functions like math.sqrt(). sys and os are useful for more advanced tasks, and you will encounter them naturally as the course progresses.
Indentation
This is the most important rule in Python. Instead of curly braces {}, Python uses indentation (whitespace at the beginning of a line) to define blocks of code:
The standard in Python is 4 spaces per indentation level. Mixing tabs and spaces will cause errors, so pick one and stay consistent.
Statements
Each line in Python is typically one statement. There are no semicolons required at the end:
You can technically write multiple statements on one line using ;, but this is discouraged:
Comments
Comments explain your code and are ignored by the interpreter:
Code Writing Rules
- Indentation defines code blocks — be consistent, always use 4 spaces or a tab
- Python is case-sensitive —
Nameandnameare different variables - No semicolons needed — each line is its own statement
- Variable names can contain letters, digits, and underscores — but cannot start with a digit
- Blank lines are allowed — use them to make your code more readable
These rules form the foundation of every Python program you will write in this course.