1-6. Setting Up Python Windows and macOS

Embarking on your Python programming journey begins with setting up the environment on your computer. This guide will walk you through installing Python on Windows and macOS, ensuring you are ready to dive into the world of programming with minimal fuss.

1.6.1. Installing Python on Windows

  • Download the Installer:

Visit the official Python website (python.org) and navigate to the Downloads section. The website should automatically offer you the latest version of Python for Windows. Click on the download link for the Windows installer.

  • Run the Installer:

Once the installer is downloaded, locate it in your downloads folder and double-click to run it. You will be greeted with the installation window.

Important: Ensure you check the box that says "Add Python X.X to PATH" before clicking "Install Now." This step is crucial as it makes Python accessible from the Command Prompt.

  • Customize Installation (Optional):

For most users, the default installation options will suffice. However, if you need to customize locations or configurations, you can click on “Customize installation” and adjust the settings accordingly.

  • Complete the Installation:

Follow the on-screen instructions to complete the installation. Once finished, you will see a screen with a message saying, “Setup was successful.” Click "Close" to finish the setup process.

 

1.6.2. Installing Python on macOS

  • Download the Installer:

Head to the official Python website (python.org) and click on the Downloads tab. The site should automatically suggest the latest version of Python for macOS. Download the macOS installer.

  • Run the Installer:

Open the downloaded package and follow the installation wizard. The wizard will guide you through the necessary steps, including agreeing to the license agreement and selecting an install location.

  • Install Python:

Continue through the installer's steps and click "Install" to begin the installation process. You may need to enter your administrator password to authorize the installation.

  • Complete the Installation:

Once the installation is complete, you will see a confirmation screen. Click "Close" to exit the installer.

 

1.6.3. Verifying Python Installation

After installation, it is crucial to verify that Python is correctly installed and accessible from your system's command line or terminal.

   On Windows:

  • Open Command Prompt by typing cmd in the search bar and pressing Enter.
  • In the Command Prompt, type python --version and press Enter. If Python is correctly installed, you should see the version number displayed.

   On macOS:

  • Open Terminal by going to Applications > Utilities > Terminal.
  • Type python3 --version in the Terminal and press Enter. You should see the Python version number if the installation was successful.

 

1.6.4. How to Use Python Shell

   On Windows:

  • Start the Python Shell: In the Command Prompt, type python and press Enter. You should see something like this:

   

  • Using the Shell: The >>> prompt indicates that you are now in the Python shell. You can type Python commands directly here. For example: 

     

  • To exit the Python shell, you can type exit() or press Ctrl + Z and then Enter.

   On macOS:

  • Start the Python Shell: In Terminal, type python3 and press Enter. You should see something like this:

   

  • Using the Shell: The >>> prompt indicates that you are now in the Python shell. You can type Python commands directly here. For example:

   

  • To exit the Python shell, you can type exit() or press Ctrl + D.

 

1.6.5. Installation of Visual Studio Code

After installing Python, an editor is needed to write and run Python codes.

   Installation on Windows:

  • Download Visual Studio Code
  • Install Visual Studio Code
    • Once the download is complete, open the installer file.
    • Follow the setup instructions: accept the agreement, choose the installation location, and select additional tasks (e.g., creating a desktop icon).
    • Click "Install" and wait for the installation to complete.
    • Once installed, launch Visual Studio Code.

   On macOS:

  • Download Visual Studio Code
  • Install Visual Studio Code
    • Once the download is complete, open the .dmg file.
    • Drag the Visual Studio Code app to the Applications folder.
    • Launch Visual Studio Code from the Applications folder.

 

1.6.6. Setting Up Python in Visual Studio Code

After installing Visual Studio Code, Python Extension for Visual Studio Code is needed.

  • Open Visual Studio Code.
  • Go to the Extensions view by clicking the Extensions icon in the Activity Bar on the side of the window or by pressing Ctrl+Shift+X (Windows) or Cmd+Shift+X (macOS).
  • Search for "Python" and click "Install" on the extension provided by Microsoft.

 

1.6.7. Running Python Code Using Command Prompt (Windows)

  • Open Command Prompt
    • Press Win+R, type cmd, and press Enter.
  • Navigate to the Python Script Directory
    • Use the cd command to navigate to the directory where your Python script is located. For example:

                    cd path\to\your\script

  • Run the Python Script
    • Execute your Python script by typing:

                    python your_script.py

 

1.6.8. Running Python Code Using Terminal (macOS)

  • Open Terminal
    • Press Cmd+Space to open Spotlight Search, type Terminal, and press Enter.
  • Navigate to the Python Script Directory
    • Use the cd command to navigate to the directory where your Python script is located. For example:

                  cd path\to\your\script

  • Run the Python Script
    • Execute your Python script by typing:

                  python3 your_script.py

 

1.6.9. Running Python Code in Visual Studio Code

  • Open Visual Studio Code
    • Launch Visual Studio Code.
  • Open Your Python Script
    • Open the file containing your Python script by navigating to File > Open File and selecting your script.
  • Run the Python Script
    • Open the integrated terminal by selecting View > Terminal.
    • Ensure the terminal is in the correct directory or navigate using the cd
    • Type the following command to run your script:

                   python your_script.py 

    • Alternatively, you can run the script by clicking the "Run" button provided by the Python extension.