Python range powers of 2

You don't need your own function for this one, just use a lambda

import sys
from math import log
for i in map[lambda v : pow[2,v], range[0,log[1024, 2]]]:
    print i

output looks like

1
2
4
8
16
32
64
128

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:

from math import log
import sys
for i in map[lambda v : pow[2,v], range[0,int[log[sys.maxint, 2]]]]:
    print i

Output looks like

1
2
4
8
16
32
64
128

2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
4294967296
8589934592
17179869184
34359738368
68719476736
137438953472
274877906944
549755813888
1099511627776
2199023255552
4398046511104
8796093022208
17592186044416
35184372088832
70368744177664
140737488355328
281474976710656
562949953421312
1125899906842624
2251799813685248
4503599627370496
9007199254740992
18014398509481984
36028797018963968
72057594037927936
144115188075855872
288230376151711744
576460752303423488
1152921504606846976
2305843009213693952
4611686018427387904

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 55e1c6b5d3292fba3e0000c2

The code is fine apart from the for loop line. Read that line again. for i in range[0, len[my_list], 2**i] means to take the next value from the range and then assign it to i. So first, Python has to generate the range. This means that i can only be assigned a value when the range has been generated. This is how things will go in your case:

  1. Python sees the for loop
  2. Python tries to generate a range [it doesn’t know what i is yet]
  3. It notices that you have passed i to the range[] method, it doesn’t know what i is so it generates a NameError [which basically means that it doesn’t know what i is a name of]

This is how they should have gone:

  1. Python sees the for loop
  2. It tries to generate a range. Generation is successful.
  3. It assigns the first value of the range to i

As you can see, in your case, i has not been created yet and you are already trying to pass it in as an argument to the range[] function. If you tell me what you are trying to do then I could fix your code.

points

about 7 years

Answer 55e28f289113cb1cd400054d

points

about 7 years

Answer 55e3b13b937676a569000576

Try 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]
class range[start, stop[, step]]

Example:

for i in range[5]:
print[i]
....................Output...............
0
1
2
3
4

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]:
print[i]
....................Output..................
5
6
7
8
9
............................................
#when you specify three numbers in the range function, the last #number is considered step; increment by the last number instead of #the default which is one.
for i in range[0, 10, 3]:
print[i]
......................Output...................
0
3
6
9
...............................................
#you can also do increment for negative numbers
for i in range[0, -10, -3]:
print[i]
.....................Output....................
0
-3
-6
-9
...............................................
for i in range[-10, -100, -30]:
print[i]
......................Output....................
-10
-40
-70
................................................

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']
for i in range[len[a]]:
print[i, a[i]]
.....................Output......................
0 reader
1 is
2 doing
3 amazingly
4 well
..................................................
#a[i] is indexing the list a with the number i

A strange thing happens if you just print a range:

print[range[10]]........................Output...............
range[0, 10]
.............................................

In many ways, the object returned by range[] behaves as if it is a list, but in fact, it isn’t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really make the list, thus saving space.

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 for statement is such a construct, while an example of a function that takes an iterable is sum[]:

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]
...........Output..........
False
...........................
#if 10 is in the range of the numbers above print True otherwise #print Falseprint[10 in r]
..................Output.......
True
...............................
#print the index of the number 10 in the range aboveprint[r.index[10]]
..................Output.......
5
...............................
#print the number in the position of 5 in the range of values we have in the variable rprint[r[5]]
..................Output..........
10
..................................
#print the numbers that is indexed from the start position to index of 5print[r[:5]]
...............Output..............
range[0, 10, 2]
...................................
#print the last number of rprint[r[-1]]...............Output................
18
.....................................

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]]:
print[i]
....................Output..............
7
6
5
4
3
2
1
0
........................................

Lastly, maybe you are curious about how to get a list from a range. Here is the solution:

print[list[range[10]]].....................Output.................
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
............................................
print[list[range[1, 11]]].....................Output.................
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
............................................
print[list[range[0, 30, 5]]].....................Output.................
[0, 5, 10, 15, 20, 25]
............................................
print[list[range[0, 10, 3]]].....................Output.................
[0, 3, 6, 9]
............................................
print[list[range[0, -10, -1]]].....................Output.................
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
............................................
print[list[range[0]]].....................Output.................
[]
............................................
print[list[range[1, 0]]].....................Output.................
[]
............................................

Things to keep in mind

  • The arguments to the range constructor must be integers [either built-in int or any object that implements the __index__ special method]. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. If step is written as the number 0, ValueError is raised.
  • For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.
  • For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.
  • A range object will be empty if r[0] does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices.
  • Ranges containing absolute values larger than sys.maxsize are permitted but some features [such as len[]] may raise OverflowError.

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 [].

Chủ Đề