正在学习
1.5 Your First Prompt: “Hello, Claude!”
Simple greeting program
This program prints a friendly message to the console.
def main():
The print() function outputs text to the terminal.
print("Hello, Claude!")
The following conditional ensures the main function
runs only when this file is executed directly.
if name == "main":
main()
Notice how Claude automatically explains the platform differences. It understands that JavaScript doesn’t use an entry point check like Python’s if __name__ == "__main__":, and it compensates by providing commentary appropriate to the language. This adaptability is what separates Claude from a simple autocomplete tool — it interprets intent and context, not just syntax.
Now, experiment further. Ask Claude to modify your greeting to include a user input or add a timestamp:
“Claude, update the Python version so that it asks for my name, greets me personally, and displays the current time.”
Claude might respond like this:
import datetime
def main():
# Ask the user for their name.
name = input("What is your name? ")
# Get the current time.
current_time = datetime.datetime.now().strftime("%H:%M:%S")
# Print a personalized greeting.
print(f"Hello, {name}! The current time is {current_time}.")
if __name__ == "__main__":
main()
Each time you refine your request, you’ll see Claude adjust intelligently. It reads your intent, applies relevant syntax, and documents what it’s doing — as if you were pair-programming with an experienced engineer who narrates their reasoning out loud.
At this stage, don’t focus on complexity. Your goal is to develop a conversational rhythm with Claude. The more clearly you express your goals, the more precise Claude’s output becomes. If something looks off, ask it to explain its reasoning:
“Claude, why did you choose this approach?”
Claude will answer descriptively, revealing the logic behind its decision — something traditional IDEs or autocomplete systems can’t do.
The takeaway from this first exercise is simple yet profound: Claude isn’t a machine that executes commands; it’s a partner that responds to clarity. You provide intent, it provides structure. You ask questions, it answers with reasoning. This collaboration is the foundation for everything else you’ll build throughout the book.
By the end of this exercise, you’ll have confirmed three things:
- Your setup works correctly.
- Claude responds accurately in your chosen environment.
- You’ve taken your first step toward thinking with an AI rather than through one.
In the next section, we’ll deepen this interaction by exploring how Claude structures conversations, interprets context, and manages multi-step reasoning — the keys to unlocking its full potential as a coding collaborator.
Key Takeaways and Next Steps
By now, you’ve taken your first practical steps into the Claude ecosystem—understanding what Claude Code is, how it differs from other AI assistants, and how to install and interact with it across your preferred development environments. You’ve also written and executed your first prompt, confirming that Claude responds as both a coder and a reasoning partner. These early exercises may seem simple, but they establish the foundation for every advanced technique you’ll explore throughout this book.
The most important realization from this chapter is that Claude Code isn’t just a tool—it’s a conversational collaborator. Its power lies in understanding intent, explaining decisions, and adjusting dynamically as you refine your prompts. The more clearly you communicate your goals, the more effectively Claude can assist you in building clean, functional, and maintainable software.
You also learned that setup matters. Integrating Claude into your existing workflow—whether through VS Code, Cursor, or Zed—turns AI assistance into a natural extension of your development process. Each environment offers different advantages: VS Code for familiarity, Cursor for deep contextual reasoning, and Zed for lightweight performance. The choice doesn’t affect Claude’s intelligence; it affects your comfort and efficiency.
Finally, your first prompt exercise demonstrated that working with Claude is a dialogue, not a command chain. Asking, explaining, revising, and testing all form part of the learning loop. With every interaction, Claude becomes a more precise reflection of your intent, and you gain a deeper understanding of its reasoning capabilities.
In the next chapter, we’ll move beyond setup and greetings to examine how Claude interprets code, understands developer intent, and structures reasoning internally. You’ll learn to see how its contextual memory and model architecture enable large-scale comprehension—laying the groundwork for prompt optimization, debugging, and real-world project automation.
The journey ahead moves from orientation to operation. You now know how to communicate with Claude; next, you’ll learn how Claude thinks about your code.
Chapter 2 – Anatomy of an AI Coding Assistant
2.1 How Claude Understands Code
To use Claude Code effectively, it helps to understand how it perceives and reasons about your code. Unlike a human developer who reads with intent and experience, Claude “reads” through a combination of language modeling, pattern recognition, and contextual weighting. Yet the end result often feels remarkably human. It sees relationships between structures, identifies intent behind functions, and reasons about logic flow. This cognitive depth is what allows Claude to suggest meaningful improvements instead of surface-level completions.
At the core of Claude Code’s intelligence lies a transformer-based architecture—the same family of neural models that power most large language systems today. What distinguishes Claude is how Anthropic has tuned this architecture to focus on reasoned interpretation rather than raw prediction. When you provide Claude with a block of code or a technical question, it doesn’t just look at the next likely token; it constructs a temporary representation of the entire context, mapping dependencies between variables, functions, and comments. This internal structure lets it reason about your intent rather than simply filling in blanks.
Claude’s understanding process can be summarized as three stages:
- Parsing and Context Formation – When you send a prompt, Claude tokenizes every word, symbol, and piece of syntax. It doesn’t “run” your code; instead, it encodes each token into a dense vector representation. These vectors capture relationships between code elements—functions call other functions, loops depend on conditions, and so forth. Claude builds a mental map of your code’s structure and purpose.
- Reasoning and Pattern Matching – Once the model forms context, it applies what Anthropic calls reasoned inference. This means Claude compares your code’s internal representation with millions of examples it has seen during pretraining. If your code resembles a known pattern (e.g., a sorting function, API handler, or test case), Claude draws parallels to infer missing steps, potential optimizations, or likely errors.
- Generation and Justification – When Claude responds, it doesn’t simply output code—it explains it. Every suggestion is guided by both statistical prediction and constitutional reasoning. That’s why Claude often adds comments like “This variable is unused, consider removing it” or “This logic may fail for null input”. It attempts to justify each change, reflecting Anthropic’s design principle of transparent reasoning.
Unlike deterministic tools such as linters or static analyzers, Claude’s understanding is probabilistic. It evaluates context based on likelihood and reasoning consistency rather than absolute correctness. This allows flexibility but also introduces variance: two similar prompts can yield slightly different phrasing or structure. The key is to guide Claude through iterative refinement—explaining what worked and what didn’t—to align its reasoning with your expectations.
Claude also possesses long-context awareness, a significant advantage over many coding assistants. Its extended token window—ranging from 100K to over 200K tokens in advanced versions—lets it hold entire projects in working memory. You can paste multiple related files or documentation, and Claude will interpret relationships across them. This capability allows multi-file reasoning such as identifying where a function is declared, where it’s used, and how its behavior impacts the rest of the system. For large codebases, this is transformative: you can discuss architecture and logic at scale instead of line by line.
The following table summarizes how Claude’s internal understanding differs from traditional developer tools:
| Capability | Static Analyzer | Autocomplete Tool | Claude Code |
|---|---|---|---|
| Input Scope | Single file or syntax tree | Current line or function | Entire codebase or conversation |
| Context Awareness | Local, rule-based | Limited | Global, reasoning-based |
| Output Type | Error reports, warnings | Code fragments | Explanatory, context-rich solutions |
| Adaptability | Fixed rules | Pattern-only | Learns and adjusts through dialogue |
| Understanding Style | Deterministic | Predictive | Interpretive and reasoned |
When you provide Claude with a new code snippet, it’s as if you’re inviting another engineer to read your logic out loud. It doesn’t execute or compile the code—it conceptually simulates its behavior, discussing likely outcomes and trade-offs. This interpretive reasoning is what makes Claude a useful teaching companion as well as a coding assistant.
As you proceed through the next sections, you’ll learn how Claude uses this deep contextual understanding to improve its prompting behavior, detect inconsistencies, and build accurate responses across sessions. You’ll also see how to leverage its reasoning model deliberately—guiding it to think, verify, and refine code the way a senior engineer would.
练习题
What is the purpose of the print() function in Python?
What does the if __name__ == "__main__": conditional check do in Python?
Claude Code is only capable of predicting the next line of code you might write.
In Python, the function used to get user input is called ___.
Explain how Claude Code differs from other AI coding tools in terms of reasoning.
Which of the following are key features of Claude Code as a collaborator? (Select all that apply)
What method is used in Python to get the current time in a specific format?
datetime.now()datetime.strftime()datetime.datetime.now().strftime()time.now()Claude Code is trained with an internal constitution that prioritizes helpfulness, honesty, and safety.
To print a personalized greeting in Python that includes a variable, you would use an ___ statement.
How does Anthropic view the role of Claude Code in relation to human developers?
Which of the following code snippets correctly combines user input with a personalized greeting and time display in Python?
python import datetime name = input("What is your name? ") current_time = datetime.datetime.now().strftime("%H:%M:%S") print("Hello, " + name + "! The current time is " + current_time + ".") python import datetime name = input("What is your name? ") current_time = datetime.datetime.now().strftime("%H:%M:%S") print(f"Hello, {name}! The current time is {current_time}.") python import datetime name = input("What is your name? ") current_time = datetime.datetime.now().strftime("%H:%M") print("Hello, " + name + "! The current time is " + current_time + ".") python import datetime name = input("What is your name? ") current_time = datetime.datetime.now().strftime("%H:%M:%S") print("Hello, {name}! The current time is {current_time}.") Claude Code is designed to act as a predictive autocomplete tool rather than a collaborative reasoning partner.
Explain how Claude's cognitive depth in code understanding differs from basic autocomplete tools.
登录后解锁笔记、知识点解析、AI 问答
立即登录