Wednesday, August 31, 2011

LPTHW - Exercise 8

Completed exercise 8.

Nothing much to report in terms of thought process, etc for this exercise.

Thursday, August 4, 2011

LPTHW - Exercise 7

In exercise 7, we learn a bit more about printing. I typed the program, added some comments to explain what it does and got it to work.



Some observations:
  • Ending a print statement with a ',' as we see on line 29, prevents a newline from being printed
  • Multiplying a String, concatenates the String with itself, that many times (as seen on line 11)

LPTHW - Exercise 6

In the last exercise, we learned about variables and also introduced String formatting. In this exercise, we learn more about Strings and text.

First, I typed the program as is, and got the expected output.



Extra Credit:

Next, I commented the code, and also counted the number of times we put a String inside a String (it is indeed four times).



Some Observations:
  • When we concatenate multiple Strings a space character is not added between them, the way print adds it, when we try to print multiple Strings. So if we want to print multiple Strings without spaces between them, then we should concatenate them with the + operator and print the concatenated result.
Some questions:

LPTHW Exercise 5

LPTHW's Exercise 5 is a nice introduction to variables and printing.

I typed the program as is (along with some comments to explain what the program does), and got the expected output.



Extra Credits:

Next I replaced all the variables which start with 'my' such that the names do not contain 'my'. Got that code to also do it's work.



Then I looked up the documentation for String formatting in Python, and learned a few interesting things.
  • Formatting is actually facilitated by a function called '%' in Python String. So basically when we type "hello %s" % name what we are actually doing is calling the '%' function of the String "hello", and giving it the variable 'name' as an argument. If we need to supply only 1 argument, then we can give it a single variable, however, if we need to supply multiple arguments, then we must use a tuple. Like this "hello %s %s" % (first_name, last_name)
  • There are many formatting characters in Python. I checked out the meaning of %r as the exercise mentions. %r will print the String representation of any Python object, by calling the objects repr method. This is interesting. So this is how Python gets a String representation of an object. In Java this is done by invoking the function toString()
Next I created some variables which convert Pounds to Kilograms, and Inches to Centimeters, and that too works properly.


Wednesday, August 3, 2011

LPTHW - Exercise 4

Starting with exercise 4, where we learn about variables. I typed in the code, and got an error when I tried to run it.



Error:
File "ex4.py", line 11
print "There are" only", drivers, "drivers available."
^
SyntaxError: invalid syntax

Solution: The problem is the double quote after 'are'

Here is the correct solution:



Here is the output:

There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today
We need to put about 3 in each car.


Questions:
  1. An interesting observation : When we give multiple values to print (separated by commas), a space is automatically put in between them. This is really nice, but what if we do not want to the space?

Observations:
  1. In python we do not have to give the type of variables (because Python is dynamically typed), and we do not need to use any special keyword like var, or def before declaring a variable,. This is in contrast to other languages which normally require something before a variable name.

Extra Credit:

The error described on the page:
Traceback (most recent call last):
File "ex4.py", line 8, in
average_passengers_per_car = car_pool_capacity / passenger
NameError: name 'car_pool_capacity' is not defined
This means that a variable of the name 'car_pool_capacity' had not yet been defined. This could happen if the author either forgot to define the variable, or did a typo when defining it. In this case it looks like a typo because the actual variable is 'carpool_capacity'.

Why do we use 4.0 instead of 4 ?
4.0 is a floating point number, while 4 is an integer. Floating point numbers can represent fractions, while integers cannot.

With some experimentation with Python, I realized that if we do operations with all integers, then the result will also be an integer. If the operation of a division operation and the result is actually a fraction, then it will be truncated to yield an integer.

However, if even one of the numbers is a floating point number, then the rest will be promoted to floating point numbers to yield a floating point number.

i = 4
j = 3
i/j
answer: 1

i = 4.0
j = 3
i/j
answer: 1.3333

i = 4
j = 3.0
i/j
answer: 1.3333

Tuesday, August 2, 2011

LPTHW - Exercise 3


Stuff I learned:
  • Operator precedence
  • The print function can take multiple arguments, separated by commas

Monday, August 1, 2011

LPTHW - Exercise 2

I did exercise 2 from the book.



Extra Credit:
  • I was right about the '#' character. It is used to comment out single lines
  • Read the file backwards comparing it to the original program... no errors so far
  • I read what I typed out aloud, and still no errors...

LPTHW - Exercise 1

I completed exercise 1. The code is embedded below.



Stuff I learned:
  1. In Python we use the 'print' function to print to the console
  2. In Python function calls need not have () like in Java
  3. In Python we use the '#' character to comment a single line. The C style comment '//' does not work in Python
  4. We do not have to end a statement with a ';' or any delimiter in Python
  5. Python Strings can be delimited by double quotes as well as single quotes
  6. A double quoted string can contain single quotes
  7. It is not compulsory to have a main method in Python

LPTHW Exercise 0

In this exercise, we install Python. I am using Ubuntu 10.04, so I moved straight to the "Installing on Linux" section.
  1. I already have GEDIT
  2. Changed the defaults in Gedit as suggested in the book
  3. Found my terminal program
  4. Not yet putting my terminal in the dock
  5. Opened my terminal program
  6. I already have Python 2.6.5 installed
  7. Hit CTRL-D and I am out of the Python shell
  8. I am at the command prompt now
  9. Made a directory
  10. Changed into it
  11. Created a README.txt file in the directory
  12. Went to the terminal and back to the browser using just the keyboard
  13. I am back in the terminal and can see the directory. I also listed it's contents
Besides the things suggested in the book, I am planing to make this a git repository. I will either push all the exercises to GitHub, or paste them in Gists.

Hello Python

I did a little work in Python a very long time back, and grasped some key concepts of Python. Since then I have forgotten a lot of the stuff I learned back then, and feel like I need to refresh my knowledge.

What better time to refresh the knowledge than before Pycon 2011, Pune.

I just enrolled in the Introduction to Python online course at DIY Computer Science. This course is based on Zed Shaw's book "Learn Python the Hard Way 2.0".

This course is based on a peer learning model. Everyone who registers must maintain a blog which will serve as their learning journal (that is what this blog will be). The course consists of some reading and 52 exercises. Participants do the exercises and blog about their notes , reflections, and code. Then they submit their blog posts in the appropriate activity sections. If anyone has questions, they can ask them in the course forum.

So that's it. I am quite excited to refresh my Python skills...