Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
How can I convert a Python tuple to string?
A tuple is a collection of objects that is ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are that tuples cannot be changed, unlike lists, and tuples use parentheses, whereas lists use square brackets. Converting a Python tuple to a String There are three different ways we can convert a Python tuple to a string: Using a for loop ...
Read MoreWhat is the difference between dict.items() and dict.iteritems() in Python?
In Python, dict.items() and dict.iteritems() are methods used to access dictionary key-value pairs. The key difference is that dict.items() returns a list of tuple pairs in Python 2 (dict_items view in Python 3), while dict.iteritems() returns an iterator over the dictionary's (key, value) pairs. Note that dict.iteritems() was removed in Python 3. dict.items() in Python 2 In Python 2, dict.items() returns a list of tuples ? # Python 2 syntax (cannot run online) my_dict = {1: 'one', 2: 'two', 3: 'three', 4: 'four'} print(my_dict.items()) print(type(my_dict.items())) [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] ...
Read MoreHow can I iterate through two lists in parallel in Python?
In Python, iterating through two or more lists in parallel is a common task. Python provides several methods to achieve this, each with different behaviors for handling lists of unequal lengths. Using range() with Index-Based Access The most basic approach uses range() with the len() function to iterate through both lists using indices ? Example When both lists have the same length ? letters = ['a', 'b', 'c', 'd', 'e'] numbers = [97, 98, 99, 100, 101] length = len(letters) # Assuming both lists have same length for i in range(length): ...
Read MoreHow do I check what version of Python is running my script?
Python is being updated regularly with new features and support. Starting from 1994 to the current release, there have been lots of updates in Python versions. Using Python standard libraries like sys or platform modules, we can get the version information of Python that is actually running on our script. In general, the Python version is displayed automatically on the console immediately after starting the interpreter from the command line ? Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. ...
Read MoreDo you think garbage collector can track all the Python objects?
The garbage collector in Python can track most objects, but it focuses specifically on unreachable objects (reference count of zero) and objects involved in circular references. Understanding when and how garbage collection works is crucial for memory management. What is a Garbage Collector? The garbage collector is an automatic process that handles memory allocation and deallocation, ensuring efficient memory usage. Python uses reference counting as its primary memory management mechanism, with garbage collection as a backup for special cases. We can interact with the garbage collector explicitly using the gc module. By default, it is enabled, but ...
Read MoreIs there any Python object inspector?
Python doesn't have a built-in object inspector, but it provides several powerful functions and modules for examining objects. Functions like type(), help(), dir(), vars(), and the inspect module help you discover attributes, properties, and methods of any object. Additional functions like id(), getattr(), hasattr(), globals(), locals(), and callable() are useful for examining object internals. Let's explore these inspection techniques using a simple class example. Sample Class for Demonstration First, let's create a Height class that we'll use throughout our examples ? class Height: """ A height class ...
Read MoreHow to encode custom python objects as BSON with Pymongo?
To encode custom Python objects as BSON with PyMongo, you need to write a SONManipulator. SONManipulator instances allow you to specify transformations to be applied automatically by PyMongo when storing and retrieving data. Creating a Custom SONManipulator Here's how to create a SONManipulator that handles custom object encoding and decoding ? from pymongo.son_manipulator import SONManipulator class Transform(SONManipulator): def transform_incoming(self, son, collection): for (key, value) in son.items(): if isinstance(value, Custom): ...
Read MoreHow to compress Python objects before saving to cache?
Sometimes we need to compress Python objects (lists, dictionaries, strings, etc.) before saving them to cache and decompress them after reading from cache. This is particularly useful when dealing with large data structures that consume significant memory. Before implementing compression, we should evaluate whether it's actually needed. Check if the data structures are too large to fit uncompressed in the cache. There's an overhead for compression/decompression that we need to balance against the benefits of caching. Using zlib for Compression If compression is necessary, zlib is the most commonly used library. It provides efficient compression with adjustable ...
Read MoreHow to get the return value from a function in a class in Python?
The following code shows how to get return value from a function in a Python class. When a method in a class uses the return statement, you can capture and use that value by calling the method on a class instance. Basic Example Here's a simple class that demonstrates returning values from methods ? class Score(): def __init__(self): self.score = 0 self.num_enemies = 5 self.num_lives = 3 ...
Read MoreHow to return an object from a function in Python?
In Python, functions can return objects of any type using the return keyword. This includes simple values, complex data structures, and custom objects. The statements after the return will not be executed. The return keyword cannot be used outside a function. If a function has a return statement without any expression, the special value None is returned. Returning Simple Values Here's a basic example of returning a calculated value ? def sum_numbers(a, b): return a + b my_var1 = 23 my_var2 = 105 result = sum_numbers(my_var1, my_var2) print(result) ...
Read More