Reading Questions
Quiz
Logical Operators
- Numerical operators have numeric values for operands
Logical operators (or Booean operators) have boolean values for operands
print(True and False) print(False or True) print(not False)
- These are useful in conditional statements when you have complex conditions
For example, if you want to check if a number is in a range
x = int(input('enter an integer: ')) if x >= 0: if x <= 100: print('you entered a number in the range [0, 100]') else: print('you did not enter an number in the range [0, 100]') else: print('you did not enter an number in the range [0, 100]')
This can be simplified with a logical operator
x = int(input('enter an integer: ')) if x >= 0 and x <= 100: print('you entered a number in the range [0, 100]') else: print('you did not enter a number in the range [0, 100]')
- Note, the operands must be Booleans!
The following will not work
print(x == 1 or 2 or 3) print(x > 0 and < 10)
Also note, and, or and not, do not have the same precedence
x = 2 print(not x == 1 or x == 2)
Is not equivalent to
x = 2 print(not (x == 1 or x == 2))
Boolean Functions
Can have variables equal to boolean values
x = int(input('enter an integer: ')) if x >= 0 and x <= 100: x_is_in_range = True else: x_is_in_range = False if x_is_in_range == True: print('you entered a number in the range [0, 100]') else: print('you did not enter a number in the range [0, 100]')
Setting a variable to a Boolean value can usually be simplified by using an expression
x = int(input('enter an integer: ')) x_is_in_range = x >= 0 and x <= 100 if x_is_in_range == True: print('you entered a number in the range [0, 100]') else: print('you did not enter a number in the range [0, 100]')
- This can make the code more readable
- Note that
if x_is_in_range == True:
is a little redundant It can be simplified to
if x_is_in_range:
which also reads a little betterx = int(input('enter an integer: ')) x_is_in_range = x >= 0 and x <= 100 if x_is_in_range: print('you entered a number in the range [0, 100]') else: print('you did not enter a number in the range [0, 100]')
Can also do this with a function that returns a Boolean value
def is_in_range(x): return x >= 0 and x <= 100 x = int(input('enter an integer: ')) if is_in_range(x) print('you entered a number in the range [0, 100]') else: print('you did not enter a number in the range [0, 100]')
Example - Point in Circles
import graphics
POINT_WIDTH = 10
# PRE: point_x, point_y, circle_x, circle_y, and circle_radius are
# positive numbers
# POST: returns whether the specified point is inside the specified
# circle
def is_point_in_circle(point_x, point_y, circle_x, circle_y, circle_radius):
delta_x = circle_x - point_x
delta_y = circle_y - point_y
distance = (delta_x * delta_x + delta_y * delta_y) ** (1 / 2)
return distance <= circle_radius
circle_offset = 50
circle_1_x = graphics.window_width() / 2 - circle_offset
circle_1_y = graphics.window_height() / 2
circle_1_radius = 100
circle_1_width = circle_1_radius * 2
circle_1_height = circle_1_width
circle_2_x = graphics.window_width() / 2 + circle_offset
circle_2_y = graphics.window_height() / 2
circle_2_radius = 100
circle_2_width = circle_2_radius * 2
circle_2_height = circle_2_width
graphics.clear()
graphics.draw_oval(circle_1_x, circle_1_y, circle_1_width, circle_1_height)
graphics.draw_oval(circle_2_x, circle_2_y, circle_2_width, circle_2_height)
while graphics.window_open():
if graphics.button_down(1):
point_x = graphics.mouse_x()
point_y = graphics.mouse_y()
in_circle_1 = is_point_in_circle(point_x, point_y,
circle_1_x, circle_1_y,
circle_1_radius)
in_circle_2 = is_point_in_circle(point_x, point_y,
circle_2_x, circle_2_y,
circle_2_radius)
if in_circle_1 or in_circle_2:
graphics.draw_oval(point_x, point_y, POINT_WIDTH, POINT_WIDTH,
fill="green")
else:
graphics.draw_oval(point_x, point_y, POINT_WIDTH, POINT_WIDTH,
fill="red")
graphics.wait()