Running Python code from the command line (cmd) can be an efficient way to quickly execute scripts or interact with the interpreter. Here's a comprehensive guide on how to do this effectively.

Before we delve into the details, ensure that Python is installed on your system and added to your system's PATH. If not, you can download it from the official Python website, install it, and ensure it's added to your PATH for easy access.

Running Python Scripts
To run a Python script from the command line, navigate to the directory containing the script using the 'cd' command, then use the 'python' or 'python3' command followed by the script name.

Here's an example: If your script is named 'example.py' and located in a folder on your Desktop, you would run it like this:
cd Desktop
python example.py
Specifying the Python Interpreter

If you have multiple Python versions installed on your system, you can specify which one to use by providing the full path to the interpreter. For instance, if you're using Python 3.8, you might run:
C:\Path\to\python38\python.exe example.py
On Unix-based systems (like Linux or macOS), use:
/usr/local/bin/python3.8 example.py
Running Scripts with Arguments

You can pass arguments to your script from the command line by including them after the script name. For example:
python example.py arg1 arg2 arg3
In your script, you can access these arguments using the 'sys.argv' list. The first element is the script name, so the first argument would be 'sys.argv[1]'.
Interactive Python Sessions

You can also use the command line to interact with the Python interpreter in an interactive session. This can be useful for testing code snippets, trying out functions, or learning Python.
To start an interactive Python session, simply type 'python' or 'python3' into your command line, followed by the Enter key. You'll then be presented with a prompt ('>>>' for Python 3) where you can type and run Python code.


















Exiting an Interactive Session
To exit the interactive session, you can simply type 'exit()' or press Ctrl+Z followed by Enter (on Windows) or Ctrl+D (on Unix-based systems).
Using the Interactive Session Effectively
During an interactive session, you can use the built-in help command to get help with Python modules, functions, and other elements. For example, to get help with the 'math' module, you would type 'help(math)'. You can also save all or part of an interactive session to a file by using the 'python -i' flag when starting the session, or the 'shell.writeexecutable(id)' function within the session.
Now that you've learned how to run Python code from the command line, you can integrate this skill into your workflow for greater productivity and code testing convenience.