LPTHW - Day 13 Strings and Text

No Day 12 , I fall asleep on my lappy while going half-way through exercise 6 in LPTHW .

Multi-variables formatted string in Python 2.x
>>> print "No %s, No %s" % ('Pain', 'Gain')
No Pain, No Gain

In  Python3, using positional (ordinal) arguments
>>> print ("No {0}, No {1}".format('Pain', 'Gain'))
No Pain, No Gain

or named arguments, which I loves alot since it make the code readable at a glance.
>>>print ("No {first}, No {second}".format(first='Pain', second='Gain'))
No Pain, No Gain

While in PHP
php> printf("No %s No %s", 'Pain', 'Gain');
No Pain No Gain

String Concatenation in Python
>>> print "foo"+"bar"
foobar

In Python 3
>>> print ("foo"+"bar")
foobar

Meanwhile in PHP
print "foo" . "bar";

LPTHW - Day 07 Comments And Pound Characters

There is no day 6, my house was "attacked" by apples, oranges, and pear.

Doing the second exercise in LPTHY book. I learned that the symbol # can be named differently depending on where or who you are. And from where I came from, according to Wikipedia, we called it hex? Seriously, I think is hash instead.

Just remember, to American and some Canadian, is pound and to everyone else, is hash. But in the book, Zed uses the name octothorpe, which is commonly used in Bell Lab's telephone system. By the way, you pronounce it as "ok-tuh-thawrp"

In the extra credit section, we were asked "review each line going backwards". Why backwards? As we normally don't read backward, it will slows you down and help you to focus and catch mistakes.

Also, we were asked to "read what you typed above out loud, including saying each character by its name.". By doing so, you can easily catch mistakes and reinforce what you've coded. To make this even more fun, why not ask someone else to read for you? Let's get our good old espeak.
$ sudo apt-get install espeak
$ espeak -f ex2.py

If you notice, learning a new programming language is like learning to read and write a foreign language. All the foreign language learning techniques can be applied here as well.

LPTHW - Day 04 A Good First Program

tl;dr : print is a statement in 2.x but in 3.x, print is a function.

As a developer with some experience with development, one will have the tendency of skipping the "hello, world" example. Nope, not going to happen here, I am going to do everything step by step. As Shunryu Suzuki once said,
"In the beginner's mind there are many possibilities, in the expert's mind there are few."
Being experienced and having an expert mentality make it harder to absorb new ways of doing stuff and unlearn all the bad habits. Do it like a beginner and no cut-and-paste.

Typed and ran the everything in below.

ex1.py
print "Hello World!"
print "Hello Again"
print "I like typing this"
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'

Works fine except when I ran it with Python3 and got this error message.
$ python3 ex1.py
File "ex1.py", line 1
print "Hello World!"
^
SyntaxError: invalid syntax

It seems to work in 2.6 but failed in 3 ? So what's going on here? According to the new features in Python3, print which used to be a statement in 2.x but now is a function in 3.x. Why ? According to Georg Brandl's rationale, as I read it, a function behaviours are easier to extend and overwrite compare to a statement.

So, change the Ex1 by adding a parenthesis around the strings we want to print as follows and save it as ex1-python3.py. Then you will get errorless output.

ex1-python3.py
print("Hello World!")
print("Hello Again")
print("I like typing this")
print("This is fun.")
print('Yay! Printing.')
print("I'd much rather you 'not'.")
print('I "said" do not touch this.')

On a side note. There is no Day 3. Shits happens.

What if you want to stick to the Python 3 print function but still want to run it using version 2.x ? You can do this using future module which let's backport certain Python 3 features to Python 2. For our example for using print() function, we just need to import this function to our code as shown below. Thanks to kamal for this tip.
from __future__ import print_function

LPTHW - Day 02 Installing Python 2 and 3 in Ubuntu 10.10

By default, the Python version in Ubuntu 10.10 is 2.6. Unless necessary, upgrading to version 2.7 is is not compulsory. But still, we want to explore version 3. Installation procedure as follow:
$ sudo apt-get install python3-minimal
$ python3

$ python3 -V
Python 3.1.2

Default python installation
$ python -V
Python 2.6.6

LPTHW - Day 01 Picking Python as New Learning Programming Language

If you have been reading the book "The Pragmatic Programmer", both authors suggested that as a programmer who wants to expand their knowledge, you should "learn at least one new language every year". I will give it another shot by trying to really settle down and learn a new programming language this year. No more lame excuses.

So which language? Python. Why Python? Because it's not PHP. Why not Ruby? Because P > R ! Joke aside, either one is okay.

I will start with Zed Shaw's "Learn Python the Hard Way" as well as "The Hitchhiker’s Guide to Python!" to get the fundamental right. Later, hopefully will try to create something non-web based from it to further my understanding of the language and the ecosystem.

Many years ago, Peter Norvig wrote that you need ten years to master a programming language. Malcolm Gladwell in his book Outliner further support this claim by narrowing it down to 10,000 hours for anyone to become a expert in any field. How long it going to take me to become a Python expert ? 54-plus years. Yes, fricking 54-plus years if I spend half an hour a day to practice and using it everyday. Pretty damn long right?

But wait. Who is a Python expert? How do you define a person who is an expert in Python? And the most important question is, does it really matters ? No. Why not just enjoy the learning process and have fun creating, exploring, and experimenting. Make lots of mistake and fails miserably. No one is going give a damn whether you're an expert or not.

By the way, happy new year.