Found 34472 Articles for Programming

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

Saba Hilal
Updated on 10-Jul-2023 17:07:13

2K+ Views

In this article, the given task is to find the number of unique words in a text file. In this Python article, using two different examples, the approaches to finding the unique words in a text file and their count are given. In the first example, given words from a text file are fetched, and then their unique set is made before counting these unique words. In example 2, first the list of words is created, then it is sorted. After this from this sorted list, the duplicates are removed and finally, the unique word left in the file ... Read More

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

Saba Hilal
Updated on 10-Jul-2023 17:03:58

111 Views

In this Python article, the given task is to get the frequency of the elements which are common in a list of strings. Sometimes the list to be analyzed using Python is available in an Excel file. To get this list from Excel, a module called openpyxl is used. In this Python article, using three different examples, the ways of getting the frequency of the items which are repeated in a list of strings are given. In example 1, the frequency of the characters which are common in a list of strings is found. In the next two examples, ... Read More

Python Program to Find Sum of First and Last Digit

Saba Hilal
Updated on 10-Jul-2023 16:51:14

887 Views

In this article, the given task is to add the first and the last digit of an integer. Now the integer can be very small or it can be a big number. So, these programs will have two parts. First, we need to find how big the integer is and then get the first digit from it. The second part will be to get the last number from the given integer which can be done easily by dividing the number by ten and finding the remainder. In this Python article, using four different examples, the approaches to adding the first ... Read More

Python program to print positive numbers in a list

Saba Hilal
Updated on 10-Jul-2023 16:23:30

364 Views

In this article, the task is to print the positive numbers from a list of integers. Here, in this Python article, this task is done using the different methods in 4 different examples. Then these positive numbers are printed. In example 1, the positive numbers are picked and separated into another list. In the example 2, all the elements that are not positive are removed. In example 3, the sorted list is split up to zero and only positives are retained. In example 4, filter is used to select the positive numbers. Example 1 - Select only the positive numbers ... Read More

Python Program to Find Unique Lines From Two Text Files

Saba Hilal
Updated on 10-Jul-2023 15:52:36

230 Views

Many times we see two files that look similar but had certain differences. If the files are big or have lots of content, searching for that difference or finding the uniqueness in that file manually is not easy. However, this problem of finding the unique lines in two text files can be done easily using Python programs. In this article, using three different examples, three different ways of finding the unique lines in two text files are given. The text files used are a.txt and b.txt while in the end the result is stored in another txt file. For these ... Read More

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

Saba Hilal
Updated on 10-Jul-2023 15:50:05

191 Views

Sometimes, we require starting index of a word and also the last index of that word. A sentence is made up of words that are separated by spaces. In this Python article, using two different examples, the two different ways of finding the beginning and ending indices of all words in a sentence or a given string, are given. In the first example, the process of simple iteration through all the characters of a string is followed while finding the spaces to mark the beginning of words. In example 2, a Natural Language Toolkit is used for the task of ... Read More

Python Program to Get Sum of N Armstrong Numbers

Saba Hilal
Updated on 10-Jul-2023 15:46:46

104 Views

A number is said to be an Armstrong number if the digits are taken individually and raised to the power of the total digits and then these subparts are added together and they give the number itself. Here, in this Python example, using two different examples, the method of finding the total sum of n-digit Armstrong numbers is given. In example 1, the method to calculate the sum of all the 3 digit Armstrong numbers is given. In example 2, the number of digits can be decided by the user at runtime. This program is tested using digit values 4 ... Read More

Python Program to find Sum of Negative, +ve Even and +ve Odd numbers in a List

Saba Hilal
Updated on 10-Jul-2023 15:41:43

514 Views

Sometimes the task is to separate the negative and positive numbers or to separate the odd numbers and even numbers from a list of integers. Here, in this Python article, both these tasks are done. First negative numbers are separated into another list. Then the positive odd and positive even numbers are separated into separate lists. To calculate the sum, the numbers from these negative number lists, positive odd number lists, and positive even number lists are taken to calculate sums separately. In the two different examples, the main list of integers is formed. In example 1, the negative and ... Read More

Python Program to Find Numbers Divisible by 7 and Multiple of 5 in a Given Range

Saba Hilal
Updated on 10-Jul-2023 15:38:57

937 Views

A multiple of a number n is also divisible by the same number n. If a number M is a multiple of a number n, then if we divide M by n, its remainder must be zero. Also to create the multiples of number n within a given range, we can add n multiple times and check that the resulting number is within the given range or not. Another way to make multiples of a given number n, within a given range, is to find the first multiple in the given range for n and then to find the value ... Read More

Python program to find the string weight

Saba Hilal
Updated on 10-Jul-2023 15:29:07

387 Views

In this article, the given task is to find the total string weight. To calculate the string weight, we have converted the given strings into the lower form. Considering the weight of the characters, we have taken a=1, b=, 2 and so up to z=26. In this Python article, using two different examples, the approaches to find the given string’s weight are given. In the first example, given characters in a string are fetc, hed, and then their respective weight is added to the updated weight. In example 2, first the frequency of occurrence of a given character in the ... Read More

Advertisements