Due Wednesday, February 9, 2017, before midnight
Late penalty: Fri 5%, Sat 10%, Sun 100%
answers1and2.txt
: Your typed answers to the first two questions.
demorgan.py
quadratic.py
tenperline.py
In python, how big a number can be stored in
a float
? For example, 2.0**100
evaluates to 1.2676506002282294e+30
,
but 2.0**10000
gives an overflow error. Run IDLE,
and experiment to find the biggest exponent n
such
that 2.0**n
evaluates without overflow.
Consider this code:
root2 = 2**0.5 two = root2 * root2 shouldBeTrue = (two == 2) print shouldBeTrueType it into IDLE. What is printed? Why?
Extend the above code, with these two lines:
zero = two - 2 print zeroType them into IDLE. What is printed? Why?
Demorgan's theorem states that, if A
and B
are booleans, the following
will both always be True
:
Prove it using python. To do this, write a programnot (A and B) == (not A) or (not B)
not (A or B) == (not A) and (not B)
demorgan.py
with code like this:
print "A:", A, "B:", B, "not(A and B)==(not A) or (not B):", not(A and B)==(not A) or (not B)and repeat this code, each time setting
A
and B
to one of the four possible
combinations of True
and False
.
Repeat for the second part of DeMorgan's theorem.
Write a program quadratic.py
that accepts three
numbers a, b, and c, and prints
the real roots of the quadratic polynomial
ax2+bx+c. Your program should be robust: it
should not crash. Instead, print messages indicating how many
real roots exist (there may be no real roots).
Crash course in quadratic equations:
Here is the quadratic equation: ax2+bx+c .
Compute the discriminant:
d = b2 - 4ac
Don't forget to use *
to multiply values!
If d is negative, there are no real roots.
If d is zero, there is one root:
x = -b / (2a) .
If d is positive, there are two roots:
x1 = (-b - d0.5) / (2a) , and
x2 = (-b + d0.5) / (2a).
Don't forget to use **
to compute an exponent.
(Bonus 10%):
Write a program tenperline.py
that prints the
numbers 1
through 100
(inclusive), with ten integers per line. Method: use a
for-loop, and print each number with a trailing comma (to avoid
advancing to a new line). To advance to a new line,
(print
by itself). Do this when the number's
remainder, on division by 10, has a specific value. Use
number % 10
to get that remainder.