Found 10476 Articles for Python

How can I preserve Python tuples with JSON?

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

405 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?

Disha Verma
Updated on 20-May-2025 14:52:06

1K+ Views

In Python, a list is a mutable data type used to store a collection of items, which are separated by commas and enclosed within square brackets [ ]. According to the Python documentation, there is no concept of a homogeneous list in Python. However, a Python list can contain collections of homogeneous items, meaning that the data types of the items are the same. Python List of Homogeneous Data A Python list can contain both homogeneous and heterogeneous data. Example  Here is an example of a list containing homogeneous data - # List containing ... Read More

How can I convert bytes to a Python string?

Vikram Chiluka
Updated on 01-Sep-2025 14:48:51

11K+ Views

In this article, we will show you how to convert bytes into a string in Python. Strings are sequences of characters, while bytes are 8-bit values. To convert a sequence of characters to 8-bit values, we can utilize Python's built-in methods, which we will explore in this article. Converting Bytes to Strings in Python The following methods can be efficiently used to convert bytes to Python string. Using decode() function Using str() function Using codecs.decode() function Using pandas library ... Read More

Using HTML5 Server-Sent Events in a web application

Jennifer Nicholas
Updated on 20-Dec-2019 10:49:19

244 Views

To use Server-Sent Events in a web application, you would need to add an element to the document.The src attribute of the element should point to an URL that should provide a persistent HTTP connection that sends a data stream containing the events.The URL would point to a PHP, PERL or any Python script that would take care of sending event data consistently.ExampleHere is an example showing application that would expect server time.                    /* Define event handling logic here */                                                              

Where are operators mapped to magic methods in Python?

Vikram Chiluka
Updated on 09-Nov-2022 08:05:07

476 Views

In this article, we will explain to you where are operators mapped to magic methods in python. Python Magic methods are special methods that begin and end with double underscores. They are also known as dunder methods. Magic methods are not intended to be invoked directly by you, but rather invocation occurs by the class on a specific action. When you use the + operator to add two numbers, the __add__() method is called internally. Many magic methods in Python are defined by built−in classes. To get the number of magic methods inherited by a class, use the dir() function. ... Read More

How will you explain Python Operator Overloading?

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

279 Views

Every class in Python, whether built-in or user defined is inherited from object class. The object class has a number of properties whose name is preceded and followed by double underscores (__). Each of these properties is a wrapper around a method of same name. Such methods are called special or magic methods.The magic methods __lt__(), __gt__(), __eq__(), __ne__(), etc. are overridden in a class to overload == and != operators respectively.

How do I find the largest integer less than x in Python?

Jayashree
Updated on 27-Oct-2022 12:23:22

3K+ Views

In this article, we will show you how to find the largest integer less than x in python. The Greatest Integer Function [X] denotes an integral part of the real number x that is the closest and smallest integer to x. It's also called the X-floor. [x]=the largest integer less than or equal to x. Generally If n

How to check whether a number is prime or not using Python?

Jayashree
Updated on 02-Mar-2020 10:06:49

859 Views

Principle used in following solution to this problem is to divide given number with all from 3 its square root, a number's square root is largest possible factor beyond which it is not necessary to check if it is divisible by any other number to decide that it is prime number.The function returns false for all numbers divisible by 2 and less than 2. For others return value of all) function will be false if it is divisible by any number upto its square root and true if it is not divisible by any numberExampledef is_prime(a):     if a ... Read More

How to randomize the items of a list in Python?

Jayashree
Updated on 24-Jun-2020 06:59:23

1K+ Views

The random module in the Python standard library provides a shuffle() function that returns a sequence with its elements randomly placed.>>> import random >>> l1=['aa',22,'ff',15,90,5.55] >>> random.shuffle(l1) >>> l1 [22, 15, 90, 5.55, 'ff', 'aa'] >>> random.shuffle(l1) >>> l1 ['aa', 'ff', 90, 22, 5.55, 15]

How to randomly select element from range in Python?

Vikram Chiluka
Updated on 09-Nov-2022 06:54:31

6K+ Views

In this article, we will show how to randomly select from a range in python. Below are the various methods to accomplish this task − Using random.randrange() function Using random.randint() function Using random.random() function Using random.sample() Using random.randrange() function Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Use the import keyword, to import the random module. Use the random.randrange() function(Returns a random number within the specified range) to generate a random number within the given range by passing minimum, and maximum numbers as arguments. Generate a random number from ... Read More

Advertisements