Programming Articles

Page 510 of 2547

Program to find next board position after sliding the given direction once in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 242 Views

Suppose we have a 2048 game board representing the initial board and a string direction representing the swipe direction, we have to find the next board state. As we know in the 2048 game, we are given a 4 x 4 board of numbers (some of them are empty, represented in here with 0) which we can swipe in any of the 4 directions ("U", "D", "L", or "R"). When we swipe, all the numbers move in that direction as far as possible and identical adjacent numbers are added up exactly once. So, if the input is like ? ...

Read More

How to use the Subprocess Module in Python?

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

The subprocess module in Python allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This is the recommended way to execute system commands and interact with the operating system from Python programs. Understanding Processes When you execute a program, your Operating System creates a process. It uses system resources like CPU, RAM, and disk space. A process is isolated from other processes — it can't see what other processes are doing or interfere with them. Python's subprocess module provides a powerful interface for working with processes, allowing you to run ...

Read More

How to process iterators in parallel using ZIP

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

The zip() function allows you to iterate over multiple sequences in parallel, pairing elements by index. This is particularly useful for processing corresponding elements from different iterables simultaneously. Basic List Processing Example First, let's see a traditional approach to multiply each element by 5 − numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] multiply_by_5 = [] for x in numbers: multiply_by_5.append(x * 5) print(f"Output: {multiply_by_5}") Output: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50] Using list comprehension, we can achieve the ...

Read More

How to process excel files data in chunks with Python?

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

Processing large Excel files can consume significant memory and slow down your Python applications. When dealing with Excel spreadsheets containing thousands of rows, loading the entire file into memory at once isn't always practical. This article demonstrates how to process Excel files in manageable chunks using Python and Pandas. Prerequisites Before working with Excel files in Python, you need to install the required libraries ? # Install required packages # pip install pandas openpyxl xlsxwriter import pandas as pd import xlsxwriter Creating Sample Excel Data First, let's create a sample Excel file to ...

Read More

How to Parse HTML pages to fetch HTML tables with Python?

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

Extracting HTML tables from web pages is a common task in web scraping and data analysis. Python provides powerful libraries like requests, BeautifulSoup, and pandas to make this process straightforward. Required Libraries First, install the necessary packages if they're not already available ? pip install requests beautifulsoup4 pandas tabulate Basic Setup Import the required libraries and set up the target URL ? import requests import pandas as pd from bs4 import BeautifulSoup from tabulate import tabulate # Set the target URL site_url = "https://www.tutorialspoint.com/python/python_basic_operators.htm" Making HTTP Request ...

Read More

How to find and filter Duplicate rows in Pandas ?

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

Sometimes during data analysis, we need to examine duplicate rows to understand patterns in our data rather than dropping them immediately. Pandas provides several methods to find, filter, and handle duplicate rows effectively. The duplicated() Method The duplicated() method identifies duplicate rows in a DataFrame. Let's work with an HR dataset to demonstrate this functionality ? import pandas as pd import numpy as np # Import HR Dataset with certain columns df = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/HRDataset.csv", usecols=["Employee_Name", "PerformanceScore", "Position", "CitizenDesc"]) ...

Read More

How to select a Subset Of Data Using lexicographical slicingin Python Pandas?

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

Pandas provides powerful indexing capabilities to select subsets of data. Lexicographical slicing allows you to select data based on alphabetical ordering of string indexes, similar to how words are arranged in a dictionary. Loading and Exploring the Dataset Let's start by importing a movies dataset and examining its structure − import pandas as pd import numpy as np movies = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/movies_data.csv", index_col="title", ...

Read More

How to select subset of data with Index Labels in Python Pandas?

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

Pandas provides powerful selection capabilities to extract subsets of data using either index positions or index labels. This article demonstrates how to select data using index labels with the .loc accessor. The .loc attribute works similar to Python dictionaries, selecting data by index labels rather than positions. This is different from .iloc which selects by integer position like Python lists. Setting Up the Dataset Let's start by importing a movies dataset with the title as the index ? import pandas as pd movies = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/movies_data.csv", ...

Read More

How to Find The Largest Or Smallest Items in Python?

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

Finding the largest or smallest items in a collection is a common task in Python. This article explores different methods to find single or multiple largest/smallest values efficiently. Method 1: Using min() and max() for Single Items For finding a single smallest or largest item (N=1), min() and max() are the most efficient functions ? import random # Create a random list of integers random_list = random.sample(range(1, 10), 9) print("List:", random_list) # Find the smallest number smallest = min(random_list) print("Smallest:", smallest) # Find the largest number largest = max(random_list) print("Largest:", largest) ...

Read More

How to Identify Most Frequently Occurring Items in a Sequence with Python?

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

When analyzing sequences of data, identifying the most frequently occurring items is a common task. Python's Counter from the collections module provides an elegant solution for counting and finding the most frequent elements in any sequence. What is a Counter? The Counter is a subclass of dictionary that stores elements as keys and their counts as values. Unlike regular dictionaries that raise a KeyError for missing keys, Counter returns zero for non-existent items. from collections import Counter # Regular dictionary raises KeyError regular_dict = {} try: print(regular_dict['missing_key']) except KeyError as e: ...

Read More
Showing 5091–5100 of 25,466 articles
« Prev 1 508 509 510 511 512 2547 Next »
Advertisements