This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Learn Python the Hard Way

My recent threads

You haven't posted any discussions yet.

Recently updated threads

Exercise 2

Go back to: General discussion

 Here is the forum for Exercise 2.

My code for this exercise looks like this.  I've spaced it so it is easier to read.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 # A comment, this is so you can read your program later.
2 # Any thing after the # is ignored by python.

3 print "I could have code like this." # and the comment after is ignored

4 # You can also use a comment to "disable" or comment out a piece of code:
5 # print "This won't run."

6 print "This will run."

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is the output.

C:\Python27\python ex2.py
 I could have code like this.
This will run.
C:\Python27\

pborzel's picture
pborzel
Thu, 2011-01-20 21:45

After seeing the # commenting, i went back and added notes (comments) to the other exercises.

Steve Phelps's picture
Steve Phelps
Thu, 2011-01-20 21:56

So you could use the # in front of a line of code when trying to debug something? Sort of a way to turn a line on or off?

pborzel's picture
pborzel
Thu, 2011-01-20 22:09

yup, it's magical how well it works, i remember doing it in BASIC, oh so many years ago. ;)

michael chang's picture
michael chang
Sat, 2011-01-22 17:33

maybe I am dense, why does reading things backwards help?

M. Volz's picture
M. Volz
Sat, 2011-01-22 17:51

It's a popular proofreading technique, see http://www.google.com/search?q=reading+backwards+proofreading

But after googling that, I discovered this paper that says it's not effective: http://eric.ed.gov/ERICWebPortal/search/detailmini.jsp?_nfpb=true&_&ERIC...

So if you find it's really not helping you, I'd say skip it :).

michael chang's picture
michael chang
Sat, 2011-01-22 18:21

Hm, interesting! Perhaps it's not as useful on the first couple passes when the code/content is fresh but is useful when you've gone over it many times and start going into "skim" mode. Thanks for the links. :)

andy gomer's picture
andy gomer
Sat, 2011-01-22 21:04

my code looks exactly like pborzel's code so I think I am doing something right :-). I do have a question though. In other programming languages like C++ and Java you can have multi line comments. They tend to start with /* and end with */ is there any way to do this in python?

M. Volz's picture
M. Volz
Sat, 2011-01-22 21:30

The answer is ... sort of. The way to do this is to start and end with three double quotes i.e. """my multi-line comment""". This is also how you write multi-line strings. However, it's not a "real" comment in that it is interpreted by the compiler, see docstrings http://www.python.org/dev/peps/pep-0257/

Naku Mayo's picture
Naku Mayo
Wed, 2011-01-26 03:16

For exercise 2, here is my code:

# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.

print "I could have code like this." # and the comment after is ignored

# You can also use a comment to "disable" or comment out of piece of code:
# print "This won't run."

print "This will run."

Output:
$ python ex2.py
I could have code like this.
This will run.

The # for comments is common especially with bash .sh .pl and other scripting language. It makes it so easy to read and following through as to what that particular piece of script is doing.

Red Olaf's picture
Red Olaf
Wed, 2011-01-26 22:20

Code:

# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print "I could have code like this." # and the comment is ignored
# You can use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run."

Output:

Python 2.7.1 (r271:86882M, Nov 30 2010, 09:39:13)
[GCC 4.0.1 (Apple Inc. build 5494)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
I could have code like this.
This will run.
>>>

jodischneider's picture
jodischneider
Sun, 2011-01-30 22:49

# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.

print "I could have code like this." # and the comment after is ignored

# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."

print "This will run."

=============
$ python ex2.py
I could have code like this.
This will run.

===
Did EC 1-4

Tyler Brown's picture
Tyler Brown
Mon, 2011-01-31 04:17

Input:

# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.

print "I could have code like this." # and the comment after is ignored

# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."

print "This will run."

Dorene DeMars's picture
Dorene DeMars
Sun, 2011-02-06 04:11

My input looked like the others listed here, so I won't resubmit it. I placed my editor (gedit) directly over the pdf page of the exercise and read it aloud as I was typing (I live alone, nobody here but the cats will think I'm crazy). I nailed this one on the first try.