Found 10476 Articles for Python

Program to create a list with n elements from 1 to n in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:14:45

3K+ Views

Suppose we have a number n. We have to create a list of elements of size n, the elements are from 1 to n.So, if the input is like n = 5, then the output will be [1, 2, 3, 4, 5]To solve this, we will follow these steps −use python list comprehension strategy to solve this problemcreate a list with i for each i from 1 to n, for this we use range() function. This will take lower bound which is n here, and upper bound n+1 because we want to generate up to n.ExampleLet us see the following ... Read More

Python program to find better divisor of a number

Arnab Chakraborty
Updated on 12-Oct-2021 07:33:33

450 Views

Suppose we have a number n. We have to find divisor of n which one is better based on these conditions: We have two numbers p and q, the one whose digits sum to a larger number is called better than the other one. When the sum of digits is same, then the smaller number is the better one.So, if the input is like n = 180, then the output will be 9 because the divisors are [1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 30, 36, 45, 60, 90, 180]. So the number whose digit ... Read More

Python program to get average heights of distinct entries

Arnab Chakraborty
Updated on 12-Oct-2021 07:09:11

682 Views

Suppose we have a set of heights there may be some duplicate entries as well. We have to find the average of distinct entries of these heights.So, if the input is like heights = [96, 25, 83, 96, 33, 83, 24, 25], then the output will be 52.2 because the unique elements are [96, 25, 83, 33, 24], so sum is 96 + 25 + 83 + 33 + 24 = 261, average is 261/5 = 52.2.To solve this, we will follow these steps −h_set := a set from heights to remove duplicatesreturn sum of h_set items / size of ... Read More

Program to find only even indexed elements from list in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:09:01

2K+ Views

Suppose we have a list of elements called nums. We have to filter out all odd indexed elements, so only return even indexed elements from that list.So, if the input is like nums = [5,7,6,4,6,9,3,6,2], then the output will be [7, 4, 9, 6]To solve this, we will follow these steps −use python list slicing strategy to solve this problemstart from index 1, end at the end of list and increase each step by 2, so the slicingsyntax is [1::2]ExampleLet us see the following implementation to get better understanding −def solve(nums): return nums[1::2] nums = [5,7,6,4,6,9,3,6,2] print(solve(nums))Input[5,7,6,4,6,9,3,6,2]Output[7, 4, 9, 6]

Python program to convert complex number to polar coordinate values

Arnab Chakraborty
Updated on 12-Oct-2021 07:07:03

426 Views

Suppose we have a complex number c, we have to convert it into polar coordinate (radius, angle). Complex number will be in form x + yj. The radius is the magnitude of the complex number which is square root of (x^2 + y^2). And the angle is the counter clockwise angle measured from positive x-axis to the line segment that joins x + yj to the origin. From the cmath library we can use the phase() function to calculate the angle. And abs() function on complex number will return the magnitude value.So, if the input is like c = 2+5j, ... Read More

Python program to get all permutations of size r of a string

Arnab Chakraborty
Updated on 12-Oct-2021 07:04:29

476 Views

Suppose we have a string s and a number r. We have to display all permutations of r number of characters in s. We have permutations() function to get all permutations. This function is present inside itertools library.So, if the input is like s = "HELLO" r = 3, then the output will be>['HEL', 'HEL', 'HEO', 'HLE', 'HLL', 'HLO', 'HLE', 'HLL', 'HLO', 'HOE', 'HOL', 'HOL', 'EHL', 'EHL', 'EHO', 'ELH', 'ELL', 'ELO', 'ELH', 'ELL', 'ELO', 'EOH', 'EOL', 'EOL', 'LHE', 'LHL', 'LHO', 'LEH', 'LEL', 'LEO', 'LLH', 'LLE', 'LLO', 'LOH', 'LOE', 'LOL', 'LHE', 'LHL', 'LHO', 'LEH', 'LEL', 'LEO', 'LLH', 'LLE', 'LLO', 'LOH', 'LOE', ... Read More

PHP – Return an array of all supported encodings with mb_list_encodings()

Urmila Samariya
Updated on 11-Oct-2021 13:17:35

321 Views

The mb_list_encodings() function in PHP is used to return an array of all supported encodings. This function is supported in PHP 5 or higher version.Syntaxarray mb_list_encodings()Parametersmb_list_encodings() takes no parameters.Return ValuesThis function returns a numerically indexed array.Errors/Exceptionsmb_list_encodings() does not produce any errors.Examplemb_list_encodings() does not produce any errors.OutputIt will produce the following output −array(87) {    [0]=> string(4) "pass"    [1]=> string(4) "auto"    [2]=> string(5) "wchar"    [3]=> string(7) "byte2be"    [4]=> string(7) "byte2le"    [5]=> string(7) "byte4be"    [6]=> string(7) "byte4le"    [7]=> string(6) "BASE64"    [8]=> string(8) "UUENCODE"    [9]=> string(13) ... Read More

Python program to find how much money will be earned by selling shoes

Arnab Chakraborty
Updated on 11-Oct-2021 12:40:03

420 Views

Suppose in a shoe shop there are n different shoe with different sizes present in an array called size and another list of pairs for m customers called demand is given, where demand[i] contains (shoe_size, money) so a customer with demand i has a demand of shoe whose size is shoe_size and he/she can pay given amount of money. We have to find how much money the shopkeeper can earn by selling these shoes.So, if the input is like shoes = [2, 3, 4, 5, 6, 8, 7, 6, 5, 18] demand = [(6, 55), (6, 45), (6, 55), (4, ... Read More

Python program to find Cartesian product of two lists

Arnab Chakraborty
Updated on 11-Oct-2021 12:35:53

6K+ Views

Suppose we have two list of data l1 and l2. We have to find Cartesian product of these two lists. As we know if two lists are like (a, b) and (c, d) then the Cartesian product will be {(a, c), (a, d), (b, c), (b, d)}. To do this we shall use itertools library and use the product() function present in this library. The returned value of this function is an iterator. We have to convert it into list by passing the output into list() constructor.So, if the input is like l1 = [1, 5, 6] l2 = [1, ... Read More

Python program to capitalize each word\'s first letter

Akshitha Mote
Updated on 15-Apr-2025 12:58:55

2K+ Views

Let's understand the problem statement: we need to convert a given strings to a title case, which involves capitalizing the first letter of each word while converting the remaining letters to lowercase. The following are the various ways to capitalize each word's first letter in a string - Using title() method Using capitalize() method Using upper() method Importing string module Importing regex module Capitalizing a String in a File Using the title() method to ... Read More

Advertisements