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
Python Articles
Page 821 of 855
Fast XML parsing using Expat in Python
Python's xml.parsers.expat module provides fast XML parsing using the Expat library. It is a non-validating XML parser that creates an XML parser object and captures XML elements through various handler functions. This event-driven approach is memory-efficient and suitable for processing large XML files. How Expat Parser Works The Expat parser uses three main handler functions ? StartElementHandler − Called when an opening tag is encountered EndElementHandler − Called when a closing tag is encountered CharacterDataHandler − Called when character data between tags is found Example Here's how to parse XML data using Expat ...
Read MoreWindows registry access using Python (winreg)
Python provides excellent support for OS-level programming through its extensive module library. The winreg module allows Python programs to access and manipulate the Windows registry, which stores configuration settings and system information. The Windows registry is organized in a hierarchical structure with keys and values. Python's winreg module provides functions to connect to, read from, and write to registry keys. Basic Registry Access First, import the winreg module and establish a connection to the registry ? import winreg # Connect to the registry access_registry = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) # Open a specific key access_key ...
Read MorePython - Filter the negative values from given dictionary
As part of data analysis, we often encounter scenarios where we need to filter out negative values from a dictionary. Python provides several approaches to accomplish this task efficiently. Below are two common methods using different programming constructs. Using Dictionary Comprehension Dictionary comprehension provides a clean and readable way to filter negative values. We iterate through each key-value pair and include only those with non-negative values ≥ Example dict_1 = {'x': 10, 'y': 20, 'z': -30, 'p': -0.5, 'q': 50} print("Given Dictionary:", dict_1) filtered_dict = {key: value for key, value in dict_1.items() if ...
Read MorePython - Filter even values from a list
As part of data analysis, we often need to filter values from a list meeting certain criteria. In this article, we'll see how to filter out only the even values from a list. To identify even numbers, we check if a number is divisible by 2 (remainder is zero when divided by 2). Python provides several approaches to filter even values from a list. Using for Loop This is the simplest way to iterate through each element and check for divisibility by 2 ? numbers = [33, 35, 36, 39, 40, 42] even_numbers = ...
Read MoreAll possible permutations of N lists in Python
When we have multiple lists and need to combine each element from one list with each element from another list, we're creating what's called a Cartesian product (not permutations). Python provides several approaches to achieve this combination. Using List Comprehension This straightforward approach uses nested list comprehension to create all possible combinations. The outer loop iterates through the first list, while the inner loop iterates through the second list ? Example A = [5, 8] B = [10, 15, 20] print("The given lists:", A, B) combinations = [[m, n] for m in A for ...
Read MorePython - Column summation of tuples
Python has extensive availability of various libraries and functions which make it so popular for data analysis. We may get a need to sum the values in a single column for a group of tuples for our analysis. So in this program we are adding all the values present at the same position or same column in a series of tuples. Column summation involves adding corresponding elements from tuples at the same positions. For example, if we have tuples (3, 92) and (25, 62), the column summation would be (28, 154). Using List Comprehension and zip() Using ...
Read Moremax() and min() in Python
Finding maximum and minimum values from a given list of values is a very common need in data processing programs. Python provides built-in max() and min() functions that handle both numbers and strings efficiently. Syntax max(iterable, *[, key, default]) min(iterable, *[, key, default]) Using max() and min() with Numeric Values Both functions work with integers, floats, and mixed numeric types to find the maximum and minimum values ? numbers = [10, 15, 25.5, 3, 2, 9/5, 40, 70] print("Maximum number is:", max(numbers)) print("Minimum number is:", min(numbers)) Maximum number is: ...
Read MoreGetter and Setter in Python
In Python, getters and setters are methods used to control access to an object's attributes. They provide a way to implement data encapsulation, ensuring that attributes are accessed and modified in a controlled manner. Getters retrieve the value of an attribute, while setters allow you to modify it. This approach helps prevent direct manipulation of attributes from outside the class. Basic Class with Public Attributes Let's start with a simple class that has a public attribute − class YearGraduated: def __init__(self, year=0): self.year ...
Read MoreFractal Trees in Python
Fractal patterns are everywhere in nature − a small branch resembles the entire tree, a fern leaf's part looks like the whole leaf. This self-repeating pattern concept is called a fractal tree. Python provides several modules to generate beautiful fractal trees programmatically. What is a Fractal Tree? A fractal tree is a mathematical structure that exhibits self-similarity at different scales. Each branch splits into smaller branches following the same pattern, creating a recursive tree-like structure. The recursion continues until reaching a specified depth. Using pygame Module The pygame module provides graphics functions to draw fractal trees. ...
Read MoreAppend Odd element twice in Python
Sometimes we need to process a list where odd numbers appear twice while even numbers remain unchanged. This is useful in data processing scenarios where odd values need special emphasis or duplication. Using List Comprehension The most concise approach uses list comprehension with conditional repetition ? numbers = [2, 11, 5, 24, 5] # Repeat odd numbers twice, keep even numbers once result = [value for num in numbers for value in ([num] if num % 2 == 0 else [num, num])] print("Original list:", numbers) print("After processing:", result) Original list: [2, ...
Read More