In this article, we will learn about the purpose of using the Optional.ifPresentOrElse() method in Java 9. The Optional Class The Optional class was introduced in Java 8. Many Java developers face null pointer exceptions to avoid this, we need to put null checks we need to put if, to check if a method is null we can put it in the if statement; otherwise, we have to put it in the else statement. And to check this, we need to put this at multiple points to avoid this situation, We use the Optional class. It avoids the null pointer ... Read More
Since Java 9, we are able to add private methods and private static methods in an interface. The advantage of using private methods in an interface is to reduce code duplication among default and static methods. For instance, if two or more default methods need to share some code, a private method can be created for the same and called from each of the default methods. Different Types of Variables and Methods in an Interface In Java 9, the following variables/methods have been defined in an interface. Constant Variables Abstract Method ... Read More
In this article, we will show you how to get a list of all the keys from a Python dictionary. We can get the list of all the keys from a Python dictionary using the following methods - Using dict.keys() Method Using list() & dict.keys() Function Using List Comprehension ... Read More
In this article, we will show you how to get a value for the given key from a dictionary in Python. Below are the 4 different methods to accomplish this task - Using Dictionary Indexing Using dict.get() Method Using keys() Function Using items() Function Assume we have taken a dictionary containing key-value pairs. We will return the value for a given key from a given input dictionary. Using dictionary indexing In Python, we can retrieve the value from a dictionary by using dict[key]. Example 1 The following program returns the index of the value for a given key ... Read More
A tuple is an ordered and immutable collection of Python objects separated by commas. Like lists, tuples are sequences. Tuples differ from lists in that they can not be modified, whereas lists can, and they use parentheses instead of square brackets. Though we can not change individual elements of a tuple, the del operator works with them. In this article, we are going to discuss the use of the del operator on a tuple. The del Operator with Tuples The del operator in Python is used to delete variables from the current scope. When you use "del" on a tuple ... Read More
In Python, the "*" operator can be used to repeat a tuple multiple times. This is known as tuple repetition. It creates a new tuple with repeated elements but does not modify the original tuple. Following are some points to remember - The repetition operator (*) creates a new tuple. Individual items are not repeated, only the entire tuple. The repeat count must be a non-negative integer. The original tuple is unaltered. Accepts tuples of any length and mixed data types. How Repetition Operator Works? When you apply '*' to a tuple with an integer, Python duplicates the ... Read More
The concatenation operator (+) is basically used to join two or more tuples together in Python. This operation returns a new tuple that contains all the items of the original tuples. In this article, we will discuss how the concatenation operator works on tuples in Python. But first, let us see how concatenation works. How Concatenation Works? When you use the concatenation operator on tuples, Python creates a new tuple that includes all the elements from the tuples being concatenated. The original tuples remain unchanged. As you may know, tuples in Python are immutable means their values cannot be changed ... Read More
Python offers the 'in' operator to verify that a value is present in a tuple. This operator is very useful when you are looking for items in a collection without requiring loops or complex logic. In this article, we will discuss the 'in' operator and how it works on tuples in Python. Before moving on, we will first discuss tuples. Tuples in Python are an immutable sequence, and they are created by placing a sequence of values separated by a 'comma', with or without the use of parentheses for data grouping. Tuples can have any number of elements and any ... Read More
Tuples in Python are immutable, which means once we create them, we can not change their items. In this article, we will discuss why tuples are immutable before moving on, and we will understand tuples in detail. Tuples in Python Tuples are a data type that belongs to the sequence data type category. They are similar to lists in Python, but they have the property of being immutable. We can't change the elements of a tuple, but we can perform operations suchas counting the number of elements, accessing elements using an index, retrieving the type of the elements, etc. Tuples ... Read More
Yes, it is recommended to define multiple Python classes in a single file. If we define one class per file, we may end up creating a large number of small files, which can be difficult to keep track of. Placing all the interrelated classes in a single file increases the readability of the code. If multiple classes are not interrelated, we can place them in different files (improves maintainability and scalability). What is a Python class? In Python, a class is a collection of objects. It is the blueprint from which objects are being created. It is a logical entity ... Read More