Python Articles

Page 682 of 855

Letter Combinations of a Phone Number in Python

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

The Letter Combinations of a Phone Number problem involves generating all possible letter combinations that a string of digits (2-9) could represent on a phone keypad. Each digit maps to a set of letters, similar to old telephone keypads. Phone Keypad Mapping 1 2abc 3def 4ghi 5jkl 6mno 7pqrs 8tuv 9wxyz * 0 # For example, if the input is "23", the possible combinations are ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. Algorithm Approach We'll use backtracking to solve this problem: ...

Read More

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 514 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 597 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 649 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 490 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
Showing 6811–6820 of 8,546 articles
« Prev 1 680 681 682 683 684 855 Next »
Advertisements