A large Python program is not one file but several modules that import each other, started from one main file. This section explains that structure, and the basic layout rules inside each file.
Top-level file and modules
Structure of a Python program
a.py (top-level file: the program starts here) / \ import import / \ b.py c.py (modules: provide functions, classes, data) | | import import | | standard library modules (math, os, ...) and installed packages
The top-level file (here a.py) is the one you run, for example with python a.py; it controls the flow of the program.
b.py and c.py are modules: they define tools and are normally not run directly but imported by a.py.
When a module is imported for the first time, its code runs once and its names become available as attributes, such as b.function_name.
Layout rules inside a file
Element
Rule
Statements
One per line normally; no semicolon needed
Blocks
A line ending in : (such as if, for, def) is followed by an indented block; the standard indent is 4 spaces
Comments
Start with # and continue to the end of the line
Docstrings
A string as the first statement of a module, function or class documents it
Long lines
Can continue inside brackets ( ), [ ], { }
Case
Python is case-sensitive: Total and total are different names
In C, indentation is only for readability and braces mark blocks. In Python, indentation is the syntax: changing it changes or breaks the program.
Running versus importing: __name__
Every module has a built-in variable __name__. When a file is run directly, its __name__ is "__main__"; when it is imported, __name__ is the module’s name. Code placed under if __name__ == "__main__": therefore runs only when the file is executed as a program, not when it is imported.
Key takeaways
The top-level file starts the program; modules provide tools and are imported.
An imported module’s code runs once, on the first import.
Indentation (4 spaces) defines blocks; # starts a comment.
__name__ is "__main__" only in the file being run directly.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Describe the roles of a.py, b.py and c.py in a program where a.py imports b and c, and explain what happens when a.py is run.
Show worked answer
a.py is the top-level file: it is the one run by the user and it controls the program’s flow. b.py and c.py are modules that define functions, classes or data for a.py to use. When python a.py runs, Python executes a.py from the top; at import b it finds b.py, runs its top-level code once and makes its names available as b.name, and the same for c. Then the rest of a.py runs, calling the imported tools as needed.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/introduction-to-python/program-structure-in-python/ to read this page.