Programming Articles - Page 2385 of 3363

Comprehensions in Python

Pradeep Elance
Updated on 17-Oct-2019 12:10:37

4K+ Views

We can create new sequences using a given python sequence. This is called comprehension. It basically a way of writing a concise code block to generate a sequence which can be a list, dictionary, set or a generator by using another sequence. It may involve multiple steps of conversion between different types of sequences.List ComprehensionIn this method, we create a new list by manipulating the values of an existing list. In the below example we take a list and create a new list by adding 3 to each element of the given list.Examplegiven_list = [x for x in range(5)] print(given_list) ... Read More

colorsys module in Python

Pradeep Elance
Updated on 17-Oct-2019 12:07:25

1K+ Views

This module allows bidirectional conversions of color values between colors expressed in the RGB (Red Green Blue) and other color spaces. The three other color spaces it uses are YIQ (Luminance (Y) In-phase Quadrature), HLS (Hue Lightness Saturation) and HSV (Hue Saturation Value). All the coordinates can be between 0 and 1 except the I and Q values in the YIQ color space.The below tables shows the functions and their purpose.FunctionPurposePermitted Valuesrgb_to_yiqfrom RGB coordinates to YIQ coordinates0 to 1rgb_to_hlsfrom RGB coordinates to HLS coordinates0 to 1rgb_to_hsvfrom RGB coordinates to HSV coordinates0 to 1yiq_to_rgbfrom YIQ coordinates to RGB coordinates-1 to 1hls_to_rgbfrom ... Read More

classmethod() in Python

Pradeep Elance
Updated on 17-Oct-2019 12:03:20

498 Views

A class method receives the class itself as its first argument. This way we are able to call the method inside a class without first creating an instance from the class. We just use the decorator @classmethod before the declaration of the method contained in the class and then we can directly access the method. Below are the main features of classmethids.A classmethod is bound to a class and does not depend on the instantiation of a class to be used.A classmethod can modify a class which in turn propagates to all instances of the class.Using the classmethodIn the below ... Read More

Class & Instance Attributes in Python

Harshit Sachan
Updated on 13-Oct-2022 11:52:51

2K+ Views

In Python Programming Language, Class and Instance are two of object orientation's most crucial ideas. Instances are unique objects made in accordance with the class, whereas classes are templates. The procedure is the same for all objects, although the data may vary. In this tutorial, we will learn about classes in python, how to instantiate them, what attributes are, and the differences between class and instance attribute in Python. Let’s begin with definitions – What is a Class? Classes provide a means of bundling data and functionality together in python. Creating a new class creates a new type of ... Read More

Check if both halves of the string have same set of characters in Python

Niharikaa Aitam
Updated on 20-Jun-2025 19:29:52

355 Views

In Python, a string is one of the data structures that is a sequence of characters enclosed within single quotes '' or double quotes "". It is immutable, i.e., once a string is created, it cannot be changed. When we want to check if both halves of the string have the same set of characters in Python, we can follow the steps below - First, based on the length of the string, we have to split the string into two halves. Next, convert each half into a set of characters using set() function. Finally, compare the two sets using ... Read More

Check for balanced parentheses in Python

Akshitha Mote
Updated on 22-Jan-2025 13:33:51

1K+ Views

In this article, we will solve the problem of checking balanced parentheses. Let's understand the problem statement, The following are the conditions for balanced parentheses − Every opening parenthesis has a corresponding closing parentheses. Parentheses should be closed in the correct order. For example, "{[()]}" is a balanced parenthesis. condiser the same example with different arrangment "{([})]" which is unbalanced parantheses. Checking parentheses Using 'list' and 'for' loop list is one of the built-in data types in Python. A Python list is a sequence of comma-separated items, enclosed ... Read More

How to add a JSON string to an existing JSON file in Java?

raja
Updated on 06-Jul-2020 13:21:04

6K+ Views

A Gson is a json library for Java and it can be used to generate a JSON. In the initial step, we can read a JSON file and parsing to a Java object then need to typecast the Java object to a JSonObject and parsing to a JsonArray. Then iterating this JSON array to print the JsonElement. We can create a JsonWriter class to write a JSON encoded value to a stream, one token at a time. Finally, a new JSON string can be written to an existing json file. Exampleimport java.io.*; import java.util.*; import com.google.gson.*; import com.google.gson.stream.*; import com.google.gson.annotations.*; public class JSONFilewriteTest {   ... Read More

When to use @JsonAutoDetect annotation in Java?

raja
Updated on 06-Jul-2020 13:16:17

6K+ Views

The @JsonAutoDetect annotation can be used at the class level to override the visibility of the properties of a class during serialization and deserialization. We can set the visibility with the properties like "creatorVisibility", "fieldVisibility", "getterVisibility", "setterVisibility" and "isGetterVisibility". The JsonAutoDetect class can define public static constants that are similar to Java class visibility levels like "ANY", "DEFAULT", "NON_PRIVATE", "NONE", "PROTECTED_AND_PRIVATE" and "PUBLIC_ONLY".Exampleimport com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class JsonAutoDetectTest {    public static void main(String[] args) throws IOException {       Address address = new Address("Madhapur", "Hyderabad", "Telangana");       Name name = new Name("Raja", "Ramesh");       Student student ... Read More

How to rename the properties of JSON using Gson in Java?

raja
Updated on 06-Jul-2020 13:17:10

4K+ Views

The Gson @SerializedName annotation can be serialized to a JSON with the provided name value as its field name. This annotation can override any FieldNamingPolicy including the default field naming policy that may have been set on the Gson instance. A different naming policy can set using the GsonBuilder class.Syntax@Retention(value=RUNTIME) @Target(value={FIELD, METHOD}) public @interface SerializedNameExampleimport com.google.gson.annotations.*; import com.google.gson.*; public class SerializedNameAnnotationTest {    public static void main(String args[]) {       Employee emp = new Employee("Rahul", "Dev", 30, "Nagpur");       Gson gson = new GsonBuilder().setPrettyPrinting().create(); // pretty print       String jsonStr = gson.toJson(emp);       System.out.println(jsonStr);    } ... Read More

How to ignore the multiple properties of a JSON object in Java?

raja
Updated on 06-Jul-2020 13:18:16

4K+ Views

The @JsonIgnoreProperties Jackson annotation can be used to specify a list of properties or fields of a class to ignore. The @JsonIgnoreProperties annotation can be placed above the class declaration instead of above the individual properties or fields to ignore.Syntax@Target(value={ANNOTATION_TYPE, TYPE, METHOD, CONSTRUCTOR, FIELD}) @Retention(value=RUNTIME) public @interface JsonIgnorePropertiesExampleimport java.io.*; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; public class JsonIgnorePropertiesTest {    public static void main(String[] args) throws IOException {       Customer customer = new Customer("120", "Ravi", "Hyderabad");       System.out.println(customer);       ObjectMapper mapper = new ObjectMapper();       String jsonString = mapper.writeValueAsString(customer);       System.out.println("JSON: " + jsonString);     ... Read More

Advertisements