Bird
Raised Fist0

What will be the output of this code snippet?

medium📝 Predict Output Q13 of Q15
Python - Structured Data Files
What will be the output of this code snippet?
import csv
from io import StringIO

csv_data = "name,age\nAlice,30\nBob,25"
file = StringIO(csv_data)
reader = csv.DictReader(file)
for row in reader:
    print(row['name'], row['age'])
AAlice 30 Bob 25
B['Alice', '30'] ['Bob', '25']
C{'name': 'Alice', 'age': '30'} {'name': 'Bob', 'age': '25'}
D30 Alice 25 Bob
Step-by-Step Solution
Solution:
  1. Step 1: Understand the CSV data and DictReader

    The CSV has two rows with columns 'name' and 'age'. DictReader reads each row as a dictionary.
  2. Step 2: Analyze the print statement

    It prints the values of 'name' and 'age' keys separated by space for each row.
  3. Final Answer:

    Alice 30 Bob 25 -> Option A
  4. Quick Check:

    Prints name and age values separated by space [OK]
Quick Trick: DictReader rows are dicts; print keys to get values [OK]
Common Mistakes:
MISTAKES
  • Printing the whole dictionary instead of values
  • Mixing order of printed values
  • Confusing list output with string output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes