Monday, September 12, 2011

LPTHW - Exercise 15

In this exercise, we will read the contents of a file and print them to the console.



Notes:

We use the open(filename) function to get a file handle, and then we use the read() function of the file handle object to read the contents of the file as a string.

I am sure there is a different way to read a binary file, and I am also sure there should be a way to buffer the input in case the file is very large, but read is the simplest way to get the job done. This is much better than Java where we have to jump through tons of hoops to read a file.

LPTHW - Exercise 14

In this exercise we use a value from the command line input to prompt the user.



Questions:

1. The unpacking thing seems to basically work with lists on the RHS and a bunch of variables on the LHS. Not sure if that is right. Will have to find out.

LPTHW - Exercise 13

In this exercise we will learn how to accept command line arguments.



In the script above in the first line we import the 'argv' variable from the 'sys' module. This variable will hold the name of the script and all command line arguments given to it.

We unpack these values and assign them to the variables on the LHS.

Questions:
1. Does importing argv make is available, or does it also turn on the function of accepting command line arguments?

2. Here we assign the values to the variables on the LHS. However, we need to be sure that the command line parameters and the number of variables are the same. This is not always possible, especially when we do not know how many values the user will enter. Perhaps, this issue will be dealt with in a later exercise. I will leave it as is for now, but will try to answer it if it is not dealt with in a later exercise.

LPTHW - Exercise 12

In this exercise, we will provide some text to the raw_input() function as a parameter. This text will be used as the prompt. This is much easier than the previous exercise, where we first printed something (as a prompt) and then asked for input using the raw_input() function.



Extra Credits:

We can read the documentation of any function using the pydoc command. To quit the documentation, press 'q' on the command prompt.

LPTHW - Exercise 11

In this exercise we are going to accept input from the user.



Notes and extra credits:

When we print something with the print statement, it normally ends with a newline. We can prevent the new line by ending the print statement with a ',' as shown in the example above.

The raw_input() function is used to read input from the console. It will read one line of input, and pass it to the program with the ending newline stripped out.

It seems we can load the 'readline' module to provide some elaborate editing and history features.

In the output of the above program, all the instances of %r are replaced by the output of running __repr() on the arguments. The output is always delimited with ''. Since the height also uses a ' within it, it must be escaped.

LPTHW - Exercise 10

In this exercise, we look at some more printing and escape characters. The "\" is the escape character in Python (just like in many other programming languages).



Extra Credit:

Escape characters are used to change the meaning of the character following it. For example \t will print the 'tab' character instead of 't'. All escape sequences supported by Python are listed on this page from the Python docs.

I tried replacing the triple double quotes in the example above with triple single quotes. When I ran the example, it ran just like the one with triple double quotes. I am not sure if there is any difference between them. Maybe if we use triple single quotes, then we can embed triple double quotes in them.

%s is better for printing because it is replaced by the String value, whereas %r is replaced by what is returned by the __repr function of the object (usually this is surrounded by double quotes).

LPTHW - Exercise 9

In this exercise, we executed some more printing code. The new concept I learned in this exercise, is that we can use triple quotes to represent a string which can span multiple lines.