GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com โ€” ET6-Programming-With-Python/2_predictive_stepping at main ยท MIT-Emerging-Talent/ET6-Programming-With-Python

Expand file tree

/

2_predictive_stepping

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

2_predictive_stepping

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
.assets
.assets
 
 
examples
examples
 
 
exercises
exercises
 
 
README.md
README.md
 
 
lesson_plan.md
lesson_plan.md
 
 
prep_work.md
prep_work.md
 
 
python_debugger_in_vscode.mp4
python_debugger_in_vscode.mp4
 
 

README.md

Outline

Predictive Stepping

Predictive Stepping is a study method where you quiz yourself on each step of a program's execution, checking your predictions with using a debugger. After you have a basic familiarity with your debugger, this study method is simple and effective - it's like having a teacher by your side every day of the week!

  • Learning Objectives
  • What is Programming?
  • Program State
  • Studying the Examples and Exercises
  • Prep Work
  • Lesson Plan

What is Programming?

Programming is communication. It's also a lot of other things, but this introduction will focus on the different ways you communicate with your code.

When you write code you are really just writing a text document, exactly like you might write an email or a note. The biggest difference between an email and a computer program is who you are writing for.

When you write an email you're writing for the person who will be reading it. When you write a computer program you are writing for 3 very different audiences at the same time! One single document (your code) needs to be understandable to:

  • Developers: A developer needs to read your code and understand what you were trying to do and why.
  • Computers: Your code needs to have perfect syntax so the computer can parse it, and you must write instructions that are allowed by the programming language or the computer will throw an error.
  • Users: The instructions you give to the computer must create an intuitive and pleasant experience for the user.

Being a developer means understanding how all these characters interact, and then communicating with everyone involved to deliver quality software within your project's constraints. This diagram shows the different channels of communication in a simple Python program:

a program

PS. In the examples and exercises for this chapter you will be both the developer and the user, running the program and interacting with it from command line.

Program State

Program State is a term that refers to the values stored in memory at EACH SPECIFIC MOMENT of execution. These values can change often, sometimes at every step of your program! Learning to program requires you to understand:

  • how the Python environment represent memory
  • how the Python environment interpret your code line-by-line
  • how each instruction interacts with program memory

Your first goal is learning to see what is happening inside your program at each step using the VSCode Debugger. It will take some practice to use effectively and is worth every second. You can see every value in memory, at every step of your program's execution. Debuggers are designed to help you understand how memory is structured in Python.

Studying the Examples and Exercises

This chapter has some examples that focus on single Python language features, and some programs that combine language features into a program.

To carefully study a program, practice following these steps. You know you understand a program when you can predict each step without mistakes:

  1. Don't step forward yet!
  2. Predict which line will execute next and how it will change program state (variables & callstack)
  3. Step forward in the program.
  4. Check your prediction.
  5. Investigate if you were wrong: Is this a bug in the program, or is it something you did not understand?

PS. You can write all the comments you want!

Learning Objectives

Priorities: ๐Ÿฅš, ๐Ÿฃ, ๐Ÿฅ, ๐Ÿ” (click to learn more)

There is a lot to learn in this repository. If you can't master all the material at once, that's expected! Anything you don't master now will always be waiting for you to review when you need it. These 4 emoji's will help you prioritize your study time and to measure your progress:

  • ๐Ÿฅš: Understanding this material is required, it covers the base skills you'll need for this module and the next. You do not need to finish all of them but should feel comfortable that you could with enough time.
  • ๐Ÿฃ: You have started all of these exercises and feel you could complete them all if you just had more time. It may not be easy for you but with effort you can make it through.
  • ๐Ÿฅ: You have studied the examples and started some exercises if you had time. You should have a big-picture understanding of these concepts/skills, but may not be confident completing the exercises.
  • ๐Ÿ”: These concepts or skills are not necessary but are related to this module. If you are finished with ๐Ÿฅš, ๐Ÿฃ and ๐Ÿฅ you can use the ๐Ÿ” exercises to push yourself without getting distracted from the module's main objectives.

VSCode Debugger

  • ๐Ÿฅš Using Breakpoints:
    • You can add and remove breakpoints, before and while you are debugging a program.
    • You can enable and disable breakpoints without removing them.
  • ๐Ÿฅš Launching the Debugger: You can open the debugger pane and launch a single-file Python program.
  • Debugger Buttons
    • ๐Ÿฅš Skip Ahead: You can skip ahead to the next breakpoint or the end of the program.
    • ๐Ÿฅš Step Over: You can step over a function call to ignore its implementation.
    • ๐Ÿฅš Step In: You can step into a function call to debug it's implementation.
    • ๐Ÿฅš Step Out: You can step out of a function call to resume debugging the main program.
    • ๐Ÿฅš Restart: You can restart your debugger at the beginning of the program.
    • ๐Ÿฅš Exiting: You can exit the debugger at any point of execution.
  • ๐Ÿฅš Reading State: At each point in a program's execution, you can find the value of any declared variable in the Variables pane.
  • The Call Stack:
    • ๐Ÿฅš You understand the relationship to between the callstack pane, local variables and function calls.
    • ๐Ÿฅš You can view the local variables for each level of the callstack by clicking on it.
  • ๐Ÿฃ Variable Categories: You can explain the difference between Locals, Globals, Function Variables, Class Variables, and Special Variables
  • ๐Ÿฃ You can predict a program's behavior while stepping through in the debugger:
    • Which line will execute next?
    • What will change in memory (callstack and variables)?
  • ๐Ÿฃ You can identify steps of execution that surprise you. This will help understand the gap between what a program does do, and what it should do.
  • ๐Ÿฅ Watch Expressions: You can create helpful watch expressions and use them to track information about your program that is not explicitly declared in the code.

Other Visualization Tools

Other options besides the debugger in VSCode:

  • ๐Ÿฅš You can use a trace table to manually execute a program with pencil and paper.
  • ๐Ÿฅš You can use Python Tutor to step through, visualize and predict program execution.
    • Python Tutor has a more approachable visualization than the VSCode Debugger, but can not handle large programs.
    • This does not replace VSCode as a code editor.
  • ๐Ÿฃ You can use the built-in Python trace module from CLI to help understand a script's execution:
    • $ python -m trace -t path/to/file.py: prints each line as it's executed to the console.
    • $ python -m trace -c path/to/file.py: generates a new .cover file indicating how many times each line of code was executed.
  • ๐Ÿฅ You can use the built-in python pdb module from CLI to step through a script in the interactive debugger:
    • $ python -m pdb path/to/file.py: starts the script in pdb
    • (Pdb) step steps to the next line
    • (Pdb) ll prints the code surrounding your current line
    • (Pdb) pp <expression> evaluates the expression and pretty-prints the result
    • (Pdb) where prints the current callstack
    • (Pdb) help prints the options available in the Python debugger
    • (Pdb) quit quits the debugger indicating how many times each line of code was executed.
  • (optional) You can use Thonny to write, step through, visualize and debug small Python programs.
You are visitor 01402792 โ€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร—600. No cookies required.