Post 2: Text Editors vs. IDEs for Python Development

Welcome to the second post in my Python learning series. Last time, we got Python installed and ran our first code. Now it’s time to set up a proper coding environment. Working directly with .py files in Notepad gets tedious quickly, so let’s explore better options for writing Python code.

In this post, we’ll cover:

  1. The difference between text editors and IDEs
  2. Popular options for Python development
  3. Setting up VS Code for Python (my personal choice)
  4. Key productivity features that will save you time
  5. Running and debugging Python code from your editor

1. Text Editors vs. IDEs: What’s the Difference?

When I started learning Python, I was confused about whether to use a “text editor” or an “IDE”. Here’s the simple breakdown:

Text Editors:

  • Lightweight applications primarily designed for editing text
  • Minimal features out of the box, but often extensible with plugins
  • Faster startup times and lower system resource usage
  • Examples: Notepad++, Sublime Text, Atom

Integrated Development Environments (IDEs):

  • Comprehensive software packages with built-in tools for development
  • Include code completion, debugging, testing, and version control features
  • More resource-intensive but offer productivity benefits
  • Examples: PyCharm, Visual Studio, Eclipse

Modern text editors with plugins can blur the line between these categories. For instance, VS Code is technically a text editor but can function like an IDE with the right extensions.


Here are some commonly used tools for Python programming:

Text Editors

Notepad++ (Windows only)

  • Free and lightweight
  • Basic syntax highlighting for Python
  • Minimal Python-specific features
  • Good for quick edits but limited for larger projects

Sublime Text

  • Cross-platform (Windows, macOS, Linux)
  • Fast and responsive with large files
  • Package manager for extensions
  • Free to evaluate, $99 license for continued use

Atom

  • Open-source and free
  • Highly customisable
  • Good Git integration
  • Can be slower with large projects

IDEs

Visual Studio Code (VS Code)

  • Free, open-source, cross-platform
  • Extensive extension library
  • Lightweight but powerful
  • Strong Python support through extensions
  • My personal choice and what we’ll focus on in this post

PyCharm

  • Python-specific IDE by JetBrains
  • Community (free) and Professional (paid) editions
  • Comprehensive tools designed specifically for Python
  • Steeper learning curve but powerful features

Jupyter Notebooks

  • Interactive coding environment
  • Great for data analysis and visualisation
  • Documents that combine code, output, and markdown text
  • We’ll cover this in detail in a later post

After trying several options, I settled on VS Code for its balance of performance and features. It’s lightweight enough to run smoothly on my machine but offers robust Python support.


3. Setting Up VS Code for Python Development

If you’re following along with me, let’s set up VS Code:

3.1 Download and Install VS Code

  1. Go to https://code.visualstudio.com/
  2. Download the installer for your operating system
  3. Run the installer with default options

3.2 Install Essential Python Extensions

  1. Launch VS Code
  2. Click the Extensions icon in the left sidebar (or press Ctrl+Shift+X)
  3. Search for and install these extensions:
    • Python by Microsoft (essential)
    • Pylance for improved language support
    • Python Indent for automatic indentation

3.3 Configure Python Interpreter

  1. Open a Python file or create a new one (File > New File, then save with .py extension)
  2. Click on “Select Python Interpreter” in the bottom status bar
  3. Choose the Python installation you installed in Post 1

3.4 Set Up Code Formatting and Linting

These tools help maintain code quality and consistent style:

  1. Install the autopep8 formatter:

    • Open a terminal in VS Code (Terminal > New Terminal)
    • Run: pip install autopep8
  2. Configure formatter settings:

    • Press Ctrl+Shift+P to open the command palette
    • Type “Preferences: Open Settings (JSON)”
    • Add these settings:
    "python.formatting.provider": "autopep8",
    "editor.formatOnSave": true,
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true
    
  3. Install Pylint for code quality checking:

    • In the terminal, run: pip install pylint

4. Key Productivity Features

Here are some features that have saved me hours of time while learning Python:

4.1 Code Highlighting and IntelliSense

VS Code automatically highlights Python syntax and provides IntelliSense (code completion):

  • Start typing a Python keyword or function name
  • VS Code shows suggestions as you type
  • Press Tab to complete the suggestion

Try it by typing pri and watching it suggest print().

4.2 Code Snippets

Snippets let you insert commonly used code patterns quickly:

  1. Type if and press Tab
  2. VS Code inserts the if statement structure
  3. Fill in the condition and press Tab to move to the body

Other useful snippets: for, def, class, try

4.3 File Explorer and Multiple Files

Having a file explorer helps manage projects with multiple files:

  1. Use File > Open Folder to open your project directory
  2. The left sidebar shows all files in that folder
  3. Create new files or folders directly in VS Code

4.4 Integrated Terminal

No need to switch between VS Code and Command Prompt:

  1. Press Ctrl+` (backtick) or use Terminal > New Terminal
  2. Run Python code, install packages, or use Git commands

5. Running and Debugging Python Code

5.1 Running Python Files

Method 1: Using the Play Button

  1. Open a Python file
  2. Click the Play icon in the top-right corner

Method 2: Using the Terminal

  1. Open the integrated terminal (Ctrl+`)
  2. Run: python your_file.py

5.2 Introduction to Debugging

Debugging helps you find and fix errors in your code:

  1. Set a breakpoint by clicking in the margin to the left of a line number (a red dot appears)
  2. Press F5 or click the Debug icon in the sidebar, then “Start Debugging”
  3. Your code runs until it hits the breakpoint, then pauses
  4. Examine variables in the “Variables” panel
  5. Use the debug toolbar to step through code line by line

5.3 Print-Driven Debugging vs. Using a Debugger

When I first started coding, I relied heavily on print() statements to understand what was happening in my code:

def calculate_total(items):
    print(f"Items received: {items}")  # Debug print
    total = sum(items)
    print(f"Calculated total: {total}")  # Debug print
    return total

While this works, using the debugger is more powerful:

  • You can inspect all variables without modifying code
  • You can pause execution and examine the program state
  • You don’t need to remove debug code when you’re done

Practice Exercise

Let’s put your new environment to use:

  1. Create a new file called calculator.py in VS Code
  2. Write a simple function that takes two numbers and an operation (add, subtract, multiply, divide) and returns the result
  3. Add proper error handling (e.g., division by zero)
  4. Use print statements to test your function with various inputs

Example solution (try it yourself first!):

def calculate(a, b, operation):
    if operation == "add":
        return a + b
    elif operation == "subtract":
        return a - b
    elif operation == "multiply":
        return a * b
    elif operation == "divide":
        if b == 0:
            return "Error: Division by zero"
        return a / b
    else:
        return "Error: Unknown operation"

# Test the function
print(calculate(10, 5, "add"))       # Should print 15
print(calculate(10, 5, "subtract"))  # Should print 5
print(calculate(10, 5, "multiply"))  # Should print 50
print(calculate(10, 5, "divide"))    # Should print 2.0
print(calculate(10, 0, "divide"))    # Should print error message

Try using the debugger to step through this code and watch how the variables change.


What’s Next?

Now that we have our Python development environment set up, we’re ready to dive deeper into Python syntax and language features. In the next post, we’ll explore variables, data types, control flow, and more fundamental Python concepts.

Stay tuned for Post 3: Python Syntax Fundamentals & Language Features!


This post is part of my journey learning Python. I’m a chartered accountant exploring programming to enhance my analytical toolkit. If you have questions or spot any errors, please leave a comment below.