Sign in to follow this  
jordi_0071

[python] array's

Recommended Posts

jordi_0071    138
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

Share this post


Link to post
Share on other sites
Fruny    1658
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.

Share this post


Link to post
Share on other sites
Miserable    606
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].

Share this post


Link to post
Share on other sites
jordi_0071    138
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
>>>

Share this post


Link to post
Share on other sites
Miserable    606
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'

Share this post


Link to post
Share on other sites
Brandon N    354
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?


Share this post


Link to post
Share on other sites
jordi_0071    138
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

Share this post


Link to post
Share on other sites
Fruny    1658
[quote]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.

Share this post


Link to post
Share on other sites
Brandon N    354
I'm not sure what you mean, but if you write your code in somefile.py and then type in the command line "python somefile.py" it'll be run there.

Oh, and you have to assign a splice. Doing a[::-1] will return the spliced list, not auto-assign it to a.
i.e.:
s = a[::-1]
print s

Share this post


Link to post
Share on other sites
Fruny    1658
I don't understand your question. A python program will produce the same output whether you run it from within IDLE or as a standalone program.

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by jordi_0071
but isnt there any other compiler for python like ms visual C++?


There are two main python distributions (as far as I am aware of), the Python Foundation's and ActiveState's. There are a few python interpreter ('compiler') implementations: CPython, Jython, Stackless; all with a specific purpose. There are a number of Python IDEs: IDLE, wxPython's PyCrust, PythonWin, Boa Constructor, Komodo, Visual Python...

Share this post


Link to post
Share on other sites
Brandon N    354
Quote:
Original post by jordi_0071
but isnt there any other compiler for python like ms visual C++?

If you mean as in something that "makes a program" (ie .exe in windows) then no, nothing that relaly does that (outside of py2exe which just wraps this process).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this