[Tutor] Hello, and a newbie question

Andy McKenzie amckenzie4 at gmail.com
Tue Apr 16 23:20:01 CEST 2013


On Tue, Apr 16, 2013 at 4:18 PM, Dave Angel <davea at davea.name> wrote:

> On 04/16/2013 11:58 AM, Andy McKenzie wrote:
>
>> Hey folks.
>>
>> I'm just starting to pick up Python, and I'd like to avoid some of the
>> mistakes I made in the past.  To elaborate on that, my primary
>> programming/scripting experience is PHP, with a little bit of Perl thrown
>> in.  Like so many people who write in PHP, I was entirely self-taught, and
>> would be the first to admit that a lot of what I've written is, well...
>> wrong.  It works, but it's sloppy and inefficient, because there were
>> standard constructions and solutions I just didn't know about.  I'd like
>> to
>> avoid that with Python.
>>
>>
> Welcome to the mailing list.  I expect you'll find Python a much cleaner
> language than the other two, though php has some definite convenience for
> its particular niche.
>
>
>
>
>  So:  my first two questions to the list.
>>
>> 1) Python 2.7 or 3.x?  I know I'm going to want to do some work with NLTK
>> (which appears to only have an alpha version out for Python 3), but I've
>> just gone through the hassle of dealing with an upgrade from PHP 4 to 5.3,
>> and I'd rather not start learning something that's already obsolete.  Any
>> words of advice?
>>
>>
> If you have to use a library that's not available yet for 3.x, then you
> need to use 2.x  on the other hand, if you're learning now, maybe that
> library will be available by the time you actually need it.
>
> For most people, I'd advise against trying to use a tutorial that targets
> a different version than you're running.  If you get frustrated quickly,
> you can get bogged down by the differences when you're just copying an
> exact program out of some book.
>
> Python 3 in particular has spent some substantial effort cleaning up the
> warts, the biggest one being Unicode.  For beginning programmers using only
> ASCII, probably the main thing that'll bog you down is that print() is now
> a function, rather than a statement, so you need parentheses. But once you
> get used to seeing syntax error, you quickly get the hang of it.  And once
> you do, the function is much nicer.
>
>
>
>  2) Best practices.  I have the WROX Press Beginning Python book, which
>> targets Python 2.  Clearly that's of only limited value if I'm going to go
>> with Python 3, but it looks like it's at least going to be a good
>> overview.
>>   But some of the stuff they do seems to be fairly personalized, rather
>> than
>> trying to follow standards.  Should I just start out with the tutorial
>> from
>> docs.python.org?  I would assume that that would start putting me in the
>> right habits from the beginning... is that accurate, or is there a better
>> way to go?
>>
>> Thanks in advance,
>>    Andy McKenzie
>>
>
> I'd start with the python.org tutorial for the version you're trying to
> learn.  Get serious about trying everything, and don't try to absorb it all
> in one sitting, even though it can be done.
>
> And use a text editor that helps you indent, or even that colorizes your
> code.  And when you just want to try things, use the interpreter directly.
>  It's amazing what you can learn directly from it.  You can ask the
> interpreter lots of questions about an object:
>
>    help(obj)
>    dir(obj)
>    print( type(obj) )
>    print( repr(obj) )
>
> And don't forget to post here when you seem to be stuck.  Sometimes a well
> placed comment beats days of struggling.  When you do get an exception you
> don't understand, paste the whole thing, as well as the code you were
> trying.
>
> Best of luck.
>
>
>
>
Thanks for the advice, folks.  Given that it looks like the biggest changes
are unicode handling (which I'm not going to need any time soon) and the
way the print function works, I decided to stick with 2.7.  I'm an IT guy,
though unemployed at the moment, and it occurred to me that "I'm familiar
with Python, but not the version your entire established codebase is in"
wasn't a great thing to have on a resume.

Since it looks like the new formatting for print -- that is, print("Print
this stuff!") -- works fine in 2.7, I'm just getting myself used to doing
that from the beginning.

I went through the first four or five sections of the tutorial this
afternoon, with a few side trips into things that got me interested, and I
figure I'll do at least one more section after dinner.  I did find it
interesting that one of the first things I wanted to know turned out to be
an extremely common question:  "What's the Python equivalent to print_r()
from PHP?"  If any of you are familiar with PHP (I know at least a couple
of you seemed to be), you'll know that pprint() (which seems to be the most
common answer) isn't actually very close.  Its output isn't nearly as
readable.

For instance:  output of running print_r on a very short dictionary from
PHP:

Array
(
    [key3] => thing3
    [key2] => thing2
    [key1] => thing1
)

And running pprint on the same dict in Python:

{'key1': 'thing1', 'key2': 'thing2', 'key3': 'thing3'}


I finally decided that a good project would be building a quick function
that recreates the print_r function, and I got that working.  New function
output, in Python:

(
    [key3] => thing3
    [key2] => thing2
    [key1] => thing1
)


So at least I've figured out enough to do SOMETHING useful, which is
reassuring.

Anyway, thanks again to all of you, and I'm sure I'll be posting soon with
problems.


-Andy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130416/f4d16d09/attachment-0001.html>


More information about the Tutor mailing list