Found 34494 Articles for Programming

Display four-digit year in Java

karthikeya Boyini
Updated on 27-Jun-2020 14:52:32

610 Views

Use the ‘Y’ date conversion character to display four-digit year.System.out.printf("Four-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);    } }OutputCurrent date and time = 26/11/2018 11:56:26 AM Four-digit Year = 2018

Display first two digits of year in Java (two-digit century)

Samual Sam
Updated on 27-Jun-2020 14:53:05

356 Views

Use the ‘C’ date conversion character to display two digits of year −System.out.printf("Two-digit Year (Century Name) = %tC/%TC", 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("Localized day name = %tA/%TA", d, d); ... Read More

Generate pseudo-random numbers in Python

Nancy Den
Updated on 27-Jun-2020 14:59:02

3K+ Views

Many computer applications need random number to be generated. However, none of them generate a truly random number. Python, like any other programming technique, uses a pseudo-random generator. Python’s random generation is based upon Mersenne Twister algorithm that produces 53-bit precision floats. The technique is fast and thread-safe but not suitable from cryptographic purpose.Python’s standard library contains random module which defines various functions for handling randomization.random.seed() − This function initializes the random number generator. When random module is imported, the generator is initialized with the help of system time. To reseed the generator, use any int, str, byte or bytearray ... Read More

pprint module (Data pretty printer)

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

1K+ Views

The pprint module (lib/pprint.py) is a part of Python’s standard library which is distributed along with standard Python distribution. The name pprint stands for pretty printer. The pprint module’s functionality enables aesthetically good looking appearance of Python data structures. Any data structure that can be correctly parsed by Python interpreter is elegantly formatted. The formatted expression is kept in one line as far as possible, but breaks into multiple lines if the length exceeds the width parameter of formatting. One unique feature of pprint output is that the dictionaries are automatically sorted before the display representation is formatted.The pprint module ... Read More

Python object serialization (Pickle)

Krantik Chavan
Updated on 27-Jun-2020 15:00:01

849 Views

The term object serialization refers to process of converting state of an object into byte stream. Once created, this byte stream can further be stored in a file or transmitted via sockets etc. On the other hand reconstructing the object from the byte stream is called deserialization.Python’s terminology for serialization and deserialization is pickling and unpickling respectively. The pickle module available in Python’s standard library provides functions for serialization (dump() and dumps()) and deserialization (load() and loads()).The pickle module uses very Python specific data format. Hence, programs not written in Python may not be able to deserialize the encoded (pickled) ... Read More

Python Standard operators as functions

Rishi Rathor
Updated on 27-Jun-2020 15:01:34

292 Views

In programming, operator is generally a symbol (key) predefined to perform a certain operation such as addition, subtraction, comparison etc. Python has a large set of built-in operations divided in different categories such as arithmetic, comparison, bit-wise, membership etc.The operator module in python library consists of functions corresponding to built-in operators. Names of the functions are analogous to type of corresponding operator. For example, add() function in operator module corresponds to + operator.Python’s Object class has dunder (double underscore before and after name) methods corresponding to operator symbols. These dunder methods can be suitably overloaded in user defined classes to ... Read More

Internal Python object serialization (marshal)

Nancy Den
Updated on 27-Jun-2020 15:01:53

869 Views

Even though marshal module in Python’s standard library provides object serialization features (similar to pickle module), it is not really useful for general purpose data persistence or transmission of Python objects through sockets etc. This module is mostly used by Python itself to support read/write operations on compiled versions of Python modules (.pyc files). The data format used by the marshal module is not compatible across Python versions (not even subversions). That’s why a compiled Python script (.pyc file) of one version most probably won’t execute on another. The marshal module is thus used for Python’s internal object serialization.Just as ... Read More

Python Functions creating iterators for efficient looping

Daniol Thomas
Updated on 30-Jul-2019 22:30:24

191 Views

As in most programming languages Python provides while and for statements to form a looping construct. The for statement is especially useful to traverse the iterables like list, tuple or string. More efficient and fast iteration tools are defined in itertools module of Python’s standard library. These iterator building blocks are Pythonic implementations of similar tools in functional programming languages such as Haskell and SML.Functions in itertools module are of three types.Infinite iteratorsFinite iteratorsCombinatoric iteratorsFollowing functions generate infinite sequences.count() − This function returns an iterator of evenly spaced values from start value. The function can have optional step value to ... Read More

Decimal fixed point and floating point arithmetic in Python

Krantik Chavan
Updated on 30-Jul-2019 22:30:24

1K+ Views

Floating point numbers are represented in the memory as a base 2 binary fraction. As a result floating point arithmetic operations can be weird at times. Addition of 0.1 and 0.2 can give annoying result as follows −>>> 0.1 + 0.2 0.30000000000000004In fact this is the nature of binary floating point representation. This is prevalent in any programming language. Python provides a decimal module to perform fast and correctly rounded floating-point arithmetic.The decimal module is designed to represent floating points exactly as one would like them to behave, and arithmetic operation results are consistent with expectations. The precision level of ... Read More

Get localized short day-in-week name in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

223 Views

Use the ‘a’ date conversion character to display short day-in-week.System.out.printf("Localized short day name = %ta/%Ta", 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 = " + ... Read More

Advertisements