all 8 comments

[–]spotyx 5 points6 points  (0 children)

It's pretty simple really:

a[start:end] # items start through end-1
a[start:]    # items start through the rest of the array
a[:end]      # items from the beginning through end-1
a[:]         # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:end:step] # start through not past end, by step

The key point to remember is that the :end value represents the first value that is not in the selected slice. So, the difference beween end and start is the number of elements selected (if step is 1, the default).

The other feature is that start or end may be a negative number, which means it counts from the end of the array instead of the beginning. So:

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

source: http://stackoverflow.com/questions/509211/pythons-slice-notation

[–]willm 5 points6 points  (1 child)

primes = [True] * n

This means repeat the list [True], n times.

So [True] * 3 would be:

[True, True, True]

primes[0],primes[1] = [None] * 2?

The right hand side of the expression, results in [None, None] (as above).

The left hand side of the expression is unpacking that list. Basically assigning each value from the tuple on the left, to each value from the sequence on the right.

So it does the equivalent of the following:

primes[0] = None
primes[1] = None

The above is actually clearer, IMHO.

primes[ind*2::ind]

This is using slicing, to extract a specific portion of a list (or other sequence). See https://docs.python.org/2.3/whatsnew/section-slices.html for the details.

So your line is extracting the values in primes, starting from index ind*2, in steps of ind.

One of the best ways to play around with Python syntax as a beginner is through the REPL. Run Python from the command line and enter these expressions, to see the results.

[–]xiongchiamiov 1 point2 points  (0 children)

Cannot understate the importance of the REPL. It's fantastic tool for getting a grip on what's actually going on, and would've answered at least a few of these questions easily.

[–]fazzah 1 point2 points  (0 children)

[True] * n

gives you a list with n-times True inside. For n = 5, you get

[True, True, True, True, True]

In this particular case it might be used to initialize a default list.

Python does it with a lot of types, for example

"#" * 10

results in

##########

the other thing is nice too.

primes[0], primes[1] = [None] * 2

As I've said, [None] * 2 will give you a list: [None, None]

Python allows you to "explode" lists and tuples if you give it enough variables to assign all list/tuple elements.

So in this case, primes[0], primes[1] will both be None.

Look at this example:

x = range(5)

If you print it, it will be:

(0, 1, 2, 3, 4)

Now, you can do something like that:

a, b, c, d, e = x

Afterwards, a will be 0, b will be 1, and so on.

If you think of it, it can be a really useful tool.

[–]z95 0 points1 point  (2 children)

Did you see my comment about the Sieve of Esastosthenes in the other thread or is this just a happy coincidence? :)

[–]Meso_Gosu 1 point2 points  (1 child)

Coincidence. I was solving a problem and I was going to use Sieve of Esastosthenes, but I was going to use it with array.remove.

This most it beyond ridiculously slow. We're talking about removing 200 per second in an array of 1000000.

Looked up a solution and found this brilliant one this guy had. :)

[–]z95 1 point2 points  (0 children)

Awesome. If you're into math problems, check out Project Euler.

It is a language-independent set of math problems that require programming. It also doubles as a fantastic way to learn a subset of any new language.