Found 34490 Articles for Programming

How to create immutable class in Java?

Rishi Raj
Updated on 30-Jul-2019 22:30:23

503 Views

An immutable class object's properties cannot be modified after initialization. For example String is an immutable class in Java. We can create a immutable class by following the given rules below − Make class final − class should be final so that it cannot be extended. Make each field final − Each field should be final so that they cannot be modified after initialization. Create getter method for each field. − Create a public getter method for each field. fields should be private. No setter method for each field. − Do not create a public setter method for any ... Read More

How does “do something OR DIE()” work in Perl?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

3K+ Views

The die() function can be used to stop the script and can be used to display a message to the end user. It can be used at the right side of the OR ( || ) operator and at left side can be any expression like the eval() function. Case 1 − Using die() function with no parameter The following is the output. Now we can use $! to print the error number and a message to the user in the die() function. Case 2 − Use of $! in the die() function The following is the ... Read More

Keywords in Python

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

3K+ Views

Like other languages, Python also has some reserved words. These words hold some special meaning. Sometimes it may be a command, or a parameter etc. We cannot use keywords as variable names. The Python Keywords are True False class def return if elif else try except raise finally for in is not from import global lambda nonlocal pass while break continue and with as yield del or assert None The True & False Keywords The True and False are the truth ... Read More

Precision Handling in Python

Samual Sam
Updated on 30-Jul-2019 22:30:23

3K+ Views

Python can handle the precision of floating point numbers using different functions. Most functions for precision handling are defined in the math module. So to use them, at first we have to import the math module, into the current namespace. import math Now we will see some of the functions for precision handling. The trunc() function The trunc() method is used to remove all fractional part from a floating point number. So it returns only the integer part from the number. The ceil() function The ceil() method is used to return the Ceiling value of a ... Read More

Formatted text in Linux Terminal using Python

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

225 Views

In this section, we will see how to print formatted texts in Linux terminal. By formatting, we can change the text color, style, and some special features. Linux terminal supports some ANSI escape sequences to control the formatting, color and other features. So we have to embed some bytes with the text. So when the terminal is trying to interpret them, those formatting will be effective. The general syntax of ANSI escape sequence is like below − \x1b[A;B;C A is the Text Formatting Style B is the Text Color or Foreground Color C is the Background ... Read More

Reading and Writing to text files in Python

Samual Sam
Updated on 30-Jul-2019 22:30:23

1K+ Views

Like other languages, Python provides some inbuilt functions for reading, writing, or accessing files. Python can handle mainly two types of files. The normal text file and the binary files. For the text files, each lines are terminated with a special character '' (It is known as EOL or End Of Line). For the Binary file, there is no line ending character. It saves the data after converting the content into bit stream. In this section we will discuss about the text files. File Accessing Modes Sr.No Modes & Description 1 r It is Read ... Read More

Generate a graph using Dictionary in Python

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

2K+ Views

The graphs can be implemented using Dictionary in Python. In the dictionary, each key will be the vertices, and as value, it holds a list of connected vertices. So the entire structure will look like Adjacency list of a graph G(V, E). We can use the basic dictionary object, but we are using default dict. It has some additional features. It has one additional writable instance variable. We are providing a text file, which contains the number of vertices, number of edges, names of vertices, and the list of edges. For undirected graph, we are providing two edges like ... Read More

Python program to find common elements in three lists using sets

AmitDiwan
Updated on 12-Aug-2022 11:44:01

1K+ Views

In this article, we will learn how to find common elements in three lists. The list is the most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type. However, Set is a collection in Python, which is unordered, unchangeable, and unindexed. Let’s say we have the following input − a = [5, 10, 15, 20, 25] b = [2, 5, 6, 7, 10, 15, 18, 20] c = [10, 20, 30, ... Read More

Break a list into chunks of size N in Python

AmitDiwan
Updated on 12-Aug-2022 11:53:11

427 Views

In this example, we will learn how to break a list into chunks of size N. We will be using the list() function here. The list() function creates a list object. A list object is a collection which is ordered and changeable. Break a list into chunks of size N with List Comprehension The List Comprehension can be used to breal a list into chunks of size N − Example A = list() # User input for list size n = int(input("Enter the size of the List")) # User input for number print("Enter the number") for i in range(int(n)): ... Read More

Three way partitioning of an array around a given range using Python

AmitDiwan
Updated on 12-Aug-2022 14:36:50

252 Views

Given an array and the range of the array [startval, endval]. Array is divided by three parts. All elements smaller than startval come first. All elements in range startval to endval come next. All elements greater than endval appear in the end. Let’s say we have the following input − A = [1, 14, 51, 12, 4, 2, 54, 20, 87, 98, 3, 1, 32] startval = 14, endval = 54 The output should be − A = [1, 12, 4, 2, 3, 1, 14, 51, 20, 32, 54, 87, 98] Three-way partitioning of an array around ... Read More

Advertisements