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
Server Side Programming Articles
Page 875 of 2109
Python Generate QR Code using pyqrcode module?
A QR code consists of black squares arranged in a square grid on a white background, which can be read by an imaging device such as a camera. It is widely used for commercial tracking applications, payment systems, and website login authentication. The pyqrcode module is used to generate QR codes in Python. There are four standardized encoding modes: numeric, alphanumeric, byte/binary, and kanji to store data efficiently. Installation First, install the pyqrcode module using pip ? pip install pyqrcode Basic QR Code Generation We use the pyqrcode.create() function to generate a QR ...
Read MorePython Front and rear range deletion in a list?
Sometimes we need to remove elements from both the beginning and end of a list simultaneously. Python provides several approaches to delete elements from both the front and rear of a list efficiently. Using List Slicing This approach creates a new list by slicing elements from both ends. The original list remains unchanged ? days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] # Given list print("Given list:", days) # Number of elements to delete from front and rear v = 2 # Create new list excluding v elements from each end new_list ...
Read MorePython file parameter in print()?
The print() function in Python can write text to different destinations using the file parameter. By default, print() outputs to the console, but you can redirect it to files or other output streams. Syntax print(*values, file=file_object, sep=' ', end='', flush=False) The file parameter accepts any object with a write() method, such as file objects, sys.stdout, or sys.stderr. Printing to a File You can write directly to a file by opening it in write mode and passing the file object to the file parameter ? # Open file in write mode with ...
Read MorePython Extract specific keys from dictionary?
Dictionaries are one of the most extensively used data structures in Python. They contain data in the form of key-value pairs. Sometimes you need to extract only specific keys from a dictionary to create a new dictionary. Python provides several approaches to accomplish this task efficiently. Using Dictionary Comprehension with Set Intersection This approach uses dictionary comprehension with set intersection to filter keys. The & operator finds common keys between the dictionary keys and your desired keys ? Example schedule = {'Sun': '2 PM', 'Tue': '5 PM', 'Wed': '3 PM', 'Fri': '9 PM'} # ...
Read MorePython Count set bits in a range?
A given positive number when converted to binary has a number of set bits. Set bits in a binary number are represented by 1. In this article we will see how to count the number of set bits in a specific range of positions within a binary representation of a number. Using bin() and String Slicing In the below example we take a number and apply the bin() function to get the binary value. Then we slice it to remove the prefixes and count set bits in the specified range ? Example def SetBits_cnt(n, l, ...
Read MorePython Convert nested dictionary into flattened dictionary?
As the world embraces more unstructured data, we come across many formats of data where the data structure can be deeply nested like nested JSONs. Python has the ability to deal with nested data structure by concatenating the inner keys with outer keys to flatten the data. In this article we will take a nested dictionary and flatten it. Using a Recursive Approach In this approach we design a function to recursively process each item in the dictionary. We pass the dictionary, design a place holder for the output dictionary, the key and separator as parameters. We use ...
Read MorePython Check if suffix matches with any string in given list?
Checking if a suffix matches with any string in a list is a common string manipulation task in Python. A suffix is the ending part of a string, and we need to verify if any string in our list ends with the given suffix pattern. Understanding Suffixes A suffix is a substring that appears at the end of a string. For example, in "Sunday", the suffix "day" appears at the end. Let's see how to check if any string in a list ends with a specific suffix. Using any() with endswith() The most efficient approach combines ...
Read MoreMemory-mapped file support in Python (mmap)?
Python's mmap module provides memory-mapped file support, allowing you to map files directly into memory for efficient reading, writing, and searching. Instead of making system calls like read() and write(), memory mapping loads file data into RAM where you can manipulate it directly. Basic Memory Mapped File Reading Memory mapping loads the entire file into memory as a file-like object. You can then slice and access specific portions efficiently ? import mmap # Create a sample file first with open('sample.txt', 'w') as f: f.write('The emissions from gaseous compounds are harmful to ...
Read MoreHyperText Markup Language support in Python?
Python has the capability to process HTML files through the HTMLParser class in the html.parser module. It can detect the nature of HTML tags, their position, and many other properties. It has functions which can also identify and fetch the data present in an HTML file. The HTMLParser class allows you to create custom parser classes that can process only the tags and data that you define. You can handle start tags, end tags, and text data between tags. Basic HTML File Structure Let's start with a simple HTML file that we'll parse ? ...
Read MorePython Get the real time currency exchange rate?
Python is excellent at handling API calls for retrieving real-time and historical currency exchange rates. This article demonstrates two primary methods: using the forex-python module and making direct API calls. Using forex-python Module The forex-python module provides the most direct way to get currency conversion rates. It offers simple functions that accept currency codes and return conversion rates. Real-time Exchange Rates Here's how to get live currency conversion rates ? from forex_python.converter import CurrencyRates c = CurrencyRates() # Get USD to GBP conversion rate rate = c.get_rate('USD', 'GBP') print(f"1 USD = {rate} ...
Read More