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:
Unlike C++, imports are not mandatory for a basic program. Python has many built-in functions like print(), input(), len(), and range() that are always available without importing anything.
No main() Function Required
In Python, there is no required entry point function. The interpreter starts at the top of the file and executes each line in order:
That said, you will sometimes see this pattern in Python code:
This is a convention used in larger programs to control when code runs, but it is not required — especially for competitive programming, where you write simple scripts.
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. Unlike C++, 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
- 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.