What is the Python Dunder Method for the “not and” Operator?

In Python, “dunder” methods, short for “double underscore” methods, are special methods that allow developers to define the behavior of built-in operations for custom objects. For instance, when you use the + operator to add two objects, Python internally calls the __add__ method. Similarly, other operators have their corresponding dunder methods. However, the term “not … Read more

Boolean Operators in Python (and, or, not): Mastering Logical Expressions

Understanding Boolean Operators Boolean operators in Python help you create conditional statements to control the flow of your program. Python provides three basic Boolean operators: and, or, and not. These operators help you construct sophisticated expressions to evaluate the truth or falsity of different conditions. And Operator The and operator returns True if both of … Read more

“is” vs “==” Python Identity and Equality

πŸ’¬ Question: What is the difference between Python’s is and == operators? Answer The Python “==” operator compares equality of values whereas the Python “is” operator compares identity of objects, i.e., do the two operands point to the same object in memory. For example, the expression [1, 2, 3] == [1, 2, 3] returns True … Read more

How to Repeat a String Multiple Times in Python

Problem Formulation and Solution Overview In this article, you’ll learn how to repeat a string multiple times in Python. Over your career as a Python coder, you will encounter situations when a string needs to be output/displayed a specified number of times. The examples below offer you various ways to accomplish this task. πŸ’¬ Question: … Read more

Python’s && Equivalent: Logical And

In Python, the equivalent of && (logical-and) in programming languages such as C++ or Java in an if-statement is and. Using && will raise a SyntaxError, so use and instead! Overview Problem: What is the equivalent of &&(logical-and) in Python? Example: Let’s have a look at the following example: Output: You will get the following … Read more

Python __init_subclass__() Magic Method

The Python class.__init_subclass__(cls) method is called on a given class each time a subclass cls for that class is created. Syntax class.__init_subclass__(cls) We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this … Read more

Python __subclasscheck__() Magic Method

Syntax class.__subclasscheck__(self, subclass) Python’s class.__subclasscheck__(self, subclass) method implements the issubclass(subclass, class) built-in function. It should return True if subclass is a direct or indirect subclass of class using Python inheritance to define the subclass relationship. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all … Read more

Python __sizeof__() Method

Syntax object.__sizeof__(self, other) The Python __sizeof__() method returns the size of the object in bytes. The sys.getsizeof() method internally call’s __sizeof__() and adds some additional byte overhead, e.g., for garbage collection. Basic Example The following code snippet creates a list object x with three integers and measures its size in bytes (104) using the x.__sizeof__() … Read more

Python __trunc__() Magic Method

Syntax and Definition object.__trunc__(self) The Python __trunc__() method implements the behavior of the math.trunc() function. For example, if you attempt to call math.trunc(x), Python will run the x.__trunc__() method to obtain the return value. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder … Read more

Python __floor__() Magic Method

Syntax and Definition object.__floor__(self) The Python __floor__() method implements the behavior of the math.floor() function. For example, if you attempt to call math.floor(x), Python will run the x.__floor__() method to obtain the return value. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder … Read more