Articles on Trending Technologies

Technical articles with clear explanations and examples

What is the difference between __str__ and __repr__ in Python?

Pythonic
Pythonic
Updated on 24-Mar-2026 314 Views

The __str__ and __repr__ methods in Python serve different purposes for string representation of objects. The __str__ method provides a human-readable representation, while __repr__ provides an unambiguous, developer-friendly representation that ideally can recreate the object. Key Differences The built-in functions str() and repr() call the __str__() and __repr__() methods respectively. The repr() function computes the official representation of an object, while str() returns the informal representation. Method Purpose Target Audience Should be Evaluable? __str__ Human-readable End users No __repr__ Unambiguous Developers Ideally yes Example with Integer Objects ...

Read More

How to remove index list from another list in python?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 3K+ Views

In Python, removing multiple elements from a list using their indices requires careful handling to avoid index shifting issues. This article covers two effective methods: pop() and del keyword. Using pop() method Using del keyword The key principle is to remove elements in descending order of their indices to prevent index shifting issues. Why Sort Indices in Descending Order? When removing elements from a list, removing from the beginning changes the indices of subsequent elements. Here's why descending order is crucial ? Example of Index Shifting Problem # Original list and ...

Read More

How to remove an element from a list by index in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 109K+ Views

In this article, we will show you how to remove an element from a list by index using Python. Here we see 4 methods to accomplish this task − Using the del keyword to remove an element from the list Using the pop() function to remove an element from the list Using slicing to remove an element from the list Using indices to remove multiple elements from the list Assume we have taken a list containing some elements. We will remove a specific item from a list by giving the index value using the above-specified methods. ...

Read More

How to get the last element of a list in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 83K+ Views

In Python, getting the last element of a list is a common operation. There are several approaches to accomplish this task, each with different use cases and behaviors ? Using Negative Indexing Python supports negative indexing, where -1 refers to the last element, -2 to the second-to-last, and so on. This is the most common and pythonic way. Example The following program returns the last element using negative indexing ? # input list numbers = [5, 1, 6, 8, 3] # printing input list print("Input list:", numbers) # getting the last element ...

Read More

Explain the variables inside and outside of a class __init__() function in Python.

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

In Python, variables can be defined inside and outside the __init__() function of a class. Variables outside the __init__() function are called class variables, while variables inside the __init__() function are called instance variables. Understanding the difference is crucial for proper object-oriented programming. Class Variables vs Instance Variables Class variables are shared among all instances of a class, while instance variables are unique to each object. Let's examine this with a practical example − Example class MyClass: stat_elem = 456 # Class variable ...

Read More

What is the correct way to define class variables in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 267 Views

Class variables are variables that are declared outside the __init__ method. These are static elements, meaning they belong to the class rather than to the class instances. These class variables are shared by all instances of that class. Basic Syntax Class variables are defined directly inside the class body, outside any method ? class MyClass: class_var1 = 123 class_var2 = "abc" def __init__(self): # Instance variables go here ...

Read More

How to declare an attribute in Python without a value?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 5K+ Views

In Python, you sometimes need to declare attributes without assigning specific values. The conventional approach is to use None, which represents "no value" in Python. Variables in Python are just names that refer to objects. You cannot have a name that doesn't refer to anything − it must be bound to an object. When you need a placeholder for attributes that don't yet have meaningful values, None is the standard choice. Method 1: Direct Assignment with None You can declare class attributes by directly assigning them None values ? # Creating a class with attributes ...

Read More

What is difference between self and __init__ methods in python Class?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 8K+ Views

In Python classes, self and __init__ serve different but complementary purposes. Understanding their roles is essential for object-oriented programming in Python. What is self? The self parameter represents the instance of a class. It allows you to access the attributes and methods of the class from within its methods. When you call a method on an object, Python automatically passes the object itself as the first argument. Example class Student: def set_name(self, name): self.name = name # self refers to the current ...

Read More

How to create instance Objects using __init__ in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 910 Views

The __init__() method is a special method in Python classes that automatically runs when you create a new instance of a class. It's commonly called a constructor and allows you to initialize object attributes with specific values. Basic __init__() Method Here's how to define a simple __init__() method ? class Student: def __init__(self): self.name = "Unknown" self.age = 0 self.grades = [] # Create an instance student1 ...

Read More

How to write a Python regular expression to match multiple words anywhere?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 3K+ Views

Regular expressions provide a powerful way to match multiple specific words anywhere within a string. Python's re module offers several approaches to accomplish this task efficiently. Using Word Boundaries with OR Operator The most common approach uses word boundaries (\b) combined with the OR operator (|) to match complete words ? import re s = "These are roses and lilies and orchids, but not marigolds or daisies" r = re.compile(r'\broses\b|\bmarigolds\b|\borchids\b', flags=re.I | re.X) print(r.findall(s)) ['roses', 'orchids', 'marigolds'] Using Alternation Groups You can simplify the pattern by grouping alternatives within ...

Read More
Showing 7471–7480 of 61,303 articles
« Prev 1 746 747 748 749 750 6131 Next »
Advertisements