Programming Articles

Page 508 of 2547

How to Add Legends to charts in Python?

Kiran P
Kiran P
Updated on 25-Mar-2026 4K+ Views

Charts help visualize complex data effectively. When creating charts with multiple data series, legends are essential for identifying what each visual element represents. Python's matplotlib library provides flexible options for adding and customizing legends. Basic Legend Setup First, let's prepare sample data and create a basic bar chart with legends ? import matplotlib.pyplot as plt # Sample mobile sales data (in millions) mobile_brands = ['iPhone', 'Galaxy', 'Pixel'] units_sold = ( ('2016', 12, 8, 6), ('2017', 14, 10, 7), ('2018', 16, 12, 8), ...

Read More

How to Visualize API results with Python

Kiran P
Kiran P
Updated on 25-Mar-2026 2K+ Views

One of the biggest advantages of writing an API is to extract current/live data. Even when data is rapidly changing, an API will always get up-to-date information. API programs use specific URLs to request certain data, like the top 100 most played songs of 2020 on Spotify or YouTube Music. The requested data is returned in easily processed formats like JSON or CSV. Python allows users to make API calls to almost any URL. In this tutorial, we'll extract API results from GitHub and visualize them using charts. Prerequisites First, install the required packages ? ...

Read More

How to implement Multithreaded queue With Python

Kiran P
Kiran P
Updated on 25-Mar-2026 3K+ Views

A multithreaded queue is a powerful pattern for distributing work across multiple threads. Python's queue module provides thread-safe queue implementations that allow multiple threads to safely add and remove tasks. Understanding Queues A queue is a First In, First Out (FIFO) data structure. Think of it like a grocery store checkout line — people enter at one end and exit from the other in the same order they arrived. Key queue operations: enqueue — adds elements to the end dequeue — removes elements from the beginning FIFO — first element added is first to be ...

Read More

How to scan for a string in multiple document formats (CSV, Text, MS Word) with Python?

Kiran P
Kiran P
Updated on 25-Mar-2026 915 Views

Searching for strings across multiple document formats is a common task in data processing and content management. Python provides excellent libraries to handle CSV, text, and MS Word documents efficiently. Required Packages Install the following packages before starting − pip install beautifulsoup4 python-docx CSV File Search Function The CSV search function uses the csv.reader module to iterate through rows and columns − import csv def csv_stringsearch(input_file, input_string): """ Function: search a string in csv files. args: input file, ...

Read More

How to make the argument optional in Python

Kiran P
Kiran P
Updated on 25-Mar-2026 441 Views

Python functions and command-line scripts often need flexible parameter handling. Optional arguments allow you to provide default values when parameters aren't supplied, making your code more user-friendly and robust. Optional Function Arguments In Python functions, you can make arguments optional by providing default values ? def greet(name="World", greeting="Hello"): return f"{greeting}, {name}!" # Using default values print(greet()) # Using one argument print(greet("Alice")) # Using both arguments print(greet("Bob", "Hi")) Hello, World! Hello, Alice! Hi, Bob! Optional Command-Line Arguments with argparse The argparse module handles optional ...

Read More

How to combine multiple graphs in Python

Kiran P
Kiran P
Updated on 25-Mar-2026 19K+ Views

Python's Matplotlib library allows you to combine multiple graphs in a single figure to create comprehensive visualizations. You can use subplots to display different charts vertically or horizontally, and dual axes to overlay different data types on the same plot. Preparing Sample Data First, let's prepare sample data for mobile phone sales across different years ? import matplotlib.pyplot as plt # Sample mobile phone sales data (in millions) mobile_brands = ['iPhone', 'Galaxy', 'Pixel'] # Sales data: (Year, iPhone, Galaxy, Pixel) units_sold = ( ('2016', 12, 8, 6), ...

Read More

How to match text at the start or end of a string in Python?

Kiran P
Kiran P
Updated on 25-Mar-2026 1K+ Views

Checking if a string starts or ends with specific text patterns is a common task in Python programming. The startswith() and endswith() methods provide elegant solutions for pattern matching at string boundaries. Using startswith() Method The startswith() method returns True if the string begins with the specified prefix ? Example text = "Is USA colder than Australia?" print(f"Starts with 'Is': {text.startswith('Is')}") Starts with 'Is': True Example filename = "Hello_world.txt" print(f"Starts with 'Hello': {filename.startswith('Hello')}") Starts with 'Hello': True Example site_url = 'https://www.something.com' ...

Read More

How to Search and Replace text in Python?

Kiran P
Kiran P
Updated on 25-Mar-2026 771 Views

Python provides several methods to search for and replace text patterns in strings. For simple literal patterns, use str.replace(). For complex patterns, use the re module with regular expressions. Basic Text Searching Let's start by creating a sample text and exploring basic search methods ? def sample(): yield 'Is' yield 'USA' yield 'Colder' yield 'Than' yield 'Canada?' text = ' '.join(sample()) print(f"Output {text}") Output Is USA Colder Than Canada? ...

Read More

How to Implement Priority Queue in Python?

Kiran P
Kiran P
Updated on 25-Mar-2026 615 Views

A priority queue is a data structure where elements are processed based on their priority rather than insertion order. Python's queue module provides PriorityQueue for thread-safe priority-based processing. Basic FIFO Queue First, let's understand a regular FIFO (First In, First Out) queue where elements are processed in insertion order − import queue fifo = queue.Queue() # Put numbers into queue for i in range(5): fifo.put(i) # Get numbers from queue print("FIFO Output:") while not fifo.empty(): print(fifo.get()) FIFO Output: 0 1 2 3 ...

Read More

How to improve file reading performance in Python with MMAP function?

Kiran P
Kiran P
Updated on 25-Mar-2026 922 Views

Memory mapping (MMAP) allows Python to access file data directly through the operating system's virtual memory, bypassing traditional I/O operations. This technique significantly improves file reading performance by eliminating system calls and buffer copying. How MMAP Works MMAP maps file contents directly into memory, treating them as mutable strings or file-like objects. The mmap module supports methods like read(), write(), seek(), and slice operations. Basic File Reading with MMAP Let's create a sample file and demonstrate basic MMAP usage ? import mmap # Create sample text file sample_text = """Lorem ipsum dolor sit ...

Read More
Showing 5071–5080 of 25,466 articles
« Prev 1 506 507 508 509 510 2547 Next »
Advertisements