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 3

Go back to: General discussion

 Here is the forum for Exercise 3.

My code for this exercise looks like this.  I've spaced it so it is easier to read.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

# Excercise 3: Numbers And Math
# + plus, adds the value on either side
# - minus, subtracts the value on the right from the value on the left
# / slash, divides the value on the left by the value on the right
# * asterisk, multiplies the values on either side
# % percent, does modulus, divides the left hand operand by the right hand operand and returns the remainder.
# < less-than, checks if the value on the left is less than the value on the right
# > greater-than, checks if the value on the left is greater than the value on the right 
# <= less-than-equal, checks if the value of the left hand operand is less-than or equal-to the value of the right hand operand 
# >= greater-than-equal, checks if the value of the left hand operand is greater-than or equal-to the value of the right hand operand 

# This line prints the text I will now count my chickens
print "I will now count my chickens:"
 
# These next 2 lines print Hens and Roosters respectively, then calculate the amount of each.  for chickens there are 25+(30/6) or 30 Hens, while there are 100-25*3%4 or 97 Roosters.  I'm not sure how modulus works in python, or in true math either.
print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4
 
# This line prints the text I will now count my eggs.
print "I will now count my eggs:"
 
# This line represents some complex way of calculating how many eggs there are.  The answer is 7.
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
 
# This line asks the question is it true that 3+2 (5) is less than 5-7 (-2).
print "Is it true that 3 + 2 < 5 - 7?"
 
# This line does the calculation for the above question and determins that the answer to the above question is false.
print 3 + 2 < 5 - 7
 
# These two lines ask for the answers to 3=2 and 5-7, resulting in 5 and -2.
print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7
 
# This line acknowledges that the question of 5 < -2 is false.
print "Oh, that's why it's False."
 
# Another text line, that ask how about some more.
print "How about some more."
 
# These three lines ask if 5 and -2 are greater-than, greater-than-or- equal-to or less-than-or-equal-to and give the answers.
print "Is it Greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2

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

C:\Python27\python ex2.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

C:\Python27\

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

The line about hens and roosters, specifically (print "Roosters", 100 - 25 * 3 % 4) (more specifically, 100 - 25 * 3 % 4) bugs me, I have been bending my brain trying to figure out how that works. I've done some research on modulus (modulo) and i get the basic premise that left is divided by the right then displays the remainder, but I just can not seem to get to 97?

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

For 100-25*3%4. The 25*3 is evaluated first, resulting in 75. Then 75%4 is evaluated, resulting in 3. Then 100-3 is evaluated, resulting in 97!

Thanks for pointing out that % is mod. I was going to look for that later today, but the power of COMMUNITY saved me the time!

Niels Sprong's picture
Niels Sprong
Wed, 2011-02-02 22:53

wow, yes, true power in this community! im a bit behind but this is just so cool that people together come to the right answers on this forum!

Alexis Tsonis's picture
Alexis Tsonis
Thu, 2011-01-20 22:27

Hello everyone!I also have a question...
I'm trying to print the result of 3/2 and instead of 1.5 it prints 1.
Can anyone help me with that?

*****
I think i found it.I should have typed 3.0/2.0 to get result 1.5

Amene Katanda's picture
Amene Katanda
Fri, 2011-01-21 11:23

Yeah. try this too: 3.00000 /2 :-)

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

@Steve, Dang! is that all it is? i was expecting (sort of hoping now) for some sort of "black art", thanks!

brandon plemon's picture
brandon plemon
Fri, 2011-01-21 01:17

I was in the same boat... but I guess if you do the old school P.E.M.D.A.S it makes sense. Thanks guys

Saurabh Bansal's picture
Saurabh Bansal
Fri, 2011-01-21 11:58

3 AN 2 ARE OF THE TYPE INT(INTEGERS) SO 3/2 GIVES an integer(the answer is integer part of 1.5 i.e. 1)
whereas 3.0 is a floating(decimal) number division of such numbers give another decimal number...

Alexis Tsonis's picture
Alexis Tsonis
Fri, 2011-01-21 17:25

thanx!!

Toby Shaw's picture
Toby Shaw
Fri, 2011-01-21 12:13

PEMDAS? I was assuming it was BODMAS? Hmm... the MD/DM could cause problems.... any idea which it is?

Toby Shaw's picture
Toby Shaw
Fri, 2011-01-21 12:14

Actually BODMAS seems to be the UK version... I'm guessing it uses a US standard

Steve Phelps's picture
Steve Phelps
Fri, 2011-01-21 13:11

What does BODMAS stand for?

PEMDAS - Parentheses, Exponents, Multiplication or Division (in order from left to right), Addition or Subtraction (in order from left to right).

ZM L's picture
ZM L
Wed, 2011-01-26 10:13

Well B for bracket, O for order ( alternative to exponents) and D for division, M for multiplication and the rest you should know.

Pretty much the same.

brandon plemon's picture
brandon plemon
Fri, 2011-01-21 20:16

Yeah back in math class they taught us to say Please Excuse My Dear Aunt Sally to remember it then you just used the first letter for the function :).

brandon plemon's picture
brandon plemon
Fri, 2011-01-21 20:18

Toby you are completely right... it stands for Brackets Order Division Multiply Addition Subrtraction. Thats super cool, I have a feeling we are going to learn a lot of lil stuff like that during these courses and thats almost as fun as the course itself. Thanks guys

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

Seems very handy you only need to add decimals to one number within the computation to get a decimal output! Is this assumption accurate or are there cases where my laziness will bite me? :)

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

I have the same result as pborzel. To Alexis Tsonis, you basically have to remember that when dividing an int by an int you will only get the value before the decimal point. However if you divide a floating point number by another number you will get a fractional answer.

Connor Rowe's picture
Connor Rowe
Sun, 2011-01-23 04:33

a point I'd like to add, that to some people is probably not necessary, but which I just discovered (after thinking about it for a second, is that python does not round when it prints out an integer but instead it truncates at the decimal point.

nada A.S's picture
nada A.S
Sun, 2011-01-23 07:20

Hello guys :),

Please read this helpful tips about division operation in python.

http://www.python.org/dev/peps/pep-0238/

nada A.S's picture
nada A.S
Sun, 2011-01-23 07:31

Try the following code : it uses a true division operations to replace that ambiguous one .

from __future__ import division;
x=79
y=4
print x/y
print x//y

Trent Branson's picture
Trent Branson
Sun, 2011-01-23 11:17

if you divide two integers, python won't round, it will do floor division.
Floor division is when python simply python getting rid of the decimal
i.e.
3/4 evaluates to 0 not 1
or 8/3 evaluates to 2 not 3

Trent Branson's picture
Trent Branson
Sun, 2011-01-23 11:19

python 3 doesn't do floor division it convert it to a float.

Chris Maenner's picture
Chris Maenner
Sun, 2011-01-23 21:33

Just a FYI on how Python's interpreter handles the calculations and how the modulus operator works:

# prints the word "Roosters" and then calculates 100 - ((25 * 3) mod 4) = 97
print "Roosters", 100 - 25 * 3 % 4

# prints the following: 3 + 2 + 1 - 5 + (4 mod 2) - (1 / 4) + 6 = 6.75 (round to 7)
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

Javier Evans's picture
Javier Evans
Mon, 2011-01-24 08:21

I'm having trouble understanding how the answer of 6.75 would round up to 7. Since at this point we're working with integers wouldn't (1/4) just be computed to 0 (I tested this in python), or 6.75 be computed as just 6? Yet doing 1- 1 /4 in python returns 1....

Hasan Hasan's picture
Hasan Hasan
Wed, 2011-02-02 13:15

I keep coming back to this point. I also still don't understand how 6.75 rounds up to 7?

Michelle Chinese's picture
Michelle Chinese
Wed, 2011-02-02 15:04

You know that question in the exercise where you count the eggs, and it tells you 7 even though the right answer is 6.75? Here's the original problem:

print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

If you typed it into your .py file exactly like that, Python tells you it's 7 because the *way you've written the numbers* indicates to Python that you don't *want* to see decimal points, you just want it to round to the nearest whole number.

But if you typed that same line like this instead, adding a floating point to each number:

print 3.0 + 2.0 + 1.0 - 5.0 + 4.0 % 2.0 - 1.0 / 4.0 + 6.0

...that indicates to Python that you do care about seeing the decimals, so it'll return 6.75.

In other words, adding floating points to your numbers is just a way to "toggle" Python's settings for either displaying a whole number OR displaying a number with decimal points.

So: if the numbers you enter have decimals (even if the decimal is just zero like I did above, such as 3.0) Python will give you an answer with decimals (like 6.75 or 0.33333333). If the numbers you enter *don't* have decimals, Python will give you a rounded answer (like 7 or 0) because it assumes you don't care about decimals.

In your example, 1 - 1/4 in Python would return 1 because you didn't indicate - using floating points - that you want to see the decimal. Behind the scenes, it calculated 1 + 0.25 = 1.25, and then it showed you a rounded version: 1. If you had calculated 1.0 - 1.0/4.0, instead it would have given you the right (not rounded) answer.

Hasan Hasan's picture
Hasan Hasan
Wed, 2011-02-02 16:55

Thanks Michelle! Great breakdown - I get it now :)

ZM L's picture
ZM L
Mon, 2011-01-24 10:36

Pretty easy exercise however, the extra credit mentions about "floating number" and upon searching them something called "double" also came up and they look so similar. Both having decimal points.

What is the difference between float and double?

M. Volz's picture
M. Volz
Mon, 2011-01-24 16:30

A double is a specific kind of floating point number, see http://en.wikipedia.org/wiki/Primitive_data_type#Overview

ZM L's picture
ZM L
Mon, 2011-01-24 16:52

hmm single precision number (float) and double precision number. confusing. O well, thinks it's not important 4 now. But would be good to know.

Rodrigo Zerbini's picture
Rodrigo Zerbini
Tue, 2011-01-25 20:14

@pborzel

Thanks for the explanation about the meaning of the % symbol!

I'm starting now exercise 4. Let's go.

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

Output:

>>>
I will now count my chickens:
Hens 30.0
Roosters 97.0
Now I will count the eggs:
6.75
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5.0
What is 5 - 7? -2.0
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
>>>

Extra credit script

# this line multiplies 12 months times 300 dollars then
# divides that sum by 365 days
print (12 * 300) / 365
print (12 * 300) / 365.0 #floating point

output:

>>>
9
9.86301369863
>>>

Sid's picture
Sid
Fri, 2011-01-28 14:43

I'm learning a lot about python I am very contant with exercises

Michelle Chinese's picture
Michelle Chinese
Sat, 2011-01-29 23:39

You could give me 50 years on a deserted island, with nothing to do but figure out what the % does, and I still wouldn't have figured it out. Thanks Steve & everybody!

jodischneider's picture
jodischneider
Sun, 2011-01-30 23:12

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
=============
$ 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

====
Did EC 1-5

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

Input:
print "I will now count my chickens."

print "Hens", 25.0 + 30.0 / 6.0
print "Roosters", 100.0 - 25.0 * 3.0 % 4.0

print "Now I will count the eggs:"

print 3.0 + 2.0 + 1.0 - 5.0 + 4.0 % 2.0 - 1.0 / 4.0 + 6.0

print "Is it true that 3 + 2 < 5 - 7?"

print 3.0 + 2.0 < 5.0 - 7.0

print "What is 3 + 2?", 3.0 + 2.0
print "What is 5 - 7?", 5.0 - 7.0

print "Oh, that's why it's False."

print "How about some more."

print "Is it greater?", 5.0 > -2.0
print "Is it greater or equal?", 5.0 >= -2.0
print "Is it less or equal?", 5.0 <= -2.0

print "Hey! Let's do a random equation! Won't that be fun? 30 * 6=", 30 * 6

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

I am currently teaching an Introductory to Algebra class and we've been over and over PEMDAS. Despite that, I was still having a hard time getting what the % symbol was doing in that sequence. Nice to have the benefit of crowdsourcing the same question. On to Exercise 4...I may have the first week done this evening! About time, too...