Completed exercise 8.
Nothing much to report in terms of thought process, etc for this exercise.
Wednesday, August 31, 2011
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:
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:
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.
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.
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" % namewhat 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
reprmethod. This is interesting. So this is how Python gets a String representation of an object. In Java this is done by invoking the functiontoString()
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:
Observations:
Extra Credit:
The error described on the page:
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
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:
- 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:
- 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):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'.
File "ex4.py", line 8, in
average_passengers_per_car = car_pool_capacity / passenger
NameError: name 'car_pool_capacity' is not defined
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:
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...
Subscribe to:
Posts (Atom)