Lecture 05 If

Joseph Haugh

University of New Mexico

Free Recall

  • Get out a sheet of paper or open a text editor
  • For 2 minutes write down whatever comes to mind about the last class
    • This could be topics you learned
    • Questions you had
    • Connections you made

Recall: Long Division

Long Division

Integer Division

  • Recall that we can divide two number using the / operator
  • This does floating-point division, meaning the result is a float
  • What if instead we just wanted to know how many times the denominator went into the numerator?
  • This is called integer division
  • We can do this using the // operator
  • This is the result we arrived at from doing long division

Example from the video:

x = 823 // 6
print(x)

Visualize

Modulus Operator

  • What about the remainder though?
  • Can we calculate that in Python?
  • Yes! Using the modulus operator
  • This operator gives you the remainder after performing integer division

Example from the video:

r = 823 % 6
print(r)

Visualize

Exercise: Long Division Calculator

Complete the exercise on onlinegdb

Types So Far

We have seen several types so far:

  • Int: whole numbers
  • Float: numbers with a decimal point
  • String: sequence of characters

But there are more, one in particular will be important today, Booleans

Booleans

  • Booleans are among the simplest types of data
  • They can only have 2 different values
  • True or False

For example:

b1 = True
b2 = False
print(b1)
print(b2)

Visualize

Boolean Expressions

A boolean expression is just an expression which results in a boolean

For example we can check if two numbers are equal to each other:

x = 5
y = 6
b = x == y
print(b)

Visualize

x == y above is an example of a boolean expression

Relational Operators

  • The == operator from the last examples is called a relational operator
  • These operators allows us to determine the relationship between two numbers
  • Python defines the following relational operators:
Operator Description
== Are two numbers equal?
!= Are two numbers not equal?
> Is the first number greater than the second?
>= Is the first number greater than or equal to the second?
< Is the first number less than the second?
<= Is the first number less than or equal to the second?

Logical Operators

  • What if we want to ask multiple questions with relational operators and combine their results?
  • For example, what if we want to know if a numbers is both positive and divisible by 2?
  • How do we know if a number, x, is divisible by another number, y?
  • We need to know if after we divide x by y that we are left with a remainder of 0

We could write these two checks independently already:

x = 7
# Check if x is positive
isPos = x >= 0
# Check if x is even (divisible by 2)
isEven = x % 2 == 0

Visualize

Logical Operators

Logical operators allow us to combine multiple boolean expressions

Operator Description
and Both expressions must be true
or Either or both expressions must be true
not Flips the expression from True to False or vice versa

Example: Positive Even Number

How then can we write a program to check if a given number is both positive and even?

x = 7
# Check if x is positive
isPos = x >= 0
# Check if x is even (divisible by 2)
isEven = x % 2 == 0
# Check if it is both
isPosAndEven = isPos and isEven
print(isPosAndEven)

Visualize

Why Booleans

  • You may be thinking what is the point of all this??
  • The answer is decision making!
  • We will almost always want our code to behavior differently based on its input
  • So far our programs have used different numbers to compute different results
  • However, they haven’t used different equations based on the input

To achieve this we need to learn a new statement, the if statement which is closely tied with boolean expressions and therefore, also with logical operators

If Statements

  • If statements allow us to make decisions in our code
  • We can choose a different path through our code depending on the value of variables
  • This ability is critical when writing non-trivial programs
  • The syntax of an if is:
if <<condition>>:
    <<statements>>

Example: Positive or Negative

As an example let’s write a program which takes in an Int and prints “positive” if it is >=0 and “negative” otherwise.

x = int(input())
if x >= 0:
    print("positive")
if x < 0:
    print("negative")

Visualize

Could a number ever be positive and negative?

No! However, our program doesn’t reflect this, we check if x is less than 0 even if we already know it is not, luckily there is a way to rectify this.

If Else Statements

  • An else branch will only execute if the if statement preceding it was False
  • With this we could rewrite our previous example as follows:
x = int(input())
if x >= 0:
    print("positive")
else:
    print("negative")

Visualize

Now the condition will only be checked once!

Elif Statements

  • Python also provides a way to chain multiple checks together called elif
  • elif is like else in that it will only be executed if the if statement before it was False but it also allows you to add a check as well
  • This allows you to combine multiple mutually exclusive checks together
  • For example, let’s write a program to figure out if a number is 1, 2, 3, or more digits long:
x = int(input())
if x < 10:
    print("1 digit")
elif x < 100:
    print("2 digits")
elif x < 1000:
    print("3 digits")
else:
    print("4+ digits")

Visualize