Python Articles

Page 694 of 855

Count positive and negative numbers in a list in Python program

Pavitra
Pavitra
Updated on 25-Mar-2026 5K+ Views

In this article, we will learn how to count positive and negative numbers in a Python list using different approaches. We'll explore various methods from simple loops to more advanced functional programming techniques. Problem statement − We are given a list, we need to count positive and negative numbers in it and display them. Using For Loop The most straightforward approach is to iterate through each element and check if it's positive or negative using a for loop ? numbers = [1, -2, -4, 6, 7, -23, 45, 0] pos_count, neg_count = 0, 0 ...

Read More

Python program to Count Even and Odd numbers in a List

Pavitra
Pavitra
Updated on 25-Mar-2026 6K+ Views

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 More

Python Program to convert Kilometers to Miles

Pavitra
Pavitra
Updated on 25-Mar-2026 421 Views

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 More

Python program to convert hex string to decimal

Pavitra
Pavitra
Updated on 25-Mar-2026 643 Views

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 More

Convert a list to string in Python program

Pavitra
Pavitra
Updated on 25-Mar-2026 292 Views

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 More

Python program to check if the given string is vowel Palindrome

Pavitra
Pavitra
Updated on 25-Mar-2026 549 Views

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 More

Python Program for Number of elements with odd factors in the given range

Pavitra
Pavitra
Updated on 25-Mar-2026 646 Views

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 More

Python Program for nth multiple of a number in Fibonacci Series

Pavitra
Pavitra
Updated on 25-Mar-2026 1K+ Views

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 More

Find sum of even factors of a number in Python Program

Utkarsha Naithani
Utkarsha Naithani
Updated on 25-Mar-2026 778 Views

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 More

Python Program for Efficient program to print all prime factors of a given number

Pavitra
Pavitra
Updated on 25-Mar-2026 397 Views

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 More
Showing 6931–6940 of 8,546 articles
« Prev 1 692 693 694 695 696 855 Next »
Advertisements