Saba Hilal

Saba Hilal

54 Articles Published

Articles by Saba Hilal

54 articles

Python program to input a comma separated string

Saba Hilal
Saba Hilal
Updated on 27-Mar-2026 5K+ Views

When working with user input, you often need to process comma-separated strings containing text, numbers, or mixed data. Python provides several approaches to split these strings into individual components and convert them to appropriate data types. Using split() Function for Text Strings The split() method is the most common approach to separate comma-delimited text ? # Input comma-separated string user_input = "apple, banana, cherry, dates" print("Original string:", user_input) # Split by comma and remove extra spaces items = [item.strip() for item in user_input.split(", ")] print("Processed list:", items) # Print each item on separate line ...

Read More

Python program to print all positive numbers in a range

Saba Hilal
Saba Hilal
Updated on 27-Mar-2026 761 Views

When working with ranges that include both positive and negative numbers, you often need to extract only the positive values. Python provides several approaches to filter positive numbers from a range: list comprehension, removal of negatives, filtering with conditions, and using the filter() function. Method 1: Using List Comprehension Create a separate list containing only positive numbers from the range − low_num = -10 high_num = 15 numbers = list(range(low_num, high_num + 1)) print("Original range:", numbers) # Extract positive numbers using list comprehension positive_numbers = [num for num in numbers if num > 0] ...

Read More

Python program to print all negative numbers in a range

Saba Hilal
Saba Hilal
Updated on 27-Mar-2026 647 Views

Sometimes you need to extract only the negative numbers from a range. Python provides several methods to filter negative numbers: using loops, list comprehension, and built-in functions like filter(). Method 1: Using a Loop to Separate Negative Numbers Create a separate list for negative numbers and append them using a loop ? low_num = -10 high_num = 15 main_numbers = list(range(low_num, high_num + 1)) negative_numbers = [] print("Original range:", main_numbers) # Separate negative numbers into a new list for num in main_numbers: if num < 0: ...

Read More

How to Make a Time Series Plot with Rolling Average in Python?

Saba Hilal
Saba Hilal
Updated on 27-Mar-2026 1K+ Views

In this article, we will explore two methods for creating a Python time series plot with a rolling average. Both approaches use popular libraries like Matplotlib, Pandas, and Seaborn, which provide powerful capabilities for data manipulation and visualization. Following these methods will enable you to visualize time series data with a rolling average efficiently and understand its general behavior. Both methods involve similar sequential steps: loading the data, converting the date column to a DateTime object, calculating the rolling average, and generating the plot. The primary difference lies in the libraries used for plotting. Sample Data For ...

Read More

Python Program to Find the Number of Unique Words in Text File

Saba Hilal
Saba Hilal
Updated on 27-Mar-2026 4K+ Views

Finding the number of unique words in a text file is a common text processing task. Python provides several approaches to accomplish this, with sets and dictionaries being the most efficient methods. Sample Text File Content For demonstration, we'll create a sample text file with repeated content ? # Create a sample text file sample_text = """This is a new file. This is made for testing purposes only. There are four lines in this file. There are four lines in this file. There are four lines in this file. There are four lines in this file. ...

Read More

Python program to find the frequency of the elements which are common in a list of strings

Saba Hilal
Saba Hilal
Updated on 27-Mar-2026 281 Views

In this Python article, we explore how to find the frequency of elements that are common in a list of strings. We'll demonstrate three different approaches: finding character frequency using reduce(), word frequency using list operations, and word frequency using pandas functions. Example 1: Character Frequency Using reduce() Function This approach finds the frequency of characters that appear in all strings using the reduce() function with Counter ? from functools import reduce from collections import Counter # Sample list of strings strings = ['Types of Environment - Class Discussion', ...

Read More

Python Program to Find Sum of First and Last Digit

Saba Hilal
Saba Hilal
Updated on 27-Mar-2026 2K+ Views

In this article, we explore how to find the sum of the first and last digits of an integer. This task requires extracting the first digit (leftmost) and the last digit (rightmost) from any number, then adding them together. We'll demonstrate four different approaches to solve this problem. Method 1: Using Repeated Division to Count Digits This approach counts digits by repeatedly dividing the number by 10, then extracts the first digit using division and the last digit using modulo ? import math def count_digits(number): count = 0 ...

Read More

Python Program to Find Unique Lines From Two Text Files

Saba Hilal
Saba Hilal
Updated on 27-Mar-2026 610 Views

Finding unique lines between two text files is a common task in data processing and file comparison. Python provides several approaches to accomplish this, from basic iteration to using specialized libraries. In this article, we'll explore three different methods to identify lines that exist in one file but not the other. Sample Data For our examples, we'll work with two text files containing course names. Here's the content comparison ? Course Name In a.txt In b.txt Introduction to Computers Yes Yes Introduction to Programming Concepts Yes Yes Introduction to ...

Read More

Python program to find start and end indices of all Words in a String

Saba Hilal
Saba Hilal
Updated on 27-Mar-2026 403 Views

Finding the start and end indices of words in a string is useful for text processing, highlighting, and analysis. Python provides multiple approaches: manual iteration through characters or using specialized libraries like NLTK. Method 1: Using Manual Iteration This approach iterates through each character, detecting spaces to identify word boundaries ? Algorithm Step 1 − Create a function that iterates through the string character by character. Step 2 − Track the starting index of each word and detect spaces to find ending indices. Step 3 − Handle the last word separately since it doesn't end with ...

Read More

Python Program to Get Sum of N Armstrong Numbers

Saba Hilal
Saba Hilal
Updated on 27-Mar-2026 278 Views

An Armstrong number (also called a narcissistic number) is a number where the sum of its digits raised to the power of the total number of digits equals the number itself. For example, 153 is a 3-digit Armstrong number because 1³ + 5³ + 3³ = 1 + 125 + 27 = 153. This tutorial shows how to find and sum all Armstrong numbers of a given digit length using Python. What is an Armstrong Number? Let's first understand the concept with a simple function to check if a number is an Armstrong number ? ...

Read More
Showing 1–10 of 54 articles
« Prev 1 2 3 4 5 6 Next »
Advertisements