Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 4 · Memory, Profiling, OptimizationAST manipulation with the `ast` modulepredict62 / 161
+125 XP
Task
📝 **Question:** Predict the exact output. Three lines of Python are parsed with \`ast.parse\`. Then the trace asks: (1) what is the root node's type? (2) how many statements in the body? (3) which node type wraps each statement? (4) drill into the first assignment — target name, value class, operator class, and finally \`ast.unparse\` round-trip. 📋 Pick the right answer. 💡 **Hint:** Re-read the theory above if unsure.
Predict the output

Read the code carefully

import ast


src = """x = 1 + 2
y = x * 3
print(y)
"""

tree = ast.parse(src)
print(type(tree).__name__)
print(len(tree.body))
for node in tree.body:
    print(type(node).__name__)

first = tree.body[0]
print(first.targets[0].id)
print(type(first.value).__name__)
print(first.value.op.__class__.__name__)
print(ast.unparse(first))

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…