Bigdata – Knowledge Base

Python – Environment setup and writing First code

Setting up the Python Programming Environment and Writing Your First Python Code #

This detailed guide will walk you through the process of setting up a Python programming environment on your computer and writing your first Python code. Whether you’re using Windows, macOS, or Linux, this guide will cover all the necessary steps to get you started.

1. Installing Python #

Windows #

  1. Download Python:
    • Go to the official Python website: python.org
    • Navigate to the Downloads section and select the latest stable version of Python for Windows.
  2. Run the Installer:
    • Double-click the downloaded executable file to run the installer.
    • Make sure to check the box that says “Add Python to PATH” before clicking “Install Now.”
    • Follow the prompts to complete the installation.
  3. Verify Installation:
    • Open Command Prompt (search for cmd in the Start menu).
    • Type python --version and press Enter. You should see the installed version of Python.

macOS #

  1. Download Python:
    • Go to the official Python website: python.org
    • Navigate to the Downloads section and select the latest stable version of Python for macOS.
  2. Run the Installer:
    • Open the downloaded .pkg file and follow the installation instructions.
  3. Verify Installation:
    • Open Terminal (search for Terminal in Spotlight).
    • Type python3 --version and press Enter. You should see the installed version of Python.

Linux #

  1. Using Package Manager:
    • Most Linux distributions come with Python pre-installed. You can check the installed version by opening the terminal and typing python3 --version.
    • If Python is not installed, you can install it using the package manager. For example, on Ubuntu, you can run: sudo apt update sudo apt install python3
  2. Verify Installation:
    • Open Terminal.
    • Type python3 --version and press Enter. You should see the installed version of Python.

2. Setting Up an Integrated Development Environment (IDE) #

An IDE provides a user-friendly interface to write, debug, and execute your Python code. Some popular IDEs for Python include PyCharm, Visual Studio Code, and Jupyter Notebook.

PyCharm #

  1. Download and Install PyCharm:
    • Go to the JetBrains website: jetbrains.com/pycharm
    • Download the Community edition (free) or Professional edition.
    • Run the installer and follow the installation instructions.
  2. Setting Up PyCharm:
    • Open PyCharm and create a new project.
    • Select the Python interpreter (usually detected automatically).

Visual Studio Code #

  1. Download and Install VS Code:
    • Go to the official Visual Studio Code website: code.visualstudio.com
    • Download the installer for your operating system and run it.
  2. Installing Python Extension:
    • Open VS Code.
    • Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window.
    • Search for “Python” and install the extension by Microsoft.
  3. Setting Up Python Interpreter:
    • Open the Command Palette (View > Command Palette).
    • Type Python: Select Interpreter and select the appropriate Python interpreter.

Jupyter Notebook #

  1. Install Jupyter Notebook:
    • Open your terminal or command prompt.
    • Run the command: pip install notebook
  2. Launch Jupyter Notebook:
    • In the terminal or command prompt, run: jupyter notebook
    • A new browser window should open with the Jupyter interface.

3. Writing Your First Python Code #

Using a Simple Text Editor #

  1. Open a Text Editor:
    • You can use any text editor like Notepad, TextEdit, or a code editor like VS Code.
  2. Write the Code:
    • Type the following code to print “Hello, World!”:
    • print("Hello, World!")
  3. Save the File:
    • Save the file with a .py extension, for example, hello.py.
  4. Run the Code:
    • Open the terminal or command prompt.
    • Navigate to the directory where you saved the file.
    • Run the file by typing python hello.py (or python3 hello.py on macOS/Linux).
    • You should see the output: Hello, World!

Using PyCharm #

  1. Create a New Project:
    • Open PyCharm and select “Create New Project.”
    • Choose a location for your project and click “Create.”
  2. Create a New Python File:
    • Right-click on the project name in the Project Explorer.
    • Select “New” > “Python File.”
    • Name the file hello.py.
  3. Write the Code:
    • Type the following code:
    • print("Hello, World!")
  4. Run the Code:
    • Right-click on the file in the Project Explorer and select “Run ‘hello’.”
    • You should see the output in the Run window: Hello, World!

Using Visual Studio Code #

  1. Open VS Code and Create a New File:
    • Open VS Code and select “File” > “New File.”
    • Save the file with a .py extension, for example, hello.py.
  2. Write the Code:
    • Type the following code:
    • print("Hello, World!")
  3. Run the Code:
    • Open the terminal in VS Code by selecting “View” > “Terminal.”
    • In the terminal, navigate to the directory where you saved the file.
    • Run the file by typing python hello.py (or python3 hello.py on macOS/Linux).
    • You should see the output: Hello, World!

Using Jupyter Notebook #

  1. Open Jupyter Notebook:
    • Launch Jupyter Notebook by running jupyter notebook in your terminal or command prompt.
    • A new browser window should open with the Jupyter interface.
  2. Create a New Notebook:
    • Click on “New” and select “Python 3” to create a new notebook.
  3. Write the Code:
    • In the first cell, type the following code: print("Hello, World!")
  4. Run the Code:
    • Click on the “Run” button or press Shift + Enter.
    • You should see the output below the cell: Hello, World!

Using Google Colab #

  1. Open Google Colab:
  2. Create a New Notebook:
    • Click on “File” in the top-left corner.
    • Select “New notebook.”
  3. Write the Code:
    • In the first cell, type the following code: print("Hello, World!")
  4. Run the Code:
    • Click on the “Run” button (a play icon) on the left side of the cell or press Shift + Enter.
    • You should see the output below the cell: Hello, World!

Conclusion #

Congratulations! You’ve successfully set up your Python programming environment and written your first Python code using various tools and platforms, including Google Colab. This setup will serve as the foundation for your Python programming journey, allowing you to develop, test, and execute Python programs efficiently. Happy coding!

What are your feelings
Updated on August 25, 2024