In this tutorial, we are going to learn how to use dictionary comprehensions in Python. If you are already familiar with list comprehension, then it won't take much time to learn dictionary comprehensions.We need the key: value pairs to create a dictionary. How to get these key-value pairs using dictionary comprehension? See the general statement of dictionary comprehension.{key: value for ___ in iterable}We need to fill in the above statement to complete a dictionary comprehension. There are many ways to fill it. Let's see some of the most common ways.Let's see how to generate numbers as keys and their squares ... Read More
In this tutorial, we are going to write a program that prints the name of the Python script file. We can find the script name using the sys module.The sys module will store all the command line arguments of python command in the sys.argv list. The first element in the list is the script name. We can extract it from that list. Python makes it easy.Let's see the steps involved in the program.Import the sys module.Now, print the first element of the sys.argv list.That's it. You got the script name.ExampleLet's see it practically. Live Demo# importing the sys module import sys ... Read More
In this tutorial, we are going to learn about the try and except of Python. Python has a concept called error and exception handling.The keywords try and except are used in the error and exception handling.Basically, we will find two types of errors in Python. They are −Syntax errors - Python gives these types of error when it doesn't understand a line of code in the program.Exception errors - Errors which are detected during the runtime of the program. Ex:- ZeroDivisionError, ValueError, etc.., We can't stop syntax errors. But, we can intimate if the program runs into an exception error ... Read More
List are the type of containers that stores data in sequential manner and allocate noncontiguous memory to the elements. In C++, lists are considered as doubly linked list where insertion and deletion of elements can be done from both the ends and therefore, it is also possible to traverse the list from both the end. For using singly linked list we use the forward list available in C++ STL.Advantage of using List over vector is thatList are faster in insertion and deletion of elements available in list container if the iterator is positioned to the correct element.Disadvantage of using List ... Read More
In this tutorial, we are going to learn about Unit Testing using the unittest built-in module. Testing plays a major role in software development. You will know the issues before going to the production itself.We'll learn the basics of testing in Python using the built-in module called unittest. Let's jump into the tutorial.What is Unit Testing?If you take the login system as an example. Each field in the login form is a unit/component. And testing those units/components functionality is known as Unit Testing.ExampleLet's see the basic structure of unittest framework. Live Demo# importing unittest module import unittest # unittest will test ... Read More
Functions are like a machine as they perform some functionality and produces a result of some type. Like, machine takes some input, process that input and produce an output similarly, function takes some value, operates on those value and produces the output. Manually a person passes the input to the machine then only the machine will start its functionality in the same manner when the programmer calls the function it will start executing.Functions can be different in name in various languages, but they share two common characteristics like −They contain sequence of instructions that needs to be processedThose instructions are ... Read More
In C++ STL, stack is used as container which is implemented as LIFO structure. LIFO means Last In First Out. Stack can view as a pile of books in which the books are arranged one above the another and the last inserted book will be the first one to be removed that’s why it is called as a LIFO structure.The operations associated with the stack are -Top() - This function returns the reference to the topmost element of a stack.Syntax - name_of_stack.top()Parameters - No ParameterReturn Value - Reference to the topmost element of a stack containerPush() - This function is ... Read More
The Boolean.Parse() method in C# is used to convert the specified string representation of a logical value to its Boolean equivalent.SyntaxFollowing is the syntax −public static bool Parse (string val);Above, the parameter value is a string containing the value to convert.ExampleLet us now see an example to implement the Boolean.Parse() method −using System; public class Demo { public static void Main(){ bool b; b = bool.Parse("FALSE"); Console.WriteLine("After parsing = "+b); } }OutputThis will produce the following output −After parsing = FalseExampleLet us see another example −using System; public class ... Read More
A standard API has been provided in Java 9 using java.lang.StackWalker class. This class designed to be efficient by allowing lazy access to the stack frames. A couple of other options allow in a stack trace that includes implementation and/or reflection frames, and it can be useful for debugging purposes. For instance, we add SHOW_REFLECT_FRAMES option to StackWalker instance upon creation, so that frames for reflective methods are printed as well.In the below example, we can able to show reflection frames of StackFrameExampleimport java.lang.StackWalker.Option; import java.lang.StackWalker.StackFrame; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import java.util.stream.Collectors; public class ReflectionFrameTest { public static ... Read More
StackWalker API has been introduced in Java 9, and it gives a snapshot of the stack trace of current thread at any given point of time and has methods to walk over it. The advantage of using StackWalker class over Thread::getStackTrace() is to filter or skip certain classes and get the instance of declaring the class itself and get either short stack trace or full stack trace instead of pulling the complete stack trace itself.In the below example, we can use java.util.stream.Stream.skip() method to skip Stack Frames.Exampleimport java.lang.StackWalker.*; import java.util.Optional; import java.util.List; import java.util.stream.Collectors; import java.lang.StackWalker.StackFrame; public class StackWalkerSkipTest { public ... Read More