Chapter 2 · Basic Conversations and Code Generation
Chapter goals: Master the use of the interactive REPL, learn to ask effective questions to generate high-quality code, and understand context comprehension capabilities.
2.1 Interactive REPL Basics
After starting claude in a project directory, you enter an interactive conversation environment:
╭───────────────────────────────────╮
│ Welcome to Claude Code! │
│ │
│ /help for help │
│ Type your request and press Enter │
╰───────────────────────────────────╯
>Common Shortcuts and Commands
| Shortcut/Command | Function |
|---|---|
Enter | Send message |
Shift+Tab | Multi-line input mode |
Ctrl+C | Cancel current operation or exit |
/clear | Clear conversation history |
/compact | Compress context (saves tokens on long sessions) |
/help | View help |
/cost | Check current session token usage |
/exit | Exit |
2.2 Direct Questions and Code Generation
Claude Code understands natural language descriptions and generates code:
> Write a Python function that parses a CSV file and returns a list of unique values from a specified columnOutput example:
import csv
from typing import Any
def get_unique_column_values(filepath: str, column_name: str) -> list[Any]:
"""Read a CSV file and return a list of unique values from the specified column."""
values = set()
with open(filepath, newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
if column_name in row:
values.add(row[column_name])
return sorted(values)Questioning Tips
- Be specific: "Write a function that parses CSV and returns unique column values" works better than "help me process CSV"
- Provide constraints: specify language, framework, error handling approach
- Give context: tell it the Python version and dependency libraries your project uses
2.3 Context Comprehension Capability
Claude Code automatically scans the project directory to understand the codebase:
> What API endpoints does this project have?It will:
- Scan the directory structure
- Find route definition files
- Parse the code to extract endpoint lists
- Return results in table format
Found the following API endpoints:
| Method | Path | Description |
|---|---|---|
| GET | /api/users | Get user list |
| POST | /api/users | Create new user |
| GET | /api/users/{id} | Get single user |
...2.4 Output Format
Claude Code replies use Markdown format:
- Code blocks: fenced code blocks with language tags, ready to copy directly
- Tables: structured data display
- Lists: steps and key points
- Bold: key concept highlighting
2.5 Follow-up and Iteration
You can continuously follow up and modify generated code:
> Add type annotations
> Write unit tests for this function
> Change the exception handling to throw a custom exception instead of ValueErrorEach conversation retains the previous context — Claude Code knows which function you're referring to.
Chapter Summary
- Use
claudeto enter the REPL;/clearclears history,/compactcompresses context; - Questions should be specific, providing context like language/framework/constraints;
- Claude Code automatically scans the project directory to understand the codebase;
- Supports multi-turn follow-ups and iterative modifications.
🧪 随堂测验
点击你认为正确的选项。答错时会展示正确答案与原因解析。
1. Which command clears the current conversation history?
2. When the conversation is long and context is near the limit, what command should you use?
3. Which questioning approach is most likely to get high-quality code?
4. How do you check how many tokens the current session has consumed?
🛠️ Hands-on Practice
- Use Claude Code in a project to generate a utility function, then follow up to add tests.
- Have it list all environment variable references in your project.
- Use
/costto check the current session's token usage.
After completing, proceed to Chapter 3.