About 10,900,000 results
Open links in new tab
  1. python - How to return a list of numbers of the power of 2?

    Aug 9, 2024 · Returns list of powers of 2 less than or equal to n. Explanation: A number can only be a power of 2 if its log divided by the log of 2 is an integer. eg. log(32) = log(2^5) = 5 * log(2) 5 * log(2) when divided by log(2) gives 5 which is an integer.

  2. python - Raising elements of a list to a power - Stack Overflow

    May 19, 2015 · def turn_to_power(list, power=1): return [number**power for number in list] Example: list = [1,2,3] turn_to_power(list) => [1, 2, 3] turn_to_power(list,2) => [1, 4, 9]

  3. Generate a list of numbers that are powers of 2 from 1 to 10

    This Python code generates a list called powers_of_2 using a list comprehension to calculate the powers of 2 from 2^1 to 2^10. Here's how the code works: powers_of_2 = [2**x for x in range (1, 11)]: This line of code initializes a variable named powers_of_2 and assigns it the result of a list comprehension.

  4. Generate Powers of Two List in Python - CodePal

    This Python code generates a list of powers of two from 2^1 to 2^1024. The function generate_power_of_two_lists() is defined to perform this task. It returns a list containing the powers of two. The code uses a loop to calculate each power of two and appends it to the list. The resulting list is then printed as an example usage of the function.

  5. python - For loop iterate over powers of 2 - Stack Overflow

    Using range() and map() on Python 2 means you produce lists up-front. range() produces a list with all the integers from 0 to floor(squareroot(N)), and then map() produces another list of integers, each a power of two.

  6. Creating a Power of Two List - Solution Guide | CodeFriends

    Write a Python function to generate a list containing powers of 2 up to a given integer n.

  7. Create a set of numbers that are powers of 2 from 1 to 10

    This Python code uses a set comprehension to create a set named powers_of_2 that contains the powers of 2 for x ranging from 1 to 10. Here's how the code works: {2**x for x in range (1, 11)}: This set comprehension creates a set by iterating through the values of x from 1 to 10 (inclusive).

  8. python - Express a number as a sum of powers of 2 - Code …

    Aug 11, 2018 · Using those definitions, you can extract the powers of two like this: def two_powers(num): powers = [] while num != 0: powers.append(num & -num) num = num & (num - 1) return powers Since it potentially avoids testing many bits (depending on the size and sparseness of the input) it may be faster.

  9. Python: Power of a number in bases raises to the ... - w3resource

    Apr 22, 2025 · Write a Python program to create a list containing the power of said number in bases raised to the corresponding number in the index using Python map. pow() is given to map two list objects, one for each base and index parameter.

  10. Python | Exponentiation by K in list - GeeksforGeeks

    Feb 27, 2023 · Create an empty list named res to store the results of exponentiation. Start a for loop that iterates over each element in the test_list . Raise the current element x to the power of K using the ** operator and append the result to the res list using the append() method.

Refresh