Python Articles

Page 678 of 855

3Sum in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 6K+ Views

The 3Sum problem asks us to find all unique triplets in an array that sum to zero. Given an array of integers, we need to identify three elements a, b, c such that a + b + c = 0. For example, in array [-1, 0, 1, 2, -1, -4], the triplets [[-1, -1, 2], [-1, 0, 1]] satisfy this condition. Algorithm Approach We use the two-pointer technique with these steps ? Sort the array to enable two-pointer approach Fix one element and use two pointers for the remaining two elements Skip duplicates to ensure unique ...

Read More

Container With Most Water in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 512 Views

The Container With Most Water problem asks us to find two vertical lines that can hold the maximum amount of water. Given an array of heights, we need to find two positions that form a container with the largest area. Problem Understanding Given an array of non-negative integers where each value represents the height of a vertical line, we need to find two lines that together with the x-axis form a container that can hold the most water. The area is calculated as width × minimum_height. Container With Most Water ...

Read More

Regular Expression Patterns in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 593 Views

Regular expressions are patterns used to match character combinations in strings. In Python, most characters match themselves, except for special control characters (+ ? . * ^ $ ( ) [ ] { } | \) which have special meanings. You can escape these control characters by preceding them with a backslash. The following table lists the regular expression syntax available in Python ? Pattern Description Example ^ Matches beginning of line ^Hello matches "Hello world" $ Matches end of line world$ matches "Hello world" . Matches ...

Read More

Regular Expression Modifiers in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 2K+ Views

Regular expression modifiers (also called flags) are optional parameters that control how pattern matching behaves. You can combine multiple modifiers using the bitwise OR operator (|) to customize the search behavior. Common Regular Expression Modifiers Modifier Name Description re.I IGNORECASE Performs case-insensitive matching re.M MULTILINE Makes ^ and $ match start/end of each line re.S DOTALL Makes dot (.) match any character including newline re.X VERBOSE Ignores whitespace and allows comments in patterns re.L LOCALE Uses locale-aware matching (deprecated) re.U UNICODE ...

Read More

Data Hiding in Python

Sarika Singh
Sarika Singh
Updated on 25-Mar-2026 7K+ Views

Data hiding is also known as data encapsulation and it is the process of hiding the implementation of specific parts of the application from the user. Data hiding combines members of class thereby restricting direct access to the members of the class. Data hiding plays a major role in making an application secure and more robust. Data Hiding in Python Data hiding in Python is a technique of preventing methods and variables of a class from being accessed directly outside of the class in which the methods and variables are initialized. Data hiding of essential member functions ...

Read More

Overloading Operators in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 638 Views

In Python, you can customize how operators work with your custom classes through operator overloading. This is achieved by defining special methods (also called magic methods) in your class that correspond to specific operators. Suppose you have created a Vector class to represent two-dimensional vectors, what happens when you use the plus operator to add them? Most likely Python will yell at you. You could, however, define the __add__ method in your class to perform vector addition and then the plus operator would behave as per expectation ? Example class Vector: ...

Read More

Base Overloading Methods in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 483 Views

Python provides special methods (also called magic methods or dunder methods) that you can override in your classes to customize their behavior. These methods allow your objects to work seamlessly with built-in functions and operators. Common Base Overloading Methods Sr.No. Method, Description & Sample Call 1 __init__(self [, args...])Constructor method called when creating an objectSample Call: obj = ClassName(args) 2 __del__(self)Destructor method called when object is garbage collectedSample Call: del obj 3 __repr__(self)Returns unambiguous string representation for developersSample Call: repr(obj) 4 __str__(self)Returns human-readable string representationSample ...

Read More

Destroying Objects (Garbage Collection) in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 1K+ Views

Python deletes unneeded objects (built-in types or class instances) automatically to free the memory space. The process by which Python periodically reclaims blocks of memory that no longer are in use is termed Garbage Collection. Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero. An object's reference count changes as the number of aliases that point to it changes. How Reference Counting Works An object's reference count increases when it is assigned a new name or placed in a container (list, tuple, or dictionary). The object's reference count decreases ...

Read More

Creating Instance Objects in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 38K+ Views

To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts. Creating Instance Objects When you create an object, Python calls the class constructor (__init__ method) to initialize the new instance ? # This would create first object of Employee class emp1 = Employee("Zara", 2000) # This would create second object of Employee class emp2 = Employee("Manni", 5000) Accessing Object Attributes You access the object's attributes using the dot (.) operator with object. Class variables can be accessed using class name ? ...

Read More

Creating Classes in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 2K+ Views

The class statement creates a new class definition. The name of the class immediately follows the keyword class followed by a colon as follows ? class ClassName: 'Optional class documentation string' class_suite The class has a documentation string, which can be accessed via ClassName.__doc__. The class_suite consists of all the component statements defining class members, data attributes and functions. Basic Class Structure A Python class contains attributes (data) and methods (functions). Here's the basic syntax ? ...

Read More
Showing 6771–6780 of 8,549 articles
« Prev 1 676 677 678 679 680 855 Next »
Advertisements