Consider the following code, which uses a while loop and found flag to search a list of powers of 2
Consider the following code, which uses a while loop and found flag to search a list of powers of 2 for the value of 2 raised to the fifth power (32).
Question 1: Rewrite the code with a while loop and an else clause to eliminate the found flag and final if statement.
Question 2: Rewrite the example to use a for loop with an else clause, to eliminate the explicit list-indexing logic.(Hint: to get the index of an item, use the list index method -- L.index(X) returns the offset of the first X in list L.
Question 3: Remove the for loop completely by rewriting the example with a simple in operator membership expression.
Question 4: Use a for loop and the list append method to generate the powers-of-2 list (L) instead of hardcoding a list literal
Question 5: Use a list comprehension and comment whether using a list comprehension makes the code more efficient.