Learning Python

No need to fear, I still love PHP way more.

I’m working on a command-line application and decided to use Python because it seems to be a little easier to work with for CLI purposes. I needed to teach myself some key things about Python by building a useless toy that does the following:

  • Incrementally repeat a user-defined symbol
  • Once fully incremented, do the same in reverse (count back)
  • The inputs must have default values

This is what I came up with:

#!/usr/bin/python def init():         symbol = raw_input("Symbol [*]: ")         times = raw_input("Times [100]: ")                 if(symbol == ""):                 symbol = "*"                 if(times == ""):                 times = 100                 return [symbol,int(times)] def show(symbol, times, forward):         print symbol*times                 if(forward):                 return times + 1         else:                 return times - 1 def go(data):         i = 0                 while i <= data[1]:                 i = show(data[0], i, True)                 while i >= 0:                 i = show(data[0], i, False) go(init())

Initial Reactions

Indentation errors? WTF? What language is going to force a user to indent lines a certain way? Am I missing something here? How can this possibly be good at all?

Working without braces and semicolons seems a bit awkward too. To be fair, I don’t necessarily thing this is bad. I’m just not used to that kind of syntax. In my opinion, using indentation alone to define code blocks makes the code much harder to read. I also think of semicolons as another important feature of syntax which not only tells the interpreter where to stop parsing an expression, but also offers the coder a visual clue where the expression stops.

I also hear that it has lambdas, much like JavaScript. This gives a developer the ability to be incredibly expressive while coding because functions can be used as ways to store code for later use, but also as values to be passed as parameters to other functions.

Overall, I’d have to say that the language seems fun to program. It’s not hard to pick up at all (this was the result of less than a half our, most of which was consumed by reading). I can tell already the language is very CLI-friendly. Although PHP does CLI-applications well, I don’t feel that it’s the best tool for the job.

This entry was posted on Sunday, October 7th, 2007 at 11:16 am and is filed under Code, Python. You can leave a response, or trackback from your own site.

What do you have to say?

Site Stuff

Pages

Projects

Archives

Categories