-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_basic_debugging.py
More file actions
118 lines (97 loc) · 3.79 KB
/
01_basic_debugging.py
File metadata and controls
118 lines (97 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
Tutorial 01: Basic Debugging in VS Code
This script introduces you to the fundamentals of debugging in VS Code:
- Setting breakpoints
- Starting the debugger
- Stepping through code
- Inspecting variables in the Variables panel
- Using the Debug Console
INSTRUCTIONS:
1. Read through the code below to understand what it does
2. Set a breakpoint on line 31 (click in the left margin or press F9)
3. Press F5 to start debugging
4. Use F10 to step through the code line by line
5. Watch how variables change in the Variables panel (left sidebar)
6. Look at the CALL STACK panel - it shows the execution path:
- Bottom: where your program started
- Top: where you are now
- Click different frames to see variables at each level
7. Try evaluating expressions in the Debug Console at the bottom
"""
import numpy as np
def calculate_statistics(numbers):
"""Calculate basic statistics for a list of numbers."""
# SET A BREAKPOINT HERE (Line 30) - Click in the left margin or press F9
# When paused here, check the CALL STACK to see: main() → calculate_statistics()
total = sum(numbers)
count = len(numbers)
average = total / count
# Watch how these variables appear in the Variables panel
maximum = max(numbers)
minimum = min(numbers)
# Convert to numpy array for additional operations
arr = np.array(numbers)
std_dev = np.std(arr)
return {
'total': total,
'count': count,
'average': average,
'max': maximum,
'min': minimum,
'std_dev': std_dev
}
def main():
"""Main function to demonstrate basic debugging."""
print("=" * 50)
print("Tutorial 01: Basic Debugging")
print("=" * 50)
# Sample data
numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
print(f"\nOriginal numbers: {numbers}")
# Call the function - step into it with F11 to see how it works
stats = calculate_statistics(numbers)
# Display results
print("\nStatistics:")
print(f" Total: {stats['total']}")
print(f" Count: {stats['count']}")
print(f" Average: {stats['average']:.2f}")
print(f" Maximum: {stats['max']}")
print(f" Minimum: {stats['min']}")
print(f" Std Dev: {stats['std_dev']:.2f}")
# Let's modify the data
print("\n" + "-" * 50)
print("Adding more numbers...")
numbers.extend([110, 120, 130])
print(f"Updated numbers: {numbers}")
# Calculate again - notice how the stats change
stats = calculate_statistics(numbers)
print("\nUpdated Statistics:")
print(f" Total: {stats['total']}")
print(f" Count: {stats['count']}")
print(f" Average: {stats['average']:.2f}")
print(f" Maximum: {stats['max']}")
print(f" Minimum: {stats['min']}")
print(f" Std Dev: {stats['std_dev']:.2f}")
print("\n" + "=" * 50)
print("Tutorial complete! Try these exercises:")
print("1. Set a breakpoint inside calculate_statistics()")
print("2. Use F11 to step INTO the function")
print("3. Use F10 to step OVER lines")
print("4. Look at the CALL STACK panel:")
print(" - See the function call hierarchy (bottom to top)")
print(" - Click on 'main' frame to see variables in main()")
print(" - Click on 'calculate_statistics' to return to current function")
print("5. In Debug Console, type: numbers[0]")
print("6. In Debug Console, type: sum(numbers)")
print("7. In Debug Console, type: len(numbers)")
print("=" * 50)
if __name__ == "__main__":
# DEBUGGING TIPS:
# - F5: Start/Continue debugging
# - F10: Step Over (execute current line)
# - F11: Step Into (enter function calls)
# - Shift+F11: Step Out (exit current function)
# - Shift+F5: Stop debugging
#
# Try setting a breakpoint on the line below and press F5!
main()