Fundamentals 8 min read

Python Quirks and Tricky Behaviors: 10 Quiz Questions with Explanations

This article presents ten Python quiz questions that reveal surprising language behaviors—such as banker's rounding, attribute lookup order, handling of -0.0, lazy evaluation of all/any, negative string multiplication, object‑type relationships, sum() defaults, name binding in functions, handling of NaN/Infinity, and floating‑point precision limits—along with detailed explanations for each.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Quirks and Tricky Behaviors: 10 Quiz Questions with Explanations

01. round() function – The code print(round(9/2)) print(round(7/2)) print(round(3/2)) produces 4, 4, 2 because Python uses banker's rounding, rounding half values to the nearest even integer.

02. Instance attribute lookup – The snippet class A: ans = 9 def __init__(self): self.answer = 10 self.__add__ = lambda x, y: x.answer + y def __add__(self, y): return self.answer - y print(A() + 4) outputs 6 . Python first searches the instance for the attribute, then the class; the custom __add__ defined on the instance is ignored, so the class method runs, computing 10‑4 .

03. max() with -0.0 – The code print(max(-0.0, 0.0)) returns -0.0 because -0.0 and 0.0 compare equal, and max returns the first occurrence.

04. Lazy operators (all / any) – The snippet print(all([])) print(any([])) produces True, False . all returns True for an empty iterable (no false element found), while any returns False (no true element found).

05. Negative string multiplication – The code print("Can you give 50 claps to this story?" * (-1)) outputs an empty string because a negative repeat count is treated as zero.

06. Everything is an object – The snippet print(isinstance(object, type)) print(isinstance(type, object)) print(isinstance(type, type)) print(isinstance(object, object)) prints True, True, True, True since in Python all types are objects and type itself is an instance of type .

07. sum() with non‑numeric start – The code print(sum("")) print(sum("", [])) print(sum("", {})) yields 0, [], {} because the empty string is treated as an empty iterable and the start value is returned unchanged.

08. Lazy class example – The snippet class follow: def func(self): return follow() a = follow() follow = int print(a.func()) outputs 0 . The name follow is rebound to int only after the class is defined; the method returns a new int instance.

09. Summing imaginary parts – The code print(sum([a.imag for a in [0, 5, 10e9, float('inf'), float('nan')]])) produces 0.0 because all numbers have an imaginary part of zero.

10. Floating‑point precision limits – The snippet a = (1 << 53) + 1 print(a + 1.0 > a) raises an error in the original text, but the explanation shows that converting the large integer to float loses precision, making the comparison evaluate to False .

These ten puzzles illustrate subtle Python behaviors that can trap developers, helping them understand the language’s internal mechanics and avoid unexpected bugs.

PythonProgrammingFundamentalsCodeQuiz
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.