Updating Lists in Python

Gireesha Devara
Updated on 14-Apr-2025 16:24:29

52K+ Views

In Python, lists are one of the built-in data structures that are used to store collections of data. The lists are mutable, which means we can modify its element after it is created. You can update single or multiple list elements using append, insert, extend, remove, and clear to change the list content by adding, updating, or removing elements from the list object. In this article, we will discuss how we can update an existing element in the list. The list is an index-based sequential data structure so that we can access the list elements by their index position, ... Read More

Understanding the str Function in Python OOP

Akshitha Mote
Updated on 14-Apr-2025 16:23:59

764 Views

In Python, the __str__() method is used to compute the informal or human readable string representation of an object. This method is called by the built-in print(), string and format() functions. This method is called by the implementation of the default __format__() method and the built-in function print(). This method returns the string object. In a class, if the __str__() method is not defined, Python will fall back to using the __repr__() method. If neither of these dunder methods is defined, the class will return the default string representation of the object Example Here, we have created a class named ... Read More

Understanding Python repr Function

Akshitha Mote
Updated on 14-Apr-2025 16:22:45

839 Views

In Python, the __repr__() method is a built-in function used to compute the official string representation of an object. It is used in debugging and logging purposes as it provides a detailed and clear representation of an object. This method is also known as dunder method as it begins with a double underscore in the beginning and at the end. In a class, if __str__() method and the __repr__() method is defined ,then the __str__() method is executed. Example Here, we have created a class named Methods and defined the __repr__() method. When we execute the code, the output ... Read More

Python Constructor Method Explained

Akshitha Mote
Updated on 14-Apr-2025 16:20:56

1K+ Views

In Python, the__init__() method is a special method within a class that gets automatically called when an object is created. It is used to initialize the attributes of the object. It is also known as a constructor. The self is always the first argument to the __init__() method that refers to the instance of the class being created, allowing access to its attributes and methods. Lets try to understand the __init__() method with an example - class Employee: def __init__(self): self.name=input("Enter employee name:") self.id=int(input("Enter id:")) ... Read More

How a Python Function Can Return a Function

Sarika Singh
Updated on 14-Apr-2025 15:45:33

19K+ Views

In Python, functions are treated as first-class objects, which means that all functions in Python are treated like any other object or variable. You can assign a function to a variable, pass it as an argument to another function, return it from a function, or even store it in data structures like lists. One very useful feature in Python is that a function can return another function. This means you can define a function inside another function and return it as a result. This is used in advanced programming concepts like closures, decorators, and higher-order functions. Returning a Simple Function ... Read More

Use Lambda Function in Python

Sarika Singh
Updated on 14-Apr-2025 15:39:42

3K+ Views

Lambda Function in Python Lamda functions are inline functions, i.e., functions that are written in a single line instead of using multiple lines. These are anonymous functions (functions without a name). We can define a lambda function using the lambda keyword. These are typically used when we need to return a function from another function or accept a function as a parameter. We can also use lambda functions with built-in functions like map(), filter(), and sorted(), where we need to perform a small operation quickly without writing a separate full function. Lamda function is basically a shortcut for writing a ... Read More

Convert Python Tuple to C Array

Vikram Chiluka
Updated on 14-Apr-2025 15:21:18

10K+ Views

In this article, we will show you how to convert a Python tuple to a C array. Python does not have a built-in array data type like other programming languages, but you can create an array using a library like Numpy. Tuple to Array Conversion Using Python The Python NumPy library provides various methods to create manipulate and modify arrays in Python Following are the two important methods that helps us to convert a tuple into an array − Using numpy.asarray() method Use numpy.array() method If you haven't already installed NumPy on your system, run the following ... Read More

Check If Two Lists of Tuples Are Identical in Python

Sindhura Repala
Updated on 14-Apr-2025 14:19:12

3K+ Views

Lists of Tuples in Python A tuple is a built-in Python data structure for storing multiple items in a single variable. While both tuples and lists can organize ordered collections of items, tuples are immutable, whereas lists are mutable. A list can store heterogeneous values, including various types of data such as integers, floating-point numbers, strings, and more. A list of tuples refers to tuples encapsulated within a list. Comparing Lists of Tuples The '==' operator is used to compare two iterables and determine if they are equal. It is used to check whether two lists are equal. Therefore, we ... Read More

Check if All the 1s in a Binary String are Equidistant in C++

AYUSH MISHRA
Updated on 14-Apr-2025 12:40:11

194 Views

In this problem, we are given a binary string, and we have to check if all the 1s present in the binary string are at an equivalent distance from each other. Example 1 Input: binaryString = "10101010" Output: Yes Explanation The positions of 1s in the given string are 1, 3, 5, and 7.Here, the difference between consecutive 1s is 2 (as 7-5 equals 5-3 equals 3-1), so all 1s are at an equivalent distance from each other. Example 2 Input: binaryString = ... Read More

Difference Between TreeSet and HashSet in Java

Alshifa Hasnain
Updated on 14-Apr-2025 12:15:27

19K+ Views

In Java,  HashSet and TreeSet both belong to the collection framework. HashSet is the implementation of the Set interface, whereas TreeSet implements a sorted set. TreeSet is backed by TreeMap while HashSet is backed by a HashMap. Difference Table The following are the key differences between HashSet and TreeSet : Sr. No. Key ... Read More

Advertisements