Programming Articles - Page 3241 of 3366

How to get the size of a list in Python?

Akshitha Mote
Updated on 23-Jun-2025 19:20:39

6K+ Views

In Python, a list is an ordered sequence that can hold several object types, such as integers, characters, or floats. In other programming languages, a list is equivalent to an array. In this article, we need to determine the size of a list, which refers to its length. For instance, the list [10, 20, 30, 40] has a length of 4. Using 'len()' method In Python, the len() method returns the number of items in a list. Example In the following example, we have created an input list and to find the length of the list, it is passed an argument ... Read More

What does the Star operator mean in Python?

Gireesha Devara
Updated on 30-Apr-2025 17:40:21

21K+ Views

The asterisk (*) operator in Python has more than one meaning attached to it. We can use it as a multiplication operator, a repetition operator, for unpacking the iterables, and as a function *args. A single asterisk, as used in a function declaration, allows a variable number of arguments to be passed from the calling environment. Inside the function, it behaves as a tuple. As the multiplication operator Generally, the start (*) operator is used for multiplication purposes. For numeric data, the asterisk (*) is used as a multiplication operator. Let’s take an example and see how the star operator works ... Read More

How to create a dictionary with list comprehension in Python?

Gireesha Devara
Updated on 29-May-2025 11:37:20

758 Views

By using the dict() method in Python, we can create a dictionary with the list comprehension. Following is the syntax of dict() method- dict(**kwarg) Keyword arguments. We can pass one or more keyword arguments. If no keyword argument is passed, then the dict() method will create an empty dictionary object. The syntax for creating a dictionary with list comprehension: dict(list_comprehension) Creating Dictionary using List Comprehension Instead of sending a number of keywords here, we need to send a list of tuples with key-value pairs to the dict() method. Let’s take an example and create a dictionary using a ... Read More

What does ** (double star) and * (star) do for parameters in Python?

Gireesha Devara
Updated on 09-Sep-2023 22:59:21

7K+ Views

While creating a function the single asterisk (*) defined to accept and allow users to pass any number of positional arguments. And in the same way the double asterisk (**) defined to accept any number of keyword arguments. The single asterisk (*) can be used when we are not sure how many arguments are going to be passed to a function and those arguments that are not keywords. The double asterisk (**kwargs) can be used to pass keywords, when we don't know how many keyword arguments will be passed to a function, which will be in a dict named ... Read More

How can I clear or empty a StringBuilder in Java.

mkotla
Updated on 24-Feb-2020 12:21:39

3K+ Views

You can either setLength to be 0 or create a new StringBuilder() instance. See the example below −Examplepublic class Tester {    public static void main(String[] args) {       StringBuilder builder = new StringBuilder();       builder.append("sample");       System.out.println(builder.toString());       builder.setLength(0);       System.out.println(builder.toString());       builder.append("sample");       System.out.println(builder.toString());    } }Outputsample sample

Difference between declaring a variable before or in a Java loop.

Giri Raju
Updated on 30-Jul-2019 22:30:21

135 Views

Performance wise, there is hardly any difference. But it is good to keep a variable local to the scope it is used. So declaring a variable inside Java loop is generally preferred.

How to convert List to int[] in Java?

Sreemaha
Updated on 30-Jul-2019 22:30:21

970 Views

You can simply iterate through list and fill the array as shown below −import java.util.ArrayList; import java.util.List; public class Tester {    public static void main(String[] args) {       List list = new ArrayList();       list.add(new Integer(1));       list.add(new Integer(2));       list.add(new Integer(3));       list.add(new Integer(4));       int[] array = new int[list.size()];       for(int i=0;i

How to pass a function as a parameter in Java

Prabhas
Updated on 17-Jun-2020 11:36:16

5K+ Views

Yes. From Java 8 onwards, we can do so using method references.Method references help to point to methods by their names. A method reference is described using "::" symbol. A method reference can be used to point the following types of methods −Static methodsInstance methodsConstructors using new operator (TreeSet::new)Method Reference ExampleCreate the following Java program using any editor of your choice in, say, C:\> JAVA.Java8Tester.java Live Demo import java.util.List; import java.util.ArrayList; public class Java8Tester {    public static void main(String args[]) {       List names = new ArrayList(); names.add("Mahesh");       names.add("Suresh");       names.add("Ramesh");       ... Read More

How to return 2 values from a Java method

seetha
Updated on 17-Jun-2020 11:32:59

792 Views

A method can give multiple values if we pass an object to the method and then modifies its values. See the example below −Examplepublic class Tester {    public static void main(String[] args) {       Model model = new Model();       model.data1 = 1;       model.data2 = 2;       System.out.println(model.data1 + ", " + model.data2);       changeValues(model);       System.out.println(model.data1 + ", " + model.data2);    }    public static void changeValues(Model model) {       model.data1 = 100;       model.data2 = 200;    } } class Model {    int data1;    int data2; }Output1, 2 100, 200

Why can't static method be abstract in Java?

radhakrishna
Updated on 30-Jul-2019 22:30:21

2K+ Views

A static method belongs to class not to object instance thus it cannot be overridden or implemented in a child class. So there is no use of making a static method as abstract.

Advertisements