Previous | Tutorial index | Next

Tutorial 10: Getting Python and IDEs Running for Learning Activities

Learning Objective

To be able to get Python and Python IDEs running for the learning activities included in this course. Verify Python installation. Set up the chosen IDE. Create a project folder. Write and run a simple script. Explore the interactive interpreter. Configure the IDE with useful extensions. Test the debugger.

10.1 Introduction: From Installation to Productivity

In Tutorial 9, you installed Python and an IDE. But having a tool on your computer is not the same as being able to use it effectively. This tutorial bridges the gap between installation and productive coding.

Think of it this way: You have bought a chef's knife and a cutting board (Python and an IDE), but now you need to learn how to hold the knife, set up your workstation, and make your first cut. We will verify everything is working, configure your IDE for maximum efficiency, introduce you to the interactive Python shell (REPL) for experimentation, and finally, teach you how to use a debugger—a tool that will save you countless hours of frustration. By the end of this tutorial, you will be fully prepared to tackle the programming exercises in Unit 2 and beyond.

10.2 Verifying Python Installation: The "Hello, World!" Rite of Passage

Before you open an IDE, let's confirm that Python is correctly installed and accessible from your terminal.

10.2.1 Running the Command

Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and execute the following command:

python --version

(If that fails, try python3 --version on macOS/Linux, or py --version on Windows).

Expected output: A line like Python 3.12.3 (or a similar 3.x version). This confirms the interpreter is on your PATH.

10.2.2 Creating Your First Python File

We will now create a proper .py script and run it. This is the standard way to execute programs.

  1. Open a text editor (like Notepad) or your IDE.

  2. Type the following code:

    print("Hello, World!")
  3. Save the file with the name hello.py on your Desktop (or in a dedicated folder). Crucial: Make sure the file extension is .py, not .txt. In Windows, ensure "Save as type" is set to "All Files" to avoid saving hello.py.txt.

10.2.3 Running the Script from the Terminal

Navigate to the folder where you saved hello.py in your terminal:

Now, run the script:

python hello.py

(Or python3 hello.py).

Expected output: Hello, World! printed to the terminal. If you see this, your Python installation is fully functional.

10.2.4 Understanding the Exit Code

When a program finishes successfully, it returns an exit code of 0 to the operating system. If an error occurs, it returns a non-zero code (e.g., 1). You can check this in your terminal (though you don't need to for basic scripts). This shows that even a simple script communicates with the OS via return codes.

10.3 Setting Up Your Integrated Development Environment (IDE)

While you can write Python in Notepad, an IDE boosts your productivity exponentially. We will focus on VS Code (recommended), but will also mention PyCharm and Thonny briefly.

  1. Open VS Code. You will likely see a "Get Started" page. Close it to see the Explorer.
  2. Open Your Project Folder:
  3. Trust the Folder: If a popup appears asking "Do you trust the authors of the files in this folder?", check the box "Trust the authors of all files in the parent folder" and click "Yes, I trust the authors". This allows VS Code to run extensions and tasks in this folder.
  4. Create a new file: Click the "New File" icon next to the folder name, or press Ctrl+N (Cmd+N on Mac). Save it immediately as hello.py (ensure the .py extension is explicit). You will notice syntax highlighting (colors appear for print and the string).
  5. Select the Interpreter (Crucial Step):

10.3.2 PyCharm Community Edition (Alternative)

  1. Launch PyCharm and click "New Project".
  2. Set the location to unit1-projects.
  3. In the "Base interpreter" dropdown, select your Python 3 installation.
  4. Check "Create a main.py welcome script" (optional, but useful).
  5. PyCharm automatically creates a virtual environment (venv) for you—a best practice.

10.3.3 Thonny (Absolute Beginner Option)

  1. Launch Thonny. It automatically detects your Python installation.
  2. Click File > Save As... and save hello.py in your unit1-projects folder.
  3. Thonny shows a shell at the bottom. Click "Run" to execute your script.

10.4 Creating a Structured Project Folder for the Course

Organizing your files is essential. Untitled folders lead to chaos.

10.5 Exploring the Interactive Interpreter (REPL)

Python's interactive interpreter is one of its most powerful features for learning and experimentation. It implements a REPL (Read-Eval-Print-Loop).

10.5.1 How to Start the REPL

Open your terminal and type python (or python3) and press Enter. You will see >>> (the primary prompt) or ... (for multi-line statements).

$ python Python 3.12.3 (tags/v3.12.3:...) Type "help", "copyright", "credits" or "license" for more information. >>>

10.5.2 Using the REPL for Quick Calculations

The REPL is perfect for testing small snippets before putting them into a script.

>>> 5 + 3 8 >>> print("Testing!") Testing! >>> import math >>> math.sqrt(16) 4.0

10.5.3 Exploring Objects with dir() and help()

>>> dir("Hello") # Shows string methods (upper, lower, split, etc.) ['__add__', '__class__', '__contains__', ..., 'upper', 'zfill'] >>> help("Hello".upper) # Shows documentation for the upper method

10.5.4 Exiting the REPL

Why use the REPL?

10.6 Configuring VS Code with Essential Extensions

VS Code is minimal out-of-the-box. Extensions add the magic. While you have the Python extension, there are others you should consider.

10.6.1 Essential Extensions

Extension Name Purpose Why You Need It
Python (Microsoft) IntelliSense, linting, debugging, Jupyter notebooks. The heart of Python support.
Pylint or flake8 Linter (catps syntax errors/style violations). Highlights errors like missing colons as you type.
Python Debugger (Microsoft) Debugging capabilities (often bundled with Python). Allows you to set breakpoints and step through code.
Jupyter (Microsoft) Support for .ipynb notebooks. Useful for data science and exploratory analysis.
indent-rainbow Colors indentation levels differently. Prevents "invisible" indentation errors (mixing spaces/tabs).
Bracket Pair Colorizer Colors matching brackets. Helps track nested parentheses and brackets.
Code Runner (Jun Han) Run code snippets quickly via a button. Quick execution without using the debugger for simple scripts.

10.6.2 Installing an Extension

  1. Click the Extensions icon on the left sidebar (it looks like a square puzzle piece).
  2. Search for, for example, indent-rainbow.
  3. Click Install.
  4. Restart VS Code if prompted (though usually it activates immediately).

10.6.3 Enabling Linting (Pylint)

  1. When you open a Python file, VS Code may prompt you to install Pylint. Click "Install".
  2. If not, open the Command Palette (Ctrl+Shift+P), type Python: Select Linter, and choose pylint.
  3. Linting will now underline errors in red squiggly lines. Hover over them to see the error message (e.g., Missing whitespace after ',').

10.7 Testing the Debugger: Your Safety Net

The debugger allows you to pause execution and inspect variables. This is far more efficient than littering your code with print() statements.

10.7.1 Writing a Debuggable Script

Create a new file debug_test.py in your project folder and write:

def add_numbers(a, b): result = a + b return result x = 10 y = 20 sum = add_numbers(x, y) print(f"The sum is {sum}")

10.7.2 Setting a Breakpoint

10.7.3 Launching the Debugger

  1. Click the Run and Debug icon on the left sidebar (a triangle with a bug).
  2. Click "Run and Debug" at the top (or press F5).
  3. Select "Python File" from the dropdown.
  4. The debugger will start and pause at the first breakpoint (the red dot).

10.7.4 Debugger Controls

Once paused, a small toolbar appears at the top of the screen:

10.7.5 Inspecting Variables

While paused, look at the Variables pane on the left:

Why debugging matters: It teaches you how code executes temporally. You see the data flow step-by-step, which is invaluable for understanding complex logic.

10.8 Hands-On Activity: The Ultimate Workflow Check

Perform the following steps sequentially to prove your setup is 100% operational:

  1. Terminal Run:
  2. IDE Run:
  3. Debug Run:
  4. REPL Exploration:
  5. Check Linting:

If all 5 steps work without errors, your environment is fully configured and ready for the rest of the course!

10.9 Quizzes

Quiz 1: Verification and Script Execution

1. What is the correct command to check your Python version on Windows if python is not recognized?

Answer(B) `py --version`. The Python launcher (`py`) is usually available even if `python` is not in PATH.

2. What does REPL stand for?

Answer(A) Read-Evaluate-Print-Loop

3. Which command should you type in a Python interactive session to view all methods of a string object?

Answer(C) `dir("hello")`

4. When you successfully run a Python script from the terminal, what exit code is typically returned?

Answer(C) `0`

Quiz 2: IDE, Extensions, and Linting

5. In VS Code, how do you select the Python interpreter for your workspace?

Answer(B) Open Command Palette (`Ctrl+Shift+P`) and select `Python: Select Interpreter`

6. What is the purpose of a linter (like Pylint) in an IDE?

Answer(C) To highlight syntax errors and stylistic issues while you type

7. Which extension in VS Code is essential for basic Python support (IntelliSense, linting)?

Answer(A) Python (Microsoft)

Quiz 3: Debugging Concepts

8. In the VS Code debugger, what does the "Step Over" action (F10) do?

Answer(B) Executes the current line and moves to the next, treating function calls as a single operation

9. If you want to inspect the values of variables inside a function during debugging, which action should you use?

Answer(B) Step Into (F11) to enter the function

10. Where do debugger outputs (like print statements) appear when you run a script via F5 in VS Code?

Answer(C) The `DEBUG CONSOLE` tab

10.10 Exercises

Exercise 1: REPL Exploration and Calculations

Instructions: Open the Python interactive interpreter (REPL) and perform the following tasks. Note down the commands you used and their output.

  1. Calculate the value of (25 + 75) * 4 / 2.
  2. Import the math module and calculate the square root of 144.
  3. Create a list my_list = [1, 2, 3]. Use dir() to find a method that adds an item to the end. What is the method name? (Hint: It involves 'append').
  4. Use help() to find out what the math.ceil() function does. Write a one-sentence explanation.
  5. Exit the REPL using the correct shortcut.
Answers 1. `(25 + 75) * 4 / 2` -> `200.0`. 2. `import math; math.sqrt(144)` -> `12.0`. 3. `dir(my_list)` -> The method is `append`. 4. `help(math.ceil)` -> It returns the ceiling of a number (the smallest integer greater than or equal to it). 5. `Ctrl+Z` (Windows) or `Ctrl+D` (Mac/Linux) or `exit()`.

Exercise 2: Debugger Practice

Instructions: Write the following Python script in VS Code and save it as buggy.py:

def divide_numbers(x, y): result = x / y return result a = 10 b = 2 c = 0 print("Calculating division...") d = divide_numbers(a, c) # This will cause a ZeroDivisionError print(f"Result: {d}") print("End of program.")
  1. Set a breakpoint on the line d = divide_numbers(a, c).
  2. Press F5 to start debugging. Step into the function (F11).
  3. Look at the Variables pane. What are the values of x and y? What is the value of result before the division?
  4. Press F10 (Step Over) to execute result = x / y. What error appears in the Variables pane or Debug Console? (Write down the error type).
  5. Why did the program crash, and how would you fix it? (Answer: by checking if y is 0 before dividing).

Exercise 3: Installing an Extension and Linting

Instructions:

  1. In VS Code, open the Extensions view (Ctrl+Shift+X).

  2. Search for and install the "indent-rainbow" extension.

  3. Create a new Python file named test_indent.py.

  4. Write the following code with incorrect indentation (mix a tab and spaces, or inconsistent levels):

    if True: print("First") print("Second") # This line has an extra space or tab issue
  5. You should see the indent-rainbow colorizing the indentation differently, making the inconsistency visible. If Pylint is installed, it will underline the invalid indentation.

  6. Write a 2-sentence reflection on why this visual indicator is helpful.

Exercise 4: Terminal vs. IDE Execution Comparison

Instructions:

  1. Write the following script path_demo.py:

    import os print("Current working directory:", os.getcwd())
  2. Run from Terminal: Navigate to the folder containing this script in your terminal and run python path_demo.py. Note the output.

  3. Run from VS Code: Open the file in VS Code and click the "Run" button (top right). Note the output.

  4. Question: Are the outputs the same? If not, explain why (Hint: Where is the terminal opened? Where is the IDE's terminal rooted?).

AnswerThe outputs are typically the same if VS Code opens the terminal in the project root (which is usually where the script is). If VS Code opens a terminal in a different default folder, the outputs will differ. This teaches the importance of using relative paths carefully.

10.11 Homework Questions

Answer the following questions in complete sentences. Each response should be 3–5 sentences unless otherwise specified.

Short Answer Questions

1. Explain the difference between running a Python script from the terminal (e.g., python my_script.py) and using the "Run" button in an IDE like VS Code. What are the advantages of each approach?

Sample AnswerRunning from the terminal gives you full control over command-line arguments and clearly shows the exact environment and working directory. It is also useful for integrating with other shell tools or running scripts on a server. The IDE "Run" button offers convenience, integrated output panels, and direct access to debugging tools without leaving the editor, making it faster for iterative development and troubleshooting.

2. What is the primary purpose of the Python interactive interpreter (REPL), and how does it differ from executing a .py file?

Sample AnswerThe REPL is designed for experimentation and rapid testing of small pieces of code, providing immediate feedback on single lines or small blocks of logic. In contrast, executing a `.py` file runs an entire program from start to finish, which is intended for production scripts and complete applications. The REPL is ideal for exploring libraries and debugging isolated functions, while scripts are meant for persistent, reusable programs.

3. Why is setting a breakpoint and stepping through code considered a more efficient debugging technique than scattering print() statements throughout your script?

Sample AnswerUsing a debugger allows you to inspect the state of variables in real-time without cluttering your code with temporary `print()` statements that must be cleaned up later. It also lets you pause at any point and examine the call stack, making it easier to trace the logical flow of complex functions. Debugging is non-invasive, saves time, and provides context (like variable types and nested scope values) that simple print statements cannot offer.

4. The indent-rainbow and Bracket Pair Colorizer extensions were recommended. Why are these particularly useful for a beginner in Python, given Python's reliance on indentation?

Sample AnswerPython uses indentation to define code blocks, so errors from mixing tabs/spaces or inconsistent nesting are common and often invisible to the naked eye. `indent-rainbow` makes indentation levels visually distinct, helping beginners immediately see where a block begins and ends. `Bracket Pair Colorizer` helps track nested parentheses and brackets in complex expressions, reducing syntax errors related to unbalanced delimiters.

Essay Questions

Answer the following questions in 300–400 words each.

5. Describe the "workflow" you would use to solve a simple programming problem (e.g., calculating the average of three numbers) using the tools we have set up. Include how you would use the REPL for testing, how you would use the IDE for writing, and how you would use the debugger if you encountered a bug.

Suggested outline:

6. Write a troubleshooting guide for a student who has installed Python but cannot get their "Hello, World!" script to run. Address the following scenarios: (a) the terminal says 'python' is not recognized, (b) the script runs but immediately closes (double-click issue), and (c) VS Code reports "No Python interpreter selected."

Suggested outline:

Research Questions

These questions require additional research beyond the tutorial content.

7. Research the difference between the Terminal, the Debug Console, and the Output panel in VS Code. Why does print() output sometimes go to the Debug Console and sometimes to the Terminal? (Hint: Look up the difference between the internal console and the integrated terminal).

8. Research the concept of breakpoint conditions in debuggers (e.g., "break only when the loop counter equals 10"). Why are conditional breakpoints more powerful than standard breakpoints in large loops or recursive functions?

9. Explore the interactive features of Jupyter Notebooks (.ipynb). How does a Jupyter Notebook combine the immediate feedback of the REPL with the permanence of a script? Write a short paragraph on why data scientists prefer Jupyter for exploratory data analysis.

Previous | Tutorial index | Next