Nothing much to report in terms of thought process, etc for this exercise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Next, I commented the code, and also counted the number of times we put a String inside a String (it is indeed four times).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Starting with exercise 4, where we learn about variables. I typed in the code, and got an error when I tried to run it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Solution: The problem is the double quote after 'are'
Here is the correct solution:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I completed exercise 1. The code is embedded below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In this exercise, we install Python. I am using Ubuntu 10.04, so I moved straight to the "Installing on Linux" section.
I already have GEDIT
Changed the defaults in Gedit as suggested in the book
Found my terminal program
Not yet putting my terminal in the dock
Opened my terminal program
I already have Python 2.6.5 installed
Hit CTRL-D and I am out of the Python shell
I am at the command prompt now
Made a directory
Changed into it
Created a README.txt file in the directory
Went to the terminal and back to the browser using just the keyboard
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.
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...