Of course! Here is a comprehensive guide to getting started with Python 3 on Windows, broken down into easy-to-follow steps.

Part 1: Installing Python 3 on Windows
This is the most crucial step. The official installer is the best and safest way to get Python.
Step 1: Download the Python Installer
- Go to the official Python website: https://www.python.org/downloads/windows/
- The page will automatically detect your Windows version (32-bit or 64-bit) and suggest the latest stable release. Click the "Download Python X.X.X" button.
Step 2: Run the Installer and Crucial Settings
-
Open the downloaded
.exefile. You will see the Python installer window. -
CHECK THE BOX THAT SAYS "Add Python to PATH". This is the most important step. It allows you to run Python from the Command Prompt without having to type the full file path. If you miss this, you'll have a much harder time later.
-
Click "Install Now". This will install Python with default settings, which is perfect for beginners.
(图片来源网络,侵删) -
Wait for the installation to complete. It should take less than a minute.
-
Once done, check the box "Disable path length limit" and click "Close". This prevents issues with long file paths in modern Windows versions.
Step 3: Verify the Installation
-
Open the Command Prompt. You can do this by pressing the Windows Key, typing
cmd, and pressing Enter. -
In the Command Prompt, type the following commands and press Enter after each:
(图片来源网络,侵删)python --version
You should see the installed Python version, for example:
Python 3.12.0pip --version
pipis Python's package installer and comes with Python. You should see its version number.
If both commands work without errors, your Python installation is successful!
Part 2: Writing and Running Your First Python Script
There are two main ways to write Python code: the Interactive Interpreter and a Script File.
Method 1: Using the Interactive Interpreter (IDLE)
The interactive interpreter lets you run Python code line by line.
-
Open the Start Menu.
-
Search for "IDLE" and open it. You'll see a window titled "Python 3.x Shell".
-
You can type Python commands directly and press
Enterto see the result.>>> print("Hello, Windows!") Hello, Windows! >>> 2 + 2 4 >>> "Python is fun" 'Python is fun' -
To exit the interpreter, type
exit()or pressCtrl+D.
Method 2: Creating and Running a Python Script File (Recommended)
This is the standard way to write programs.
-
Create the File:
- Open any simple text editor like Notepad.
- Type the following code:
# This is my first Python script print("Hello from a script file!") print("I am learning Python on Windows.") - Save the file. Important: Change the "Save as type" to "All Files" and name the file with a
.pyextension, for example,hello.py. If you don't, it might save ashello.py.txt.
-
Run the Script from Command Prompt:
- Open the Command Prompt.
- Navigate to the directory where you saved your file. For example, if you saved it on your Desktop:
cd C:\Users\YourUsername\Desktop
(Replace
YourUsernamewith your actual Windows username). - Run the script by typing
pythonfollowed by the filename:python hello.py
- You should see the output in the Command Prompt:
Hello from a script file! I am learning Python on Windows.
Part 3: Choosing a Code Editor (Highly Recommended)
While Notepad works, a proper code editor will make your life much easier. They provide features like syntax highlighting (coloring code), auto-completion, and error checking.
Here are the top free choices for Windows:
Visual Studio Code (VS Code) - Top Recommendation
VS Code is a free, powerful, and extremely popular editor from Microsoft.
- How to Install:
- Go to https://code.visualstudio.com/
- Click the "Download for Windows" button and run the installer.
- How to Set it up for Python:
- Open VS Code.
- Go to the Extensions tab (the icon with four squares on the left sidebar).
- Search for "Python" by Microsoft and click "Install". This is the official extension that provides all the great features.
- Now you can open your folder (e.g., your Desktop), create a
.pyfile, and start coding with full support.
PyCharm Community Edition
PyCharm is a dedicated Python IDE (Integrated Development Environment). It's more powerful than VS Code for pure Python development.
- How to Install:
- Go to https://www.jetbrains.com/pycharm/download/
- Download the "Community Edition" (it's free).
- Run the installer and follow the steps.
IDLE
IDLE also comes with Python and has its own built-in editor. It's simpler than VS Code or PyCharm, making it a good starting point for absolute beginners.
Part 4: Managing Packages with pip
Almost everything useful in Python comes from packages (libraries). You install them using pip.
-
Install a Package: Let's install the
requestslibrary, which is used for making web requests.- Open the Command Prompt.
- Type the following command:
pip install requests
pipwill download and install the package and its dependencies.
-
Use the Package in a Script:
-
Create a new file named
test_requests.py. -
Add the following code:
import requests # Make a request to a public API response = requests.get('https://api.github.com') # Check if the request was successful (status code 200) if response.status_code == 200: print("Successfully fetched data from GitHub!") print(f"Response headers: {response.headers['Server']}") else: print(f"Error: Failed to fetch data. Status code: {response.status_code}") -
Save the file and run it from the Command Prompt:
python test_requests.py.
-
-
Update a Package:
pip install --upgrade requests
-
Uninstall a Package:
pip uninstall requests
-
List Installed Packages:
pip list
Part 5: Best Practices for Windows Users
- Virtual Environments: For different projects, it's best practice to use virtual environments. This isolates the packages for each project, preventing conflicts.
- To create one:
python -m venv my_project_env - To activate it:
my_project_env\Scripts\activate - To deactivate it:
deactivate
- To create one:
- Use or
\\for Paths: In Python, you can use forward slashes () in file paths, which often works across all operating systems. For Windows-specific paths, you can use double backslashes (\\).- Good:
file_path = "C:/Users/YourUsername/Desktop/data.txt" - Also Good:
file_path = "C:\\Users\\YourUsername\\Desktop\\data.txt"
- Good:
- Watch for
pythonvs.python3: On Linux and macOS, you often need to typepython3to ensure you're using Python 3. On Windows, the installer sets uppythonto point to Python 3 by default, so you just usepython.
Troubleshooting
- Problem:
'python' is not recognized as an internal or external command...- Solution: This means the "Add Python to PATH" step was missed. You have two options:
- Re-run the installer and make sure to check the "Add Python to PATH" box.
- Manually add it: Find your Python installation path (e.g.,
C:\Users\YourName\AppData\Local\Programs\Python\Python312\) and add it to your system's PATH environment variable. (Search for "Edit the system environment variables" in the Start Menu).
- Solution: This means the "Add Python to PATH" step was missed. You have two options:
- Problem: My
.pyfile opens in Notepad or another editor when I double-click it.- Solution: Right-click the file -> "Open with" -> "Choose another app". Select "Python" from the list. If it's not there, click "Look for another app on this PC" and navigate to your Python installation's
python.exefile (e.g.,C:\...\Python312\python.exe). Check the box "Always use this app to open .py files".
- Solution: Right-click the file -> "Open with" -> "Choose another app". Select "Python" from the list. If it's not there, click "Look for another app on this PC" and navigate to your Python installation's
