Read sections 5.1, 5.2, and 5.3 in the book and do the examples.
We can combine these concepts to make some complicated boolean expressions. For example, we might want to know whether a number is divisible by 3 and is greater than zero.
>>> x = 123 >>> x % 3 == 0 True >>> x > 0 True >>>
It passes both tests, and we can combine that all into one big test:
>>> (x % 3 == 0) and (x > 0) True >>>
If we're doing this a lot, we might want to encapsulate it into a function:
def positive_and_divisible_by_3(x): """Prints whether x is divisible by 3 and positive.""" boolean = (x % 3 == 0) and (x > 0) print("It is", boolean, "that", x, "is divisible by 3 and positive.")
Now some tests:
>>> positive_and_divisible_by_3(123) It is True that 123 is divisible by 3 and positive. >>> positive_and_divisible_by_3(-1231) It is False that -1231 is divisible by 3 and positive. >>> positive_and_divisible_by_3(37) It is False that 37 is divisible by 3 and positive. >>> positive_and_divisible_by_3(-6) It is False that -6 is divisible by 3 and positive. >>> positive_and_divisible_by_3(0) It is False that 0 is divisible by 3 and positive.