Python range powers of 2
You don't need your own function for this one, just use a lambda Show
output looks like
If you know what power of 2 you need to go to. If you don't, you could just go up to the largest storable int, like this:
Output looks like 0 points about 7 years my_list = [0,1,2,3,6,5,7] for i in range(0,len(my_list),2**i): print my_list[i] this code gives an error Answer 55e1c6b5d3292fba3e0000c2The code is fine apart from the for loop line. Read that line again.
This is how they should have gone:
As you can see, in your case, points about 7 years Answer 55e28f289113cb1cd400054dpoints about 7 years Answer 55e3b13b937676a569000576Try the following: import math l = list(range(0,100)) for i in range(0, int(math.log(len(l),2))+1): print l[2**i - 1] Note that the last line is indented but does not show in the preview. points about 7 years Demystifying Python one function at a time. Python is a beautiful and powerful language to code in and I will be breaking down some functions in it to give you a better understanding of what they can do. In this article, I will be writing about the range function in python and the powerful things you can do with it. This is the first article in the series and I hope you will enjoy reading and implementing the codes. range() is a built-in function of Python. By default range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number. The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops. Pick your fighter:class range(stop) Example: for i in range(5): In the example above, you will note the number specified is not specified. This is because you are telling the for loop to iterate through all the numbers in the range of 0–5 without printing 5. It will stop the iteration at the number specified. The given number is never part of the generated sequence. So basically range(5) generates 5 values and range(10) generates 10 values; the legal indices for items of a sequence of the length of the number specified. It is possible to let the range start at another number, or to specify a different increment (even negative; this is called the ‘step’). Example: #when you specify two numbers in the range function, it sees the #first number as start number i.e start from the range of this #number and include it, and the second number as end number i.e end #the range here but don't include this number.for i in range(5, 10): If you add four numbers inside the range function…you are on your own…actually, you will get this: To iterate over the indices of a sequence, you can combine range() and len() as follows: a = ['reader', 'is', 'doing', 'amazingly', 'well'] A strange thing happens if you just print a range: print(range(10))........................Output............... In many ways, the object returned by We say such an object is iterable, that is, suitable as a target for functions and constructs that expect something from which they can obtain successive items until the supply is exhausted. We have seen that the print(sum(range(4))) # 0 + 1 + 2 + 3....................Output.................6 The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start, stop and step values, calculating individual items and subranges as needed). Range objects provide features such as containment tests, element index lookup, slicing, and support for negative indices. Example: r = range(0, 20, 2)#if 11 is in the range of the numbers above print True otherwise #print Falseprint(11 in r) Ranges implement all of the common sequence operations except concatenation and repetition (due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern). You can also use the reversed function with range; it performs the operation from the back Example: for i in reversed(range(8)): Lastly, maybe you are curious about how to get a list from a range. Here is the solution: print(list(range(10))).....................Output................. Things to keep in mind
Thank you for reading and I hope this article you have been able to learn one or two things. You can drop a comment if you have any questions and I can be contacted via email (). |