- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10784 Articles for Python
220 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
940 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.
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
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
533 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/
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
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]
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
4K+ Views
You can combine multiple print statements per line using, in Python 2 and use the end argument to print function in Python 3.examplePython2.x print "Hello", print " world" Python3.x print ("Hello", end='') print (" world")OutputThis will give the output −Hello worldAnother thing you could do is put all the things in an array and call ''.join(array). examplearr = ["Hello", "world"] print(' '.join(arr))OutputThis will give the output −Hello world
502 Views
You can use the PEP8 guide as a holy grail. Almost all python world uses this guide to write clean understandable and standard python code. This is available as an extension as a linter for all modern text editors. You can check it out at http://www.python.org/dev/peps/pep-0008/Properly Structure your folders. All projects need proper structuring. This helps organize code better. Python has an opinionated folder structure that you should use.README.rst LICENSE setup.py requirements.txt sample/__init__.py sample/core.py sample/helpers.py docs/conf.py docs/index.rst tests/test_basic.py tests/test_advanced.pyUse doctests. The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to ... Read More