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
Programming Articles
Page 119 of 2547
Python program to create a dictionary from a string
In this article, we will learn different methods to convert a string into a dictionary in Python. This is useful when you have string data that represents key-value pairs and need to work with it as a dictionary. Problem statement − We are given a string input and need to convert it into a dictionary type. Here we will discuss three methods to solve this problem, each suitable for different string formats. Method 1 − Using eval() Function The eval() method works when the string syntax already resembles a dictionary format with curly braces, quotes, and ...
Read MoreCount words in a sentence in Python program
In this article, we will learn different approaches to count the number of words in a string in Python. This is a common text processing task that can be accomplished using several methods. Problem statement − We are given a string and need to count the number of words in it. Using split() Method The split() method breaks the string into a list using space as the default delimiter. This is the simplest and most commonly used approach ? Example test_string = "Tutorials point is a learning platform" # Display original string print("The ...
Read MoreCount positive and negative numbers in a list in Python program
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 MorePython 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 More