Practical Usage Methods
Interactive Shell Techniques
Basic Arithmetic Operations
>>> 10 + 20
30
>>> _ * 2
60
>>> _ - 15
45
Complex Calculations
>>> def complex_calculation(x):
... return x ** 2 + 5
>>> complex_calculation(4)
21
>>> _
21
List and Collection Manipulation
List Comprehension
>>> numbers = [1, 2, 3, 4, 5]
>>> [x * 2 for x in numbers]
[2, 4, 6, 8, 10]
>>> _
[2, 4, 6, 8, 10]
graph TD
A[Last Result Usage] --> B[Interactive Exploration]
A --> C[Quick Calculations]
A --> D[Temporary Data Manipulation]
Comparison of Usage Methods
| Method |
Scope |
Reliability |
Use Case |
Interactive _ |
REPL Only |
Low |
Quick Calculations |
| Explicit Variables |
All Contexts |
High |
Structured Programming |
| Chained Operations |
Limited |
Medium |
Intermediate Tasks |
Advanced Interaction Patterns
Function Chaining
>>> def process(x):
... return x * 2
>>> def validate(x):
... return x > 10
>>> validate(process(5))
True
>>> _
True
LabEx Learning Tip
When practicing on LabEx platforms, use the last result feature to understand intermediate calculation steps and experiment with different operations.
Potential Pitfalls
- Not suitable for production code
- Limited to interactive environments
- Can lead to confusing code if overused
Code Example on Ubuntu 22.04
$ python3
>>> x = 100
>>> x + 50
150
>>> _
150
>>> _ / 2
75.0
By mastering these practical usage methods, you'll enhance your Python interactive programming skills and develop more flexible coding techniques.