Fundamentals 9 min read

10 Surprising Python Behaviors That Can Trip Up Your Code

This article presents ten Python quiz questions that reveal unexpected language quirks—from bankers rounding and attribute lookup order to the behavior of empty iterables, negative string multiplication, and floating‑point precision limits—each explained with code examples and clear reasoning.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
10 Surprising Python Behaviors That Can Trip Up Your Code

01. round() Function

Predict the output of the following code:

print(round(9/2))
print(round(7/2))
print(round(3/2))

Answer: 4, 4, 2.

Explanation: In Python, round implements bankers rounding, where half values are rounded to the nearest even integer.

02. Class Attribute Lookup Example

Predict the output of the following code:

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)

Answer: 6.

Explanation: Python searches for attributes first on the instance, then the class, then parent classes. The expression evaluates as (10 – 4) = 6.

03. max() with Negative Zero

Predict the output of the following code: print(max(-0.0, 0.0)) Answer: -0.0.

Explanation: -0.0 and 0.0 are considered equal, and max returns the first occurrence, which is -0.0.

04. Lazy Operators

Predict the output of the following code:

print(all([]))
print(any([]))

Answer: True, False.

Explanation: all([]) returns True because there are no false elements; any([]) returns False because there are no true elements.

05. Negative Multiplication of a String

Predict the output of the following code:

print("Can you give 50 claps to this story?" * (-1))

Answer: an empty string.

Explanation: A negative multiplier is treated as zero, producing an empty sequence of the original string.

06. Everything Is an Object

Predict the output of the following code:

print(isinstance(object, type))
print(isinstance(type, object))
print(isinstance(type, type))
print(isinstance(object, object))

Answer: True, True, True, True.

Explanation: In Python, all types (including int, str, and object) are instances of the type class, and everything is an object.

07. sum() Function Edge Cases

Predict the output of the following code:

print(sum(""))
print(sum("", []))
print(sum("", {}))

Answer: 0, [], {}.

Explanation: sum iterates over the provided iterable; an empty string is treated as an empty iterable, so the start value is returned unchanged.

08. Name Binding and Laziness

Predict the output of the following code:

class follow:
    def func(self):
        return follow()

a = follow()
follow = int
print(a.func())

Answer: 0.

Explanation: The method body is executed only when called; at that time, the name follow refers to int, so a.func() returns a new int instance, which prints as 0.

09. Summing Imaginary Parts

Predict the output of the following code:

print(sum([a.imag for a in [0, 5, 10e9, float('inf'), float('nan')]]))

Answer: 0.0.

Explanation: All numeric types inherit from the base object and have an imag attribute that defaults to 0, so the sum of all imaginary parts is 0.0.

10. Large Integer vs Float Comparison

Predict the output of the following code:

a = (1 << 53) + 1
print(a + 1.0 > a)

Answer: False (the comparison fails because the large integer cannot be represented exactly as a float, causing the conversion to round down).

Explanation: Python’s arbitrary‑precision integers exceed float precision. Converting a to a float rounds it to 9007199254740992.0, adding 1.0 yields the same float, so the comparison a + 1.0 > a evaluates to False.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonprogrammingTipsQuizTraps
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

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.