
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 33676 Articles for Programming

3K+ Views
Both str() and repr() methods in python are used for string representation of a string. Though they both seem to serve the same puppose, there is a little difference between them.Have you ever noticed what happens when you call a python built-in function str(x) where x is any object you want? The return value of str(x) depends on two methods: __str__ being the default choice and __repr__ as a fallback.Let’s first see what python docs says about them −>>> help(str) Help on class str in module builtins: class str(object) | str(object='') -> str | str(bytes_or_buffer[, encoding[, errors]]) -> str ... Read More

5K+ Views
In Python, to get values from users, use the input(). This works the same as scanf() in C language. Input multiple values from a user in a single line using input() To input multiple values in one line, use the input() method − x, y, z = input(), input(), input() Let’s say the user entered 5, 10, 15. Now, you can display them one by one − print(x) print(y) print(z) Output 5 10 15 From above output, you can see, we are able to give values to three variables in one line. To avoid using multiple input() methods(depends on ... Read More

1K+ Views
There is always a debate in the coding community on which python version was the best one to learn: Python 2.x or Python 3.x.Below are key differences between pyton 2.x and python 3.x1. The print functionIn python 2.x, “print” is treated as a statement and python 3.x explicitly treats “print” as a function. This means we need to pass the items inside your print to the function parentheses in the standard way otherwise you will get a syntax error.#Python 2.7 print 'Python', python_version() print 'Hello, World!' print('Hello, World!') print "text", ; print 'some more text here'OutputPython 2.7.6 Hello, World! ... Read More

1K+ Views
If you have done little-bit of programming in python, you have seen the word “**args” and “**kwargs” in python functions. But what exactly they are?The * and ** operators did different operations which are complementary to each other depending on where we are using them.So when we are using them in method definition, like -def __init__(self, *args, **kwargs): passAbove operation is called ‘packing” as it pack all the arguments into one single variable that this method call receives into a tuple called args. We can use name other than args, but args is the most common and pythonic way of ... Read More

938 Views
At first, let us create and array of strings:String[] strArr = { "ABCD", "AB", "ABCDEFG", "ABC", "A", "ABCDE", "ABCDEF", "ABCDEFGHIJ" };Now, for shortest to longest pattern, for example A, AB, ABC, ABCD, etc.; get the length of both the string arrays and work them like this:Arrays.sort(strArr, (str1, str2) -> str1.length() - str2.length());The following is an example to sort array of strings by their lengths with shortest to longest pattern:Exampleimport java.util.Arrays; public class Demo { public static void main(String[] args) { String[] strArr = { "ABCD", "AB", "ABCDEFG", "ABC", "A", "ABCDE", "ABCDEF", "ABCDEFGHIJ" }; ... Read More

242 Views
Following is our integer array:Integer[] arr = {20, 50, 100, 150, 200, 250, 300, 350, 400, 500};Now convert the above Integer array to List:List list = new ArrayList(Arrays.asList(arr));Now, to sort the above Integer list in reversed order:Comparator initialComp = Integer::compare; Comparator revComp = initialComp.reversed(); Collections.sort(list, revComp);The following is an example to sort Integer list in reversed order:Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Demo { public static void main(String[] args) { Integer[] arr = {20, 50, 100, 150, 200, 250, 300, 350, 400, 500}; List list = new ... Read More

716 Views
In this article, we will learn how to sort lists containing null values in Java and ensure these nulls are kept at the bottom while upper elements remain in order. This can be done using Comparator.nullsLast, which sorts the non-null elements and places all null elements at last. Problem Statement A list of strings with some string values and nulls is given. Write a Java program to sort a list placing nulls in the end. Input Initial List = ("Jack", null, "Thor", null, "Loki", "Peter", null, "Hulk") Output Initial List = [Jack, null, Thor, null, Loki, Peter, null, Hulk] List ... Read More

574 Views
Let us create a list first with string elements. Some of the elements are null in the List:List list = Arrays.asList("Jack", null, "Thor", null, "Loki", "Peter", null, "Hulk");Now, sort the above list and place nulls first with nullsFirst:list.sort(Comparator.nullsFirst(String::compareTo));The following is an example to sort a List that places nulls first:Exampleimport java.util.Arrays; import java.util.Comparator; import java.util.List; public class Demo { public static void main(String... args) { List list = Arrays.asList("Jack", null, "Thor", null, "Loki", "Peter", null, "Hulk"); System.out.println("Initial List = "+list); list.sort(Comparator.nullsFirst(String::compareTo)); System.out.println("List placing nulls first = "+list); ... Read More

800 Views
Yes, we can sort a list with Lambda. Let us first create a String List:List list = Arrays.asList("LCD", "Laptop", "Mobile", "Device", "LED", "Tablet");Now, sort using Lambda, wherein we will be using compareTo():Collections.sort(list, (String str1, String str2) -> str2.compareTo(str1));The following is an example to sort a list with Lambda in Java:Exampleimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo { public static void main(String... args) { List list = Arrays.asList("LCD", "Laptop", "Mobile", "Device", "LED", "Tablet"); System.out.println("List = "+list); Collections.sort(list, (String str1, String str2) -> str2.compareTo(str1)); System.out.println("Sorted List ... Read More

635 Views
Let’s say the following is our stream:Stream.of("u2", "h9", "s8", "l3")Now, map to get the substring:.map(s -> s.substring(1))Convert to int and find the minimum:.mapToInt(Integer::parseInt) .min()The following is an example to Map and get substring and convert to int:Exampleimport java.util.stream.Stream; public class Demo { public static void main(String[] args) throws Exception { Stream.of("u2", "h9", "s8", "l3") .map(s -> s.substring(1)) .mapToInt(Integer::parseInt) .min() .ifPresent(System.out::println); } }Output2