Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 690 of 855
Python program to Count Even and Odd numbers in a List
In this article, we will learn how to count even and odd numbers in a Python list using three different approaches. Problem statement − We are given a list of integers, and we need to count how many are even and how many are odd. Using For Loop (Brute Force) The most straightforward approach is to iterate through each number and check if it's even or odd using the modulo operator ? numbers = [21, 3, 4, 6, 33, 2, 3, 1, 3, 76] even_count, odd_count = 0, 0 # Iterate through each number ...
Read MorePython Program to convert Kilometers to Miles
In this article, we will learn how to convert distance from kilometers to miles using Python. This is a common unit conversion problem in programming. Problem statement − We are given distance in kilometers and we need to convert it into miles. As we know that 1 kilometer equals 0.621371 miles (more precise conversion factor). Formula Miles = Kilometers × 0.621371 Method 1: Direct Conversion The simplest approach is to multiply kilometers by the conversion factor ? kilometers = 5.5 # conversion factor as 1 km = 0.621371 miles conversion_factor ...
Read MorePython program to convert hex string to decimal
Converting hexadecimal strings to decimal numbers is a common programming task. Python provides multiple approaches to handle this conversion using built-in functions and modules. What is Hexadecimal? Hexadecimal is a base-16 number system using digits 0-9 and letters A-F (representing values 10-15). For example, hex 'F' equals decimal 15, and hex '1A' equals decimal 26. Using int() Function The most straightforward method uses Python's built-in int() function with base 16 ? # Single hex digit hex_string = 'F' decimal_result = int(hex_string, 16) print(f"Hex '{hex_string}' = Decimal {decimal_result}") # Multi-digit hex string hex_string = ...
Read MoreConvert a list to string in Python program
In this article, we will learn different methods to convert a list to a string in Python. This is a common operation when you need to transform list elements into a single string representation. Problem statement − We are given a list iterable, and we need to convert it into a string type. There are four main approaches to solve this problem. Let's explore them one by one ? Using String Concatenation (Brute-force Approach) This method iterates through each element and concatenates them into a single string ? def listToString(s): ...
Read MorePython program to check if the given string is vowel Palindrome
In this article, we will learn how to check if a string becomes a palindrome after removing all consonants, keeping only the vowels. Problem statement − We are given a string containing both vowels and consonants. We need to remove all consonants and check if the resulting vowel-only string is a palindrome. A palindrome reads the same forwards and backwards. Our approach involves two steps: first extract only vowels from the string, then check if this vowel string is a palindrome. Algorithm The solution follows these steps ? Extract all vowels from the input ...
Read MorePython Program for Number of elements with odd factors in the given range
In this article, we will learn how to find the number of elements with odd factors in a given range. This problem is based on the mathematical property that only perfect squares have an odd number of factors. Problem statement − We are given a range [n, m], and we need to find how many numbers in this range have an odd number of factors. Understanding the Concept The key insight is that only perfect squares have an odd number of factors. This is because factors usually come in pairs (if d divides n, then n/d also ...
Read MorePython Program for nth multiple of a number in Fibonacci Series
The Fibonacci series is a sequence where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34... In this article, we'll find the position of the nth multiple of a given number k in the Fibonacci series. Problem Statement Given a number k and a value n, we need to find the position of the nth multiple of k in the Fibonacci series. For example, if k=4 and n=5, we find the 5th number in the Fibonacci series that is divisible by 4. Understanding the Approach ...
Read MoreFind sum of even factors of a number in Python Program
Finding the sum of even factors of a number involves identifying all numbers that divide evenly into the given number, then summing only the even factors. What are Factors? Factors are numbers that completely divide a given number, leaving zero as remainder. For example, the factors of 12 are: 1, 2, 3, 4, 6, and 12. For even factors, we only consider factors that are divisible by 2. If the given number is odd, it will have no even factors (except possibly 1, which isn't even). Example Analysis Let's examine the factors of 60: ...
Read MorePython Program for Efficient program to print all prime factors of a given number
In this article, we will learn about an efficient solution to find and print all prime factors of a given number. Prime factors are prime numbers that divide the given number exactly. Problem statement − We are given a number, we need to find all the prime factors of a given number. Algorithm Approach The efficient solution follows these steps: First, divide the number by 2 as much as possible Then check odd numbers from 3 up to √n If any remainder exists greater than 2, it's also a prime factor Example ...
Read MorePython program to find uncommon words from two Strings
In this article, we will learn how to find uncommon words from two strings. Uncommon words are words that appear exactly once across both strings combined. Problem statement − We are given two strings, we need to get the uncommon words from the given strings. Understanding Uncommon Words A word is considered uncommon if it appears exactly once when we combine both strings. For example: String 1: "hello world" String 2: "world python" Uncommon words: "hello", "python" (each appears once) "world" appears twice, so it's not uncommon Using Dictionary to Count Word Frequency ...
Read More