[python] array's

Started by
13 comments, last by Brandon N 18 years, 11 months ago
hey i made a array code:

a = "Hello"
print "Reveres of the a string is:\n"
a[4]+[3]+[2]+[1]+[0]

the code doesnt work: cannot concatenate 'str' and 'list' objects and if i do this:

a = "Hello"
a[3]

it doesnt work either: it just doesnt show anything
Advertisement
You can reverse the string by writing a[::-1]. It's more efficient than your version.

But your error is that you forgot to repeat the 'a'.
a[4]+a[3]+a[2]+a[1]+a[0]

[0], [1], [2] and [3] on their own are one-element list objects.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
You can reverse the string by writing a[::1]<:tt>. It's more efficient than your version.

Surely a typo: a[::-1].

can you of someone else explain this: a[::-1]?

and if i open a new window in the shell and type the code in it and then run the module the shell only shows:

>>>
>>>

and it should be:
>>> olleH
>>>
What we are dealing with is known as slicing, which basically amounts to selecting a subsequence (slice) of a sequence (this can be any type of ordered sequence: tuple, list, string ...). The syntax is
seq[start:end]

(where either start or end may be omitted to signify defaults of beginning and end of sequence, respectively) or, with an optional step parameter,
seq[start:end:step]

so that the slice [start, end) starts at index start, ends at index end-1 (as usual, the end of the range is exclusive), and we move in increments of step (for instance, taking every second or every third element). A few examples with strings:
>>> "hello"[1:5]'ello'>>> "hello"[0:4]'hell'>>> "hello"[0:4:1]'hell'>>> "hello"[0:5:2]'hlo'>>> "hello"[::-1]'olleh'>>> "hello"[4::-1]'olleh'
i still dont where the first : between () a[(:):-4]

what about the new window and the shell question?
Quote:Original post by jordi_0071
i still dont where the first : between () a[(:):-4]

Slicing works like somelist[a:b:c]

What is returned is a list [a.b) (from element index a to b-1) which is stepped through with c (so -1 would step through it in reverse, 2 would skip every other element and so on)

Quote:Original post by jordi_0071
what about the new window and the shell question?


if i do this:

open the IDLE >> File >> New Window >> write : a = "Hello"
a[4::-1]
print a

>> run module

then it showes just hello in the interpenter
Original post by jordi_0071
and if i open a new window in the shell and type the code in it and then run the module the shell only shows:

If you don't print the string, the result isn't going to show up in the interpreter. Results are only printed when you use the interpreter interactively.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
is there any other compiler that only debugs+compiles it and show the code in ms-dos? like ms visual C++?

This topic is closed to new replies.

Advertisement