Using the Python Interpreter¶
Python code can be executed in two primary ways: 1. Interactive Mode: Running statements one line at a time in the interactive shell (REPL). 2. Script Mode: Saving code in a .py file and executing the entire file.
1. Interactive Mode (REPL)¶
The interactive interpreter is useful for testing short code snippets, checking documentation, and performing quick math calculations.
Starting the Interpreter¶
Open your terminal or command prompt and type:
The interpreter will start and display the primary prompt:
Python 3.14.7 (main, Sep 2026) [MSC v.1944 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> prompt indicates that the interpreter is ready to receive commands.
Special Variable: _ (Last Printed Value)¶
In interactive mode, the special variable _ holds the result of the last evaluated expression:
Exiting Interactive Mode¶
- Type
exit()orquit()and press Enter. - On Windows: Press
Ctrl + Z, then Enter. - On macOS / Linux: Press
Ctrl + D.
2. Script Mode (Running .py Files)¶
For writing permanent programs, write your code in a text file with a .py extension.
Running the Script¶
Open your terminal in the directory where greet.py is saved:
Passing Arguments via Command Line¶
Understanding sys.argv¶
Command-line arguments passed to a Python script are stored in the list sys.argv provided by the sys module:
sys.argv[0]: The script name (e.g.'greet.py').sys.argv[1]: The first argument passed.sys.argv[2]: The second argument passed, and so on.
The if __name__ == "__main__": Construct¶
In Python, every module has a built-in attribute called __name__: - When a file is run directly from the terminal, __name__ is set to "__main__". - When a file is imported into another file, __name__ is set to the file's module name.
Using if __name__ == "__main__": ensures that the code inside this block only runs when the script is executed directly, not when it is imported as a module by another script.