Programming Articles

Page 49 of 2547

Check if the Sum of Digits of a Number N Divides It

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 415 Views

A number is divisible by the sum of its digits when the remainder of dividing the number by its digit sum equals zero. For example, if N = 36, the digit sum is 3 + 6 = 9, and since 36 % 9 = 0, the number 36 is divisible by its digit sum. Algorithm The algorithm follows these steps ? Extract each digit of the number using modulo operator (% 10) Add all digits to get the sum Check if the original number is divisible by this sum using modulo operator Return true if remainder ...

Read More

How to Sort a Pandas DataFrame by Date?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 3K+ Views

Sorting a Pandas DataFrame by date is a common operation in data analysis. Pandas provides several methods to accomplish this, with sort_values() being the most efficient. Before sorting, ensure your date column is in proper datetime format using to_datetime(). Basic Date Sorting with sort_values() The most straightforward method is using sort_values() after converting string dates to datetime format ? import pandas as pd # Create sample DataFrame with date strings data = { 'Date': ['2023-06-26', '2023-06-24', '2023-06-28', '2023-06-25'], 'Sales': [100, 200, 300, 150] } df ...

Read More

Python - Filter Tuples with All Even Elements

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 384 Views

Filtering tuples with all even elements is a common operation in Python data processing. This involves checking if every element in each tuple is divisible by 2 and keeping only those tuples that satisfy this condition. Using List Comprehension with all() The most Pythonic approach combines list comprehension with the all() function to filter tuples where every element is even ? tuple_list = [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7, ), (2, 4, 6)] result = [tup for tup in tuple_list if all(ele % 2 == 0 for ele in ...

Read More

Check if a Word is Present in a Sentence

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 1K+ Views

In Python, checking if a word is present in a sentence is a common task in text processing and string manipulation. Python provides multiple approaches including the in operator, find() method, and split() method for word-by-word matching. Using the in Operator The simplest way to check if a word exists in a sentence is using Python's in operator ? sentence = "The bestseller book is amazing" word = "book" if word in sentence: print("The given word is present in the sentence.") else: print("The given word is not ...

Read More

Check if a number with even number of digits is palindrome or not

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 471 Views

A palindrome is a number that reads the same forwards and backwards. This article shows how to check if a number with an even number of digits is a palindrome in Python. Examples include 2662, 4224, 44, 1001, etc. Using String Comparison The simplest approach converts the number to a string and compares it with its reverse ? def is_even_digit_palindrome(num): # Convert number to string num_str = str(num) # Check if even number of digits ...

Read More

Python - K List Dictionary Mesh

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 229 Views

The term dictionary mesh refers to creating a nested dictionary structure where each element from the first list becomes a key, and each element from the second list becomes a nested key with empty lists as values. This creates a mesh-like structure that can hold organized data. Understanding Dictionary Mesh Given two input lists ? list1 = [10, 20, 30] list2 = [40, 50] The dictionary mesh creates this structure ? {10: {40: [], 50: []}, 20: {40: [], 50: []}, 30: {40: [], 50: []}} Method 1: Using Nested ...

Read More

How to iterate over Columns in Numpy

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 2K+ Views

NumPy provides several methods to iterate over columns in a 2D array. The most common approaches include using nditer() with transpose, array transpose directly, apply_along_axis(), and manual iteration with indexing. Syntax Here are the key functions used for column iteration ? np.nditer(array.T) # Iterator with transpose array.T # Array transpose np.apply_along_axis() # Apply function along axis array.shape[1] ...

Read More

Python - K Elements Reversed Slice

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 308 Views

K Elements Reversed Slice extracts the last K elements from a list and returns them in reverse order. This technique is useful for data analysis, queue processing, and extracting recent entries from datasets. What is K Elements Reversed Slice? A K elements reversed slice takes the last K elements from a list and reverses their order. For example, if we have [1, 2, 3, 4, 5] and K=3, we get the last 3 elements [3, 4, 5] reversed as [5, 4, 3]. Using Slicing The most concise approach uses Python's slice notation with negative indexing and ...

Read More

Python - Filter unequal elements of two lists corresponding same index

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 658 Views

When working with two lists in Python, you may need to find elements that are equal at the same index positions. This is useful for data comparison, synchronization, and filtering operations. For example, given two lists: list1 = [10, 20, 30, 40, 50] list2 = [1, 20, 3, 40, 50] # Elements at same index: 20, 40, 50 are equal Using zip() and List Comprehension The most Pythonic approach uses zip() to pair elements and list comprehension to filter matches − list1 = [1, 2, 3, 4, 5] list2 = [1, 2, ...

Read More

Finding Words with both Alphabet and Numbers using Python

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 840 Views

Finding words that contain both alphabetic characters and numeric digits is a common text processing task in Python. This tutorial demonstrates four different approaches using built-in functions like filter(), isdigit(), isalpha(), and regular expressions. Problem Overview Given a text string, we need to extract words that contain both letters and numbers ? Input my_text = "WELCOME TO CLUB100" print("Input:", my_text) Input: WELCOME TO CLUB100 Expected Output ['CLUB100'] Method 1: Using Regular Expressions Regular expressions provide a powerful pattern-matching approach to find words containing both letters and digits ...

Read More
Showing 481–490 of 25,469 articles
« Prev 1 47 48 49 50 51 2547 Next »
Advertisements