Logical Statements

We use logical statements to make choices. For example, we could have the statement:

If a number is exactly divisible by 2 then it is an even number.

There is, however, not a straight off way of testing if a number is divisible by 2. Instead we can make the statement.

If the remainder is zero when we divide two numbers then the first number is exactly divisible by the second.

which means that:

If the remainder is zero when we divide a number by 2 then the number is even.

We can find the remainder when you divide two numbers by using the modulus operator (%). For example:

            >>> 10 % 3
            1
            >>> 12 % 3
            0
            >>> 24 % 5
            4
        
We can see that 12 is exactly divisible by 3 because the modulus is zero.
Notes and Code Outcome

Testing for even numbers

Write a program that determines if a number is even by checking:

if the remainder when the number is divided by 2 is zero then the number is even else the number is odd.

Notice that:
  • the if statement (line 5) ends with a colon (:) and what happens if the statement is true is indented. The else statement also ends with a colon and what happens when the if statement is false is also indented.
  • ==: (Line 5) testing if two things are equal uses a double equal sign.

              n = 12

              remainder = n % 2

              if remainder == 0:
                  print(n, " is even")
              else:
                  print(n, " is odd")

            

A List and a Loop

To speed up the testing of different numbers, we can give the program a list of number to test.
Notice that:
  • Line 1: We create a list of numbers by putting the numbers in square brackets ([]) and each value is comma separated.
  • Line 3: We iterate through the list of numbers, which means that we create a loop and go through each number, one at a time, in the sequence given in the list.
Exercise (optional): Adapt the code to make the test for odd or even be a function.
Show Notes ▼

              numbers = [2, 5, 22, 50, 16562, 11]

              for n in numbers:
                  remainder = n % 2

                  if remainder == 0:
                    print(n, " is even")
                  else:
                    print(n, " is odd")

            

Comparison Operators

Testing for equality uses the == operator. The comparison operators are:
== equal to
> greater than
< less than
<= less than or equal to
>= greater than or equal to
!= not equal to
Exercise: Write a program to test if a number is greater than 10.
Code.
Show Notes ▼
Results.
Show Notes ▼
Exercise: Write a program to test if a number is less than or equal to 22.
Code.
Show Notes ▼
Results.
Show Notes ▼

Logical Operators

We can combine different comparisons with the logical operators
  • and
  • or
  • not
Example: Write a program to test if a number is between 5 and 50.
Code.

              numbers = [2, 5, 22, 50, 16562, 11]

              for n in numbers:
                  if (n > 5) and (n < 50):
                      print(n, "is between 5 and 50")
                  else:
                      print(n, "is not between 5 and 50")
            
Results.
Show Notes ▼
Exercise: Write a program check if a number is divisible by either 3 or 7. Use the test list:

                    numbers = [42, 5, 22, 50, 16562, 11]
                  
Exercise: Write a program check if a number is divisible by both 3 and 7. Use the test list:

                    numbers = [42, 5, 22, 50, 16562, 11]
                  
Practice: Write a program that prints the square root of positive and negative numbers. For example, for the square root of -4 you should print "2i". Use the test list:

                    numbers = [4, 3, 2, 1, 0, -1, -2, -3, -4]
                  

Else If

What if we had multiple conditions to check, such as in assigning a letter grade to a numerical value or a piecewise defined function. Let's say the grade scheme was:
GradeScore (s)
A$$ 90 \lt s \le 100 $$
B$$ 80 \lt s \le 90 $$
C$$ 70 \lt s \le 80 $$
D$$ 60 \lt s \le 70 $$
F$$ s \le 60 $$

                    scores = [50, 95, 82, 88, 99, 70, 79]

                    for s in scores:
                        if (s > 90):
                            grade = "A"
                        elif (s > 80):
                            grade = "B"
                        elif (s > 70):
                            grade = "C"
                        elif (s > 60):
                            grade = "D"
                        else:
                            grade = "F"

                        print(f"Score = {s}, Grade = {grade}")

                  

Exercise

Draw a graph for the piecewise defined function below: $$ f(x) = \left\{ \begin{array}{ll} x & \quad x \lt -2 \\[.5em] 2x-1 & \quad -2 \le x \le 3 \\[.5em] -1 & \quad x > 3 \end{array} \right. $$ Ref: Remember how to draw a simple graph and plot points.
The result could look something like this:
Show Notes ▼