Running Python Programs
After installing Python, the next step is learning how to run Python programs. Python code can be executed interactively through the Python interpreter or by running Python script files.
Running Python in Interactive Mode
The Python interpreter allows you to execute Python code line by line, making it ideal for learning, testing, and experimenting.
Windows
Open Command Prompt and run:
python
or
py
macOS and Linux
Open Terminal and run:
python3
You should see output similar to:
Python 3.x.x (default, ...)
>>>
Now you can enter Python commands directly:
print("Hello, World!")
Output:
Hello, World!
To exit the interpreter:
exit()
Running a Python Script File
For larger programs, it's common to write code in a file with a .py extension.
Step 1: Create a Python File
Create a file named:
hello.py
Add the following code:
print("Hello, World!")
Save the file.
Step 2: Open a Terminal or Command Prompt
Navigate to the folder containing your Python file.
Example:
cd path/to/your/project
Step 3: Run the Script
Windows
python hello.py
or
py hello.py
macOS and Linux
Output:
Hello, World!
Running Python from an IDE
Many developers use Integrated Development Environments (IDEs) or code editors to write and run Python programs.
Popular options include:
- Visual Studio Code (VS Code)
- PyCharm
- Spyder
- Jupyter Notebook
Most IDEs provide a Run button that executes your Python script without needing to use the command line.
Common Errors
Python Command Not Found
If you see an error indicating that Python is not recognized, verify that Python is installed correctly and added to your system's PATH.
File Not Found
Ensure you are running the command from the directory where your Python file is located, or provide the full file path.