LearnFrenzy
Sign in Take a free test

Python Programming · Topic 24 of 25

Tricky Python Output Questions Questions and Concepts

  • 4 exercises
  • 50 questions
  • ~60 min
  • Answers explained

Coding Techniques

Tricky Python Output Questions · Study material

Trace names and objects in a table

Write down each name and the object it refers to after every line. Most "tricky" outputs become obvious once you see which names share an object.

# A reliable way to solve output questions: track every name in a small table.
#   step              a          b          c
#   a = [1, 2]        [1, 2]     -          -
#   b = a             ↑ same     [1, 2]     -
#   a.append(3)       [1, 2, 3]  (same obj) -
#   c = a[:]          [1, 2, 3]  [1, 2, 3]  copy [1, 2, 3]
#   a[0] = 9          [9, 2, 3]  [9, 2, 3]  [1, 2, 3]
a = [1, 2]
b = a
a.append(3)
c = a[:]
a[0] = 9
print(a, b, c)

Output

[9, 2, 3] [9, 2, 3] [1, 2, 3]

New object or same object?

OperationEffect on other names
b = asame object
a.append(x), a += [x], a[0] = x, a.sort()changes the shared list
a = a + [x], a = sorted(a), a = a[:]a now refers to a new list
s += "x" (str), n += 1 (int), t += (x,) (tuple)always a new object
copy(), list(a), a[:]new outer list, shared inner objects
copy.deepcopy(a)everything new

Print details that change the answer

print(1, 2, 3)              # 1 2 3        (sep=" ")
print(1, 2, 3, sep="")      # 123
print("a", end=""); print("b")   # ab
print([1, "1"], ("x",))     # [1, '1'] ('x',)   lists show repr(): quotes around strings
print("x")                  # x            print() shows str(): no quotes
print({}, set(), ())        # {} set() ()

Output question checklist

  • Track which names share an object.
  • Check / vs // vs int() and round() rules.
  • Remember which methods return None.
  • Watch for mutable defaults and late-binding lambdas.
  • Remember generators and iterators are one-shot.
  • Follow try/except/else/finally in order.
  • Reproduce print's spacing and quotes exactly.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Python Programming

Why it matters

  • Python is the most popular first language and the default for data science, AI, automation and scripting
  • Campus coding rounds and technical interviews increasingly allow – or prefer – Python
  • "Predict the output" questions on lists, strings, dictionaries and mutable defaults are a favourite in written tests
  • Clean Python with comprehensions, generators and OOP is what product companies expect from freshers

Who it is for

  • School students (Class 11–12 computer science with Python) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, coding tests and Python interviews
  • Anyone moving into data science, AI or automation who needs strong Python fundamentals

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs: syntax errors, runtime exceptions and logical bugs
  • Coding problems with solutions and real output, every one run on Python 3 – open any example in the online Python compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture tests
  • Python interviews for freshers
  • Data science interviews
  • CBSE / university practicals
  • GATE CS

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, errors, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links