MCS-201 · Unit 9

Introduction to Python

Section 6 of 7

Block 3 · Unit 9Introduction to Python9 min · Section 6 of 7

9.6Program Structure in Python

Covers IGNOU MCS-201 §9.6

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

ElementRule
StatementsOne per line normally; no semicolon needed
BlocksA line ending in : (such as if, for, def) is followed by an indented block; the standard indent is 4 spaces
CommentsStart with # and continue to the end of the line
DocstringsA string as the first statement of a module, function or class documents it
Long linesCan continue inside brackets ( ), [ ], { }
CasePython 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 1 · multiple choice

What marks a block of statements in Python?

Show answer

Answer: C. Indentation after a line ending with :

Python uses indentation as syntax; braces create dictionaries or sets.

Question 2 · multiple choice

What is the value of __name__ inside a file that is run directly with python file.py?

Show answer

Answer: B. "__main__"

The file being executed is named "__main__"; an imported module gets its own name.

Question 3 · multiple choice

tools.py contains print("loading tools") at the top level, and main.py contains import tools twice. What does running python main.py print?

Python · main_quiz.py
import tools
import tools
print("done")
Show answer

Answer: B. loading tools, done (two lines)

A module’s code runs only on its first import; the second import reuses the already loaded module.

Question 4 · multiple choice

Which line is a comment in Python?

Show answer

Answer: D. # total marks

# starts a comment that runs to the end of the line. The others are comment styles from C, C++ and SQL.

Question 5 · multiple choice

Why is test code in a module placed under if __name__ == "__main__":?

Show answer

Answer: C. So that it runs only when the module is executed directly, not when it is imported

The condition is false during import, so importing the module does not trigger its test code.

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.

↑ ↓ move↵ openTab switch type