Found 34494 Articles for Programming

The Python Debugger (pdb)

Jennifer Nicholas
Updated on 27-Jun-2020 14:38:10

2K+ Views

In software development jargon, 'debugging' term is popularly used to process of locating and rectifying errors in a program. Python's standard library contains pdb module which is a set of utilities for debugging of Python programs.The debugging functionality is defined in a Pdb class. The module internally makes used of bdb and cmd modules.The pdb module has a very convenient command line interface. It is imported at the time of execution of Python script by using –m switchpython –m pdb script.pyIn order to find more about how the debugger works, let us first write a Python module (fact.py) as follows ... Read More

Measure execution time of small Python code snippets (timeit)

Nancy Den
Updated on 27-Jun-2020 14:38:42

169 Views

The Timer class and other convenience functions in timeit module of Python's standard library are designed to provide a mechanism to measure time taken by small bits of Python code to execute. The module has a command line interface and the functions can be called from within program as well.Easiest way to measure time of execution is by using following convenience functiontimeit()This function returns object of Timer class. It mainly requires two parameters.stmt − a string containing valid Python statement whose execution time is to be measured.setup − a string containing Python statement which will be executed once, primarily to ... Read More

Python Program Exit handlers (atexit)

Daniol Thomas
Updated on 27-Jun-2020 14:38:59

523 Views

The atexit module in the standard distribution of Python has two functions – register() and unregister(). Both functions take some existing function as an argument. Registered functions are executed automatically when the interpreter session is terminated normally.If more than one functions are registered, their execution is in the reverse order of registration. It means, functions f1(), f2() and f3() are registered one after other, their order of execution will be f3(), f2() and f1().The unregister() function removes the specified function from list of functions to be automatically invoked.Following code shows how a function is registered for automatic execution upon termination ... Read More

Java Program to list Weekday names

Samual Sam
Updated on 27-Jun-2020 14:46:33

2K+ Views

To list weekday names, use the getWeekdays () from the DateFormatSymbols class in Java.DateFormatSymbols is a class for encapsulating localizable date-time formatting data.Get weekday month names in an array −String[] days = new DateFormatSymbols().getWeekdays ();Display the weekdays −for (int i = 0; i < days.length; i++) {    String weekday = days[i];    System.out.println(weekday); }The following is an example −Example Live Demoimport java.text.DateFormatSymbols; public class Demo {    public static void main(String[] args) {       String[] days = new DateFormatSymbols().getWeekdays();       for (int i = 0; i < days.length; i++) {          String weekday ... Read More

Java program to reverse bits of a positive integer number

karthikeya Boyini
Updated on 27-Jun-2020 14:47:41

215 Views

The bits of an integer number can be reversed to obtain another number. An example of this is given as follows −Number = 11 Binary representation = 1011 Reversed binary representation = 1101 Reversed number = 13A program that demonstrates this is given as follows −Example Live Demopublic class Example {    public static void main(String[] args) {       int num = 14;       int n = num;       int rev = 0;       while (num > 0) {          rev = 1;       }       ... Read More

Java program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer

Samual Sam
Updated on 27-Jun-2020 14:48:28

267 Views

The length of the longest consecutive 1’s in the binary representation of a given integer involves the length of the longest series of 1’s that occur together. An example of this is given as follows −Number = 13 Binary representation = 1101The length of the longest consecutive 1’s in binary representation of 13 = 2A program that demonstrates this is given as follows −Example Live Demopublic class Example {    public static void main(String strings[]) {       int num = 55;       int n = num;       int count = 0;       while (num!=0) {          num = (num & (num

Display two-digit day of the month in Java

karthikeya Boyini
Updated on 27-Jun-2020 14:50:08

664 Views

Use the ‘d’ date conversion character to display two-digit day of the month, for example, 27, 28, 20, etc.System.out.printf("Two-digit day of the month: %td", d);Above, d is a date object −Date d = new Date();The following is an example −Example Live Demoimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year ... Read More

Display two-digit month in Java

Samual Sam
Updated on 27-Jun-2020 14:50:32

2K+ Views

Use the ‘m’ date conversion character to display two-digit month.System.out.printf("Two-digit month: %tm", d);Above, d is a date object −Date d = new Date();The following is an example −Example Live Demoimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year = %TY", d);       System.out.printf("Two-digit Year = %ty", d);   ... Read More

Display three-digit day of the year in Java

karthikeya Boyini
Updated on 27-Jun-2020 14:51:12

351 Views

Use the ‘j’ date conversion character to display three-digit day of the year.System.out.printf("Three-digit Day of the Year: %tj/%Tj", d, d);Above, d is a date object −Date d = new Date();The following is an example −Example Live Demoimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year = %TY", d);     ... Read More

Display two-digit year in Java

Samual Sam
Updated on 27-Jun-2020 14:51:59

1K+ Views

Use the ‘y’ date conversion character to display two-digit year.System.out.printf("Two-digit Year = %TY", d);Above, d is a date object −Date d = new Date();The following is an example −Example Live Demoimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year = %TY", d);       System.out.printf("Two-digit Year = %ty", d); ... Read More

Advertisements