Newbie questions on Python

C

Chris Angelico

Hi all,
i'm programming in python for the first time (usually i use C as programming language). I don't understand these results:
a=[1,2,3,4,5]
a[:-1] [1, 2, 3, 4]
a[::-1] [5, 4, 3, 2, 1]
a[2::-1]
[3, 2, 1]

what does a[2::-1] means?

That's taking a slice. This page has something to say on the subject:

http://docs.python.org/3.3/tutorial/introduction.html

By the way, regarding your email address: there are no cheat codes in
Python... either that, or Python *is* a cheat code. :)

ChrisA
[[ VERY HAPPY CODING ADDED ]]
 
M

Matt Jones

When slicing: l[start:end:step]

In your example of "a[2::-1]" you are reversing the list by using a step of
-1, then you are slicing at index 2 (third element).

*Matt Jones*


Hi all,
i'm programming in python for the first time (usually i use C as
programming language). I don't understand these results:
a=[1,2,3,4,5]
a[:-1] [1, 2, 3, 4]
a[::-1] [5, 4, 3, 2, 1]
a[2::-1]
[3, 2, 1]

what does a[2::-1] means?

That's taking a slice. This page has something to say on the subject:

http://docs.python.org/3.3/tutorial/introduction.html

By the way, regarding your email address: there are no cheat codes in
Python... either that, or Python *is* a cheat code. :)

ChrisA
[[ VERY HAPPY CODING ADDED ]]
 
N

Neil Cerutti

Hi all,
i'm programming in python for the first time (usually i use C as programming language). I don't understand these results:
a=[1,2,3,4,5]
a[:-1] [1, 2, 3, 4]
a[::-1] [5, 4, 3, 2, 1]
a[2::-1]
[3, 2, 1]

The third item is the "step". The default value is 1. If you
provide a negative step, your slice will be in reverse. So you
are getting item 2 through 0 in reverse order in your result
slice.

Imagine something like the following for loop taking place
somewhere:

for (int i = 2; i <= 0; --i) {
fprintf(a);
}
 
L

Lele Gaifax

Neil Cerutti said:
Imagine something like the following for loop taking place
somewhere:

for (int i = 2; i <= 0; --i) {
fprintf(a);
}


Neil most probably meant

for (int i = 2; i >= 0; --i) {
fprintf(a);
}

where "fprintf" is actually a fictitious "do_something" function.

ciao, lele.
 
N

Neil Cerutti

Neil Cerutti said:
Imagine something like the following for loop taking place
somewhere:

for (int i = 2; i <= 0; --i) {
fprintf(a);
}


Neil most probably meant

for (int i = 2; i >= 0; --i) {
fprintf(a);
}

where "fprintf" is actually a fictitious "do_something" function.

ciao, lele.


Thanks for the correction.
 
C

Chris Angelico

ROFLMAO. Incidentally, my son used to use IDDQD rather than IDKFA.

I of course spurned all such, since I preferred to do it the hard way.
Thus I was Doomed.

I'd sometimes use IDDQD/IDFA (no K) and then see how quickly I could
blitz the levels, with proper navigation. There's something inherently
fun about blasting through everything with the rocket launcher.

Mind you, I was really cheap, so I never bought Doom... and was just
playing through the demo over and over.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top