So I got turned on to the idea of learning Python the hard way at, well, http://learnpythonthehardway.org/book/ and as directed by the book I am now trying to figure out a few things that I don't understand. This post has ended up being fairly long, and I hope that it's not bothersome. I figure this is exactly what this forum is for, but I can envision some folks taking the time to tell me that I should choose a different book to learn from if this one is going to lead me to ask such involved questions of others.
Anticipating that suggestion, I'll say now that I like the way that these lessons are challenging me to figure things out on my own through careful analysis, and to ask about what I can't figure out. It forces me to articulate my thoughts, and I think that's a good thing.
Anywho . . .
The following code (as seen in the lesson here http://learnpythonthehardway.org/book/ex3.html:
print "I will now count my chickens:"
print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4
print "Now I will count the eggs:"
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
print "Is it true that 3 + 2 < 5 - 7?"
print 3 + 2 < 5 - 7
print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7
print "Oh, that's why it's False."
print "How about some more."
print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2
Yields this:
python ex3.py
I will now count my chickens:
Hens 30
Roosters 97
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5
What is 5 - 7? -2
Oh, that's why it's False.
How about some more.
Is it greater? True
Is it greater or equal? True
Is it less or equal? False
Again per the instructions, I'm adding comments to my code that describe what I'm learning by typing each line. Stuff like, what effect quotes have or how python deals with math. Here's what I'm not clear on. I can't seem to figure out the order of operations that python is going through for this code: print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
The first half of the modulus (that is, what is in front of the % symbol . . . just to clarify my way of reference) is easy peasy. The second half is confusing me. The result of the whole string ("string" may or may not be how I should refer to that line) is 7. I understand how the % works, but not the order of operations for 2-1/4+6.
It seems like it should be super obvious. I know the order for 100-25*3%4 is 100-((25*3)%4). Could someone add parentheses to the line in question to clarify this point?
Second, I'm assuming that the commas after a printed sentence are not decorative, and that they let python know that what follows is another instruction, yes?
I had more questions, but writing this post led me to the answers. Sweet!
Any help shall be greatly appreciated. Thanks!
Anticipating that suggestion, I'll say now that I like the way that these lessons are challenging me to figure things out on my own through careful analysis, and to ask about what I can't figure out. It forces me to articulate my thoughts, and I think that's a good thing.
Anywho . . .
The following code (as seen in the lesson here http://learnpythonthehardway.org/book/ex3.html:
print "I will now count my chickens:"
print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4
print "Now I will count the eggs:"
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
print "Is it true that 3 + 2 < 5 - 7?"
print 3 + 2 < 5 - 7
print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7
print "Oh, that's why it's False."
print "How about some more."
print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2
Yields this:
python ex3.py
I will now count my chickens:
Hens 30
Roosters 97
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5
What is 5 - 7? -2
Oh, that's why it's False.
How about some more.
Is it greater? True
Is it greater or equal? True
Is it less or equal? False
Again per the instructions, I'm adding comments to my code that describe what I'm learning by typing each line. Stuff like, what effect quotes have or how python deals with math. Here's what I'm not clear on. I can't seem to figure out the order of operations that python is going through for this code: print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
The first half of the modulus (that is, what is in front of the % symbol . . . just to clarify my way of reference) is easy peasy. The second half is confusing me. The result of the whole string ("string" may or may not be how I should refer to that line) is 7. I understand how the % works, but not the order of operations for 2-1/4+6.
It seems like it should be super obvious. I know the order for 100-25*3%4 is 100-((25*3)%4). Could someone add parentheses to the line in question to clarify this point?
Second, I'm assuming that the commas after a printed sentence are not decorative, and that they let python know that what follows is another instruction, yes?
I had more questions, but writing this post led me to the answers. Sweet!
Any help shall be greatly appreciated. Thanks!