How to restrict argument values using choice options in Python?

Kiran P
Updated on 25-Mar-2026 12:04:58

3K+ Views

When building command-line applications in Python, you often need to restrict user input to specific valid values. Python's argparse module provides the choices parameter to limit argument values to predefined options, preventing invalid input and improving data validation. Basic Argument Parser Without Restrictions Let's start with a simple tennis Grand Slam title tracker that accepts any integer value ? import argparse def get_args(): """Function to parse command line arguments""" parser = argparse.ArgumentParser( description='Tennis Grand Slam title tracker', ... Read More

How to use one or more same positional arguments in Python?

Kiran P
Updated on 25-Mar-2026 12:04:19

927 Views

Python's argparse module allows you to handle multiple positional arguments of the same type using the nargs parameter. This is particularly useful when you need exactly N arguments of the same data type, like performing arithmetic operations on numbers. Using nargs for Same Type Arguments The nargs parameter specifies how many command-line arguments should be consumed. When you set nargs=2, argparse expects exactly two values of the specified type. Example: Subtracting Two Numbers Let's create a program that subtracts two integers using positional arguments − import argparse def get_args(): ... Read More

How to plot pie-chart with a single pie highlighted with Python Matplotlib?

Kiran P
Updated on 25-Mar-2026 12:04:01

1K+ Views

Pie charts are one of the most popular visualization types for displaying percentages and proportions. In this tutorial, we'll learn how to create pie charts with highlighted segments using Python's Matplotlib library. Basic Pie Chart Setup First, let's install and import the required library − import matplotlib.pyplot as plt # Sample data: Tennis Grand Slam titles tennis_stats = (('Federer', 20), ('Nadal', 20), ('Djokovic', 17), ('Murray', 3)) # Extract titles and player names titles = [title for player, title in tennis_stats] players = [player for player, title in tennis_stats] print("Titles:", titles) print("Players:", players) ... Read More

How to plot 4D scatter-plot with custom colours and cutom area size in Python Matplotlib?

Kiran P
Updated on 25-Mar-2026 12:03:33

1K+ Views

A 4D scatter plot in Matplotlib allows you to visualize four dimensions of data simultaneously: X and Y coordinates, point size (area), and color. This is useful for analyzing relationships between multiple variables in a single visualization. Installing Matplotlib First, install matplotlib using pip ? pip install matplotlib Basic 2D Scatter Plot Let's start with a simple 2D scatter plot using tennis player statistics ? import matplotlib.pyplot as plt # Tennis player data (name, grand slam titles) tennis_stats = (('Federer', 20), ('Nadal', 20), ('Djokovic', 17), ('Sampras', 14), ... Read More

How to extract required data from structured strings in Python?

Kiran P
Updated on 25-Mar-2026 12:03:07

2K+ Views

When working with structured strings like log files or reports, you often need to extract specific data fields. Python provides several approaches to parse these strings efficiently when the format is known and consistent. Understanding Structured String Format Let's work with a structured report format: Report: - Time: - Player: - Titles: - Country: Here's our sample data: report = 'Report: Daily_Report - Time: 2020-10-10T12:30:59.000000 - Player: Federer - Titles: 20 - Country: Switzerland' print(report) Report: Daily_Report - Time: 2020-10-10T12:30:59.000000 - Player: Federer - ... Read More

How to create Microsoft Word paragraphs and insert Images in Python?

Kiran P
Updated on 25-Mar-2026 12:02:39

589 Views

Creating Microsoft Word documents programmatically in Python is essential for automating report generation. The python-docx library provides a simple interface to create paragraphs, add text formatting, and insert images into Word documents. Installing python-docx First, install the required library using pip ? pip install python-docx Creating Paragraphs and Adding Text Start by creating a new document and adding paragraphs with text ? import docx # Create a new document word_doc = docx.Document() # Add a paragraph paragraph = word_doc.add_paragraph('1. Hello World, Some Sample Text Here...') run = paragraph.add_run() ... Read More

How to Add Legends to charts in Python?

Kiran P
Updated on 25-Mar-2026 12:02:10

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
Updated on 25-Mar-2026 12:01:37

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
Updated on 25-Mar-2026 12:01:06

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
Updated on 25-Mar-2026 12:00:37

933 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

Advertisements