Giving Python Another Shot

2008-11-11 15:59 - Programming

I've been introduced to and worked with Python in dribs and drabs in the past. I've read plenty about it, or that mentioned it. I'm somewhat (and growing more, by the day) familiar with its advantages. And I'm interested.

Thing is, I'm a web developer. The vast majority (though definitely not all) of things I program are web pages. For a web page, 99% of the input comes from a database or form submission, and 99% of the output goes to a database or HTML page. And it's all just strings. In PHP, the type conversion is handled automatically for you, and there's thousands of times I find that very useful for each time I have even a minor issue with it. In Python, not so. Thus:

$ php -r 'print "5"+4; print "\n";'
9
$ python -c 'print "5"+4'
Traceback (most recent call last):
  File "", line 1, in 
TypeError: cannot concatenate 'str' and 'int' objects

This was one of the big things that put me off, when last I tried to get in bed with Python. It truly is the little things. The other is the module system. While I can easily find every function defined in the PHP manual, that's much less true with Python.

I am working on the Project Euler problems, as a tangible thing to "do" as I give learning Python another shot. I'm reading Dive Into Python and Thinking In Python (among others), but I learn by doing much better.

So I got to a question asking to count the first-of-the-month days that fell on a Sunday in the 1900s. I decided to "cheat" by simply construction date objects, and checking their day-of-week property. I managed to google up the Python date reference and write the code in a flash. But when I tried it:

Traceback (most recent call last):
  File "scratch.py", line 4, in 
    d=date(year, month, 1)
NameError: name 'date' is not defined

I'm pretty sure I need to import something, but it's not at all clear what. If I look hard enough, I see that the section of the manual that contains this one is talking about "the datetime module", so from that I managed to figure it out. But this is another annoying step. One of the things that turned me off, the first time. I've hit it again, but I'm doing my best to really give it the old college try, and see where it gets me.

Comments:

No comments!

Post a comment:

Username
Password
  If you do not have an account to log in to yet, register your own account. You will not enter any personal info and need not supply an email address.
Subject:
Comment:

You may use Markdown syntax in the comment, but no HTML. Hints:

  • An empty line between text will create a paragraph boundary.
  • Use angle braces around a plain URL to auto-link it: <http://www.example.com/>.
  • Use this format to create a link with different text showing: [An Example](http://www.example.com/).
  • Use backticks (``), not leading spaces to enclose a code block.

If you are attempting to contact me, ask me a question, etc, please send me a message through the contact form rather than posting a comment here. Thank you. (If you post a comment anyway when it should be a message to me, I'll probably just delete your comment. I don't like clutter.)