1.
What do these expressions evaluate to?
3 == 3
3 != 3
3 >= 4
not (3 < 4)
Solution.
- True
- False
- False
- False
3 == 3
3 != 3
3 >= 4
not (3 < 4)
not
operator.a > b
a >= b
a >= 18 and day == 3
a >= 18 or day != 3
Mark | Grade |
---|---|
>= 90 | A |
[80-90) | B |
[70-80) | C |
[60-70) | D |
< 60 | F |
def grade(mark):
if mark >= 90:
return "A"
else:
if mark >= 80:
return "B"
else:
if mark >= 70:
return "C"
else:
if mark >= 60:
return "D"
else:
return "F"
mark = 83
print( "Mark:", str(mark), "Grade:", grade(mark))
import turtle
def drawBar(t, height):
""" Get turtle t to draw one bar, of height. """
t.begin_fill() # start filling this shape
if height < 0:
t.write(str(height))
t.left(90)
t.forward(height)
if height >= 0:
t.write(str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.left(90)
t.end_fill() # stop filling this shape
xs = [48, -50, 200, 240, 160, 260, 220] # here is the data
maxheight = max(xs)
minheight = min(xs)
numbars = len(xs)
border = 10
tess = turtle.Turtle() # create tess and set some attributes
tess.color("blue")
tess.fillcolor("red")
tess.pensize(3)
wn = turtle.Screen() # Set up the window and its attributes
wn.bgcolor("lightgreen")
if minheight > 0:
lly = 0
else:
lly = minheight - border
wn.setworldcoordinates(0-border, lly, 40*numbars+border, maxheight+border)
for a in xs:
drawBar(tess, a)
wn.exitonclick()
findHypot
. The function will be given the length of two sides of a right-angled triangle and it should return the length of the hypotenuse. (Hint: x ** 0.5
will return the square root, or use sqrt
from the math module)is_even(n)
that takes an integer as an argument and returns True
if the argument is an even number and False
if it is odd.from test import testEqual
def is_even(n):
if n % 2 == 0:
return True
else:
return False
testEqual(is_even(10), True)
testEqual(is_even(5), False)
testEqual(is_even(1), False)
testEqual(is_even(0), True)
is_odd(n)
that returns True
when n
is odd and False
otherwise.is_odd
so that it uses a call to is_even
to determine if its argument is an odd integer.from test import testEqual
def is_even(n):
if n % 2 == 0:
return True
else:
return False
def is_odd(n):
if is_even(n):
return False
else:
return True
testEqual(is_odd(10), False)
testEqual(is_odd(5), True)
testEqual(is_odd(1), True)
testEqual(is_odd(0), False)
is_rightangled
which, given the length of three sides of a triangle, will determine whether the triangle is right-angled. Assume that the third argument to the function is always the longest side. It will return True
if the triangle is right-angled, or False
otherwise.x
is equal or close enough to y
, they would probably code it up asif abs(x - y) < 0.001: # if x is approximately equal to y
...
from test import testEqual
def is_rightangled(a, b, c):
is_rightangled = False
if a > b and a > c:
is_rightangled = abs(b**2 + c**2 - a**2) < 0.001
elif b > a and b > c:
is_rightangled = abs(a**2 + c**2 - b**2) < 0.001
else:
is_rightangled = abs(a**2 + b**2 - c**2) < 0.001
return is_rightangled
testEqual(is_rightangled(1.5, 2.0, 2.5), True)
testEqual(is_rightangled(4.0, 8.0, 16.0), False)
testEqual(is_rightangled(4.1, 8.2, 9.1678787077), True)
testEqual(is_rightangled(4.1, 8.2, 9.16787), True)
testEqual(is_rightangled(4.1, 8.2, 9.168), False)
testEqual(is_rightangled(0.5, 0.4, 0.64031), True)
True
if the year is a leap year, False
otherwise.
a = year % 19 b = year % 4 c = year % 7 d = (19 * a + 24) % 30 e = (2 * b + 4 * c + 6 * d + 5) % 7 dateofeaster = 22 + d + e
year = int(input("Please enter a year"))
if year >= 1900 and year <= 2099:
a = year % 19
b = year % 4
c = year % 7
d = (19*a + 24) % 30
e = (2*b + 4*c + 6*d + 5) % 7
dateofeaster = 22 + d + e
if year == 1954 or year == 2981 or year == 2049 or year == 2076:
dateofeaster = dateofeaster - 7
if dateofeaster > 31:
print("April", dateofeaster - 31)
else:
print("March", dateofeaster)
else:
print("ERROR...year out of range")