Programming Articles - Page 2499 of 3366

Python Increment and Decrement Operators

Pavitra
Updated on 07-Aug-2019 07:21:40

842 Views

In this article, we will learn about increment and decrement operators in Python 3.x. Or earlier. In other languages we have pre and post increment and decrement (++ --) operators.In Python we don’t have any such operators . But we can implement these operators in the form as dicussed in example below.Examplex=786 x=x+1 print(x) x+=1 print(x) x=x-1 print(x) x-=1 print(x)Output787 788 787 786Other languages have for loops which uses increment and decrement operators. Python offers for loop which has a range function having default increment value “1” set. We can also specify our increment count as ... Read More

Absolute Deviation and Absolute Mean Deviation using NumPy

Pradeep Elance
Updated on 03-Jul-2020 07:47:01

1K+ Views

In Statistical analysis study of data variability in a sample indicates how dispersed are the values in a given data sample. The two important ways we calculate the variability are Absolute Deviation and  Mean Absolute Deviation.Absolute DeviationIn this method we first find the mean value of the given sample and then calculate the difference between each value and the mean value of the sample called as the absolute deviation value of each data sample. So for the values higher than mean the value the deviation value will be positive and for those lower than the mean value the deviation value ... Read More

Inbuilt Data Structures in Python

Pavitra
Updated on 07-Aug-2019 07:18:26

1K+ Views

In this article, we will learn about the 4 inbuilt data structures in python namely Lists, Dictionaries, Tuples & Sets.ListsA list is an ordered sequence of elements. It is a non-scalar data structure and mutable in nature. A list can contain distinct data types in contrast to arrays storing elements belonging to the same data types.Lists can be accessed by the help of the index by enclosing index within square brackets.Now let’s see an illustration to understand list better.Examplelis=['tutorialspoint', 786, 34.56, 2+3j] # displaying element of list for i in lis:    print(i) # adding an elements at end of ... Read More

A += B Assignment Riddle in Python

Pradeep Elance
Updated on 03-Jul-2020 07:49:19

143 Views

In this chapter we see what happens when we update the values in a tuple, which is actually immutable. We will be able to merge new values with old values but that throws an error. We can study the bytecode of the error and understand better how the rules for tuple work.First we define a tuple and then issue a command to update its last element as shown below.Example>>> tupl = (5, 7, 9, [1, 4]) >>> tupl[3] += [6, 8]OutputRunning the above code gives us the following result −Traceback (most recent call last): File "", line 1, in TypeError: ... Read More

id() function in Python

Pavitra
Updated on 07-Aug-2019 07:05:39

201 Views

In this article, we will learn about the usage and implementation of id() function in Python 3.x. Or earlier. It is present in Python Standard Library and is automatically imported before executing the code.Syntax: id ()Return value: Identity value of type The function accepts exactly one argument i.e. the name of the entity whose id has to be used. This id is unique for every entity until they are referring to the same data.Id’s are merely address in the memory locations and is used internally in Python.Example Codestr_1 = "Tutorials" print(id(str_1)) str_2 = "Tutorials" print(id(str_2)) # This will return True ... Read More

How can we convert character array to a Reader in Java?

Vivek Verma
Updated on 28-Aug-2025 16:19:10

439 Views

Converting a character array to a reader in Java means treating the array as a stream of characters to be read sequentially. Let's quickly learn about character array and its creation in Java: In Java, a character array is an array that holds elements of only char type, and it is also used to store a sequence of characters. Following is the syntax to define a character array in Java: char char_array_name[] = {'char1', 'char2', 'char3'....} or char char_array_name[] = new char[size]; Here, char_array_name: The name of the character array. ... Read More

Can a method throw java.lang.Exception without declaring it in java?

Maruthi Krishna
Updated on 06-Aug-2019 11:39:12

244 Views

No, for that matter to throw any exception explicitly you need to create an object of that exception and throw it using the throw keyword.Without creating an object you cannot throw an exception explicitly, you might create a scenario that causes the respective exception.ExampleFollowing Java program throws a NullPointerExceptionpublic class ExceptionExample {    public static void main(String[] args) {       System.out.println("Hello");       NullPointerException nullPointer = new NullPointerException();       throw nullPointer;    } }OutputHello Exception in thread "main" java.lang.NullPointerException    at MyPackage.ExceptionExample.main(ExceptionExample.java:6)

How can I add condition in custom exceptions in java?

Maruthi Krishna
Updated on 06-Aug-2019 11:37:29

736 Views

While reading values from the user in the constructor or any method you can validate those values using if condition.ExampleIn the following Java example we are defining two custom exception classes verifying name and age.import java.util.Scanner; class NotProperAgeException extends Throwable{    NotProperAgeException(String msg){       super(msg);    } } class NotProperNameException extends Throwable{    NotProperNameException(String msg){       super(msg);    } } public class CustomException{    private String name;    private int age;    public static boolean containsAlphabet(String name) {       for (int i = 0; i < name.length(); i++) {          char ch = name.charAt(i);          if (!(ch >= 'a' && ch

How to loop the program after an exception is thrown in java?

Maruthi Krishna
Updated on 06-Aug-2019 11:33:47

3K+ Views

Read the inputs and perform the required calculations within a method. Keep the code that causes exception in try block and catch all the possible exceptions in catch block(s). In each catch block display the respective message and call the method again.ExampleIn the following example we have an array with 5 elements, we are accepting two integers from user representing positions in the array and performing division operation with them, if the integer entered representing the positions is more than 5 (length of the exception) an ArrayIndexOutOfBoundsException occurs and, if the position chosen for denominator is 4 which is 0 ... Read More

How to print custom message instead of ErrorStackTrace in java?

Maruthi Krishna
Updated on 02-Jul-2020 15:03:02

4K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Printing the Exception messageYou can print the exception message in Java using one of the following methods which are inherited from Throwable class.printStackTrace() − This method prints the backtrace to the standard error stream.getMessage() − This method returns the detail message string of the current throwable object.toString() − This message prints the short description of the current throwable object.Exampleimport java.util.Scanner; public class PrintingExceptionMessage { ... Read More

Advertisements