Found 34490 Articles for Programming

How can I preserve Python tuples with JSON?

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

222 Views

There is no concept of a tuple in the JSON format. Python's JSON module converts Python tuples to JSON lists because that's the closest thing in JSON to a tuple. Immutability will not be preserved. If you want to preserve them, use a utility like a pickle or write your own encoders and decoders.If you're using pickle, it won't store the Python temples in JSON files but in pkl files. This isn't useful if you're sending data across the web. The best way is to use your own encoders and decoders that will differentiate between lists and tuples depending on ... Read More

What is the homogeneous list in Python list?

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

945 Views

There is nothing like homogenous list in Python. The python docs just suggest to use lists for homogenous data. Qouting the docsLists are mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application).You can very well use lists for heterogenous data as well.

How do we grep a particular keyword from Python tuple?

Lakshmi Srinivas
Updated on 05-Mar-2020 05:54:11

174 Views

If you have a tuple of strings and you want to search for a particular string, You can use the in operator. exampletpl = ("Hello", "world", "Foo", "bar") print("world" in tpl)OutputThis will give the output −TrueExampleIf you want to check if there is a substring present. You can loop over the tuple and find it using:tpl = ("Hello", "world", "Foo", "bar") for i in tpl:    if "orld" in i:       print("Found orld in " + i )OutputThis will give the output −Found orld in world

How can I convert a bytes array into JSON format in Python?

Arjun Thakur
Updated on 05-Mar-2020 05:44:42

19K+ Views

You need to decode the bytes object to produce a string. This can be done using the decode function from string class that will accept then encoding you want to decode with. examplemy_str = b"Hello" # b means its a byte string new_str = my_str.decode('utf-8') # Decode using the utf-8 encoding print(new_str)OutputThis will give the outputHelloOnce you have the bytes as a string, you can use the JSON.dumps method to convert the string object to JSON. examplemy_str = b'{"foo": 42}' # b means its a byte string new_str = my_str.decode('utf-8') # Decode using the utf-8 encoding import json d = json.dumps(my_str) ... Read More

Can you explain what is metaclass and inheritance in Python?

karthikeya Boyini
Updated on 17-Jun-2020 09:53:05

535 Views

Every class is an object. It's an instance of something called a metaclass. The default metaclass is typed. You can check this using the is instance function. For example,class Foo:    pass foo = Foo() isinstance(foo, Foo) isinstance(Foo, type)This will give the output:True TrueA metaclass is not part of an object's class hierarchy whereas base classes are. These classes are used to initialize the class and not its objects.You can read much more in-depth about Metaclasses and inheritance on https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/

How can I convert bytes to a Python string?

Vikram Chiluka
Updated on 28-Oct-2022 08:28:47

11K+ Views

In this article, we will show you how to convert bytes to a string in python. Below are the various methods to accomplish this task − Using decode() function Using str() function Using codecs.decode() function Using pandas library Using decode() function The built-in decode() method in python, is used to convert bytes to a string. Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task –. Create a variable to store the input byte string data. Print input data. Use the type() function(returns the data type of an object) to print the type ... Read More

How to assign multiple values to a same variable in Python?

Ankith Reddy
Updated on 05-Mar-2020 05:40:07

899 Views

In Python, if you try to do something likea = b = c = [0,3,5] a[0] = 10You'll end up with the same values ina, b, and c: [10, 3, 5]This is because all three variables here point to the same value. If you modify this value, you'll get the change reflected in all names, ie, a,b and c. To create a new object and assign it, you can use the copy module.  examplea = [0,3,5] import copy b = copy.deepcopy(a) a[0] = 5 print(a) print(b)OutputThis will give the output −[5,3,5] [0,3,5]

How do I assign a dictionary value to a variable in Python?

Lakshmi Srinivas
Updated on 05-Mar-2020 05:36:33

13K+ Views

You can assign a dictionary value to a variable in Python using the access operator []. examplemy_dict = {    'foo': 42,    'bar': 12.5 } new_var = my_dict['foo'] print(new_var)OutputThis will give the output −42exampleThis syntax can also be used to reassign the value associated with this key. my_dict = {    'foo': 42,    'bar': 12.5 } my_dict['foo'] = "Hello" print(my_dict['foo'])OutputThis will give the output −Hello

Why we can't use arrow operator in gets and puts?

Pythonista
Updated on 22-Jun-2020 09:11:02

104 Views

You can't read user input in an un-initialized pointer. Instead, have a variable of the struct data type and assign its address to pointer before accessing its inner elements by → operatorexample#include struct example{    char name[20]; }; main(){    struct example *ptr;    struct example e;    puts("enter name");    gets(e.name);    ptr=&e;    puts(ptr->name); }OutputTypical result of above codeenter name Disha You entered Disha

Java is also not pure object-oriented like c++

Pythonista
Updated on 30-Jul-2019 22:30:22

293 Views

the main() method in Java code is itself inside a class. The static keyword lets the main() method which is the entry point of execution without making an object but you need to write a class. In C++, main() is outside the class and writing class it self is not mandatory. Hence, C++ is not a pure object oriented language bu Java is a completely object oriented language.

Advertisements