Computer Science Thing of the Day
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 <= 9: print('you entered a single digit number') else: print('you did not enter a single digit number') else: print('you did not enter a single digit number')
This can be simplified with a logical operator
x = int(input('enter an integer: ')) if x >= 0 and x <= 9: print('you entered a single digit number') else: print('you did not enter a single digit number')
- 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 <= 9: x_is_digit = True else: x_is_digit = False if x_is_digit == True: print('you entered a single digit number) else: print('you did not enter a single digit number')
Setting a variable to a Boolean value can usually be simplified by using an expression
x = int(input('enter an integer: ')) x_is_digit = x >= 0 and x <= 9 if x_is_digit == True: print('you entered a single digit number) else: print('you did not enter a single digit number')
- This can make the code more readable
- Note that
if x_is_digit == True:
is a little redundant It can be simplified to
if x_is_digit:
which also reads a little betterx = int(input('enter an integer: ')) x_is_digit = x >= 0 and x <= 9 if x_is_digit: print('you entered a single digit number) else: print('you did not enter a single digit number')
Can also do this with a function that returns a Boolean value
def is_digit(x): return x >= 0 and x <= 9 x = int(input('enter an integer: ')) if is_digit(x) print('you entered a single digit number) else: print('you did not enter a single digit number')
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()