Review

Calvin (Deutschbein)

W6Wed: 02 Oct

Announcements

  • Midterm on Friday.
    • Practice Exams posted
    • There is a practice exam on Canvas where the exam will be hosted.
  • Sections this week on Midterm review.

Today

  • Review
    • Problem 1
      • Numerical Arithmetic
      • Booleans
      • String Slices
    • Problem 2: Program Tracing
    • Problem 3
      • For
      • If
      • Print
      • Return
    • Problem 4: Strings

Numerical Arithmetic

  • Recall PEMDAS:
    • Parenthesis ()
    • Exponents **
    • Multiplication/Division * / //
    • Addition/Subtract + -

Mult > Add

  • Calculations are not left-to-right. >>> 1 + 2 * 3 7
  • The multiplication occurs prior to the addition, same as: >>> 2 * 3 6 >>> 1 + 6 7

Mult/Div Together

  • The order here does not matter. >>> 5 * 6 // 3 10 >>> (5 * 6) // 3 10 >>> 5 * (6 // 3) 10
  • So they occur the same.

Add/Sub Together

  • The order here does not matter. >>> 1 + 2 - 3 0 >>> (1 + 2) - 3 0 >>> 1 + (2 - 3) 0
  • So they occur the same.

Mult > Add

  • Calculations are not left-to-right. >>> 1 + 2 * 3 7
  • Order does matter here: >>> (1 + 2) * 3 9 >>> 1 + (2 * 3) 7
  • Multiplication first.

Exp > Mult

  • Exponentiation beats out multiplication. >>> 2 ** 1 * 3 6 >>> (2 ** 1) * 3 6 >>> 2 ** (1 * 3) 8

Example

  • Just break things down incrementally. 3 + 3 * 3 ** 3 % 3
  • Exponents first. 3 + 3 * 27 % 3 # 3 ** 3 is 27
  • Multiply, divide, mod, right-to-left 3 + 3 * 27 % 3 # 3 ** 3 is 27
  • Multiply, divide, mod, right-to-left 3 + 81 % 3 # 3 * 27 is 81
  • Multiply, divide, mod, right-to-left 3 + 0 # 81 % 3 is 0 (81 is a multiple of 3)
  • Add, subtract right-to-left 3 # 3 + 0 is 3

Today

  • Review
    • Problem 1
      • ✓ Numerical Arithmetic
      • Booleans
      • String Slices
    • Problem 2: Program Tracing
    • Problem 3
      • For
      • If
      • Print
      • Return
    • Problem 4: Strings

Numerical Arithmetic

  • Recall not / and / or
  • Recall "truthiness"

not

  • Changes True or False to the other. >>> not True False >>> not False True

and

  • True if both sides are True, otherwise False >>> True and True True >>> True and False False >>> False and True False >>> False and False False

or

  • True if either side is True, otherwise False >>> True or True True >>> True or False True >>> False or True True >>> False or False False

not > and

  • Do any "not" before "and" >>> (not True) and False False >>> not (True and False) True >>> not True and False False

and / or

  • Do "and" / "or" left-to-right >>> True and False or True True >>> (True and False) or True True >>> True and (False or True) True

Comparisons

  • Usually we get booleans from comparisons. >>> 1 == 0 False >>> 1 != 0 True >>> 1 >= 0 True >>> 1 <= 0 False >>> 'a' in 'aieou' True >>> 'a' == 'aieou'[0] True

Truthiness

  • Zero and "" are like False >>> bool(0) False >>> bool(1) True >>> bool("") False >>> bool(" ") True >>> bool(.5) True

Today

  • Review
    • Problem 1
      • ✓ Numerical Arithmetic
      • ✓ Booleans
      • String Slices
    • Problem 2: Program Tracing
    • Problem 3
      • For
      • If
      • Print
      • Return
    • Problem 4: Strings

Indices

  • Strings (words) have indices:
    • Each character (letter) has number corresponding to its location
    • Numbers begin with zero and increase by one
    • The numbers are all integers (whole numbers). >>> s = "hi there" >>> s[0] 'h' >>> s[1] 'i' >>> s[2] ' ' >>> s[3] 't'

Negative Indices

  • Strings (words) have indices:
    • Each character (letter) has number corresponding to its location
    • Negative values begin with -1 at the end of the string. >>> s = "hi there" >>> s[-1] 'e' >>> s[-2] 'r' >>> s[-3] 'e' >>> s[-4] 'h'

Slices

  • Slices take part of a string, based on indices.
    • They begin at some index, and continue up to some index. >>> s = "hi there" >>> s[0:4] 'hi t' >>> s[2:6] ' the' >>> s[-4:-2] 'he' >>> s[1:-1] 'i ther'

Slices

  • Slices take part of a string, based on indices.
    • If the first index is ommitted, it is regard as zero. >>> s = "hi there" >>> s[:2] 'hi' >>> s[0:2] 'hi' >>> s[:4] 'hi t' >>> s[0:4] 'hi t'

Slices

  • Slices take part of a string, based on indices.
    • If the last part is omitted, it is regard as the length of the string. >>> s = "hi there" >>> len(s) 8 >>> s[6] 'r' >>> s[6:] 're' >>> s[6:8] 're' >>> s[8] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range
    • NOTE: This does not correspond to a character!

Slices

  • Slices take part of a string, based on indices.
    • There may be a third value, a step size. >>> s = "hi there" >>> s[:4] 'hi t' >>> s[0:4:1] 'hi t' >>> s[:4:1] 'hi t' >>> s[:4:2] 'h ' >>> s[:6:2] 'h h'
    • Step of '2' gives every other letter.

Slices

  • Slices take part of a string, based on indices.
    • There may be a third value, a step size.
    • We can omit start and stop indices to take the whole list. >>> s = "hi there" >>> s[::1] 'hi there' >>> s[::2] 'h hr' >>> s[::3] 'htr' >>> s[::5] 'he' >>> s[5] 'e'
    • Basically, take indices that are multiples of the step size.

Slices

  • Slices take part of a string, based on indices.
    • There may be a third value, a step size.
    • We can omit start and stop indices to take the whole list.
    • Negative steps are permitted. >>> s = "hi there" >>> s[::-1] 'ereht ih' >>> s[::-2] 'eeti' >>> s[::-4] 'et' >>> s[-4] 'h'
    • This is much weirder.
    • In practice: basically only -1 is ever used, to reverse things.

Today

  • Review
    • Problem 1
      • ✓ Numerical Arithmetic
      • ✓ Booleans
      • ✓ String Slices
    • Problem 2: Program Tracing
    • Problem 3
      • For
      • If
      • Print
      • Return
    • Problem 4: Strings

Tracing

  • Take your time. def enigma(s:str, i:int) -> str: return s[:3:-3*i] def mystery(w:str) -> str: s = "" for i in range(1,3): s += enigma(w, i) return s print(mystery("abcdefgh"))
  • Start at the beginning.

Tracing

  • Take your time. def enigma(s:str, i:int) -> str: return s[:3:-3*i] def mystery(w:str) -> str: s = "" for i in range(1,3): s += enigma(w, i) return s print(mystery("abcdefgh")) # start here
  • "def" lines must be called, one is called at the end.

Tracing

  • We can imagine the code isn't in a def, like so: def enigma(s:str, i:int) -> str: return s[:3:-3*i] w = "abcdefgh" # was def mystery(w:str) -> str: s = "" for i in range(1,3): s += enigma(w, i) print(s) # was return s
  • Same as having the code block of mystery run then print the return value.

Tracing

  • We can do the same with "enigma"
  • But we have to be careful!
  • "enigma s" and "mystery w" are the same
  • "enigma s" and "mystery s" are totally different. def enigma(s:str, i:int) -> str: return s[:3:-3*i] w = "abcdefgh" s = "" for i in range(1,3): s += w[:3:-3*i] # was enigma(w, i) print(s)
  • Same as having the code block of mystery run then print the return value.

Tracing

  • Simplify. w = "abcdefgh" s = "" for i in range(1,3): s += w[:3:-3*i] print(s)
  • This is all that is left.

Tracing

  • We can expand the for loop. w = "abcdefgh" s = "" s += w[:3:-3*1] s += w[:3:-3*2] print(s)

Tracing

  • Do some multiplications. w = "abcdefgh" s = "" s += w[:3:-3] s += w[:3:-6] print(s)

Tracing

  • These indices:
    • Read backwards
    • End at position 3 (the fourth letter, 'd')
  • So we remove those four letters and the ending indices. w = "efgh" s = "" s += w[::-3] s += w[::-6] print(s)

Tracing

  • Perhaps easier with the string right there. s = "" s += "efgh"[::-3] s += "efgh"[::-6] print(s)

Tracing

  • Perhaps easier with the string right there. s = "" s += "efgh"[::-3] s += "efgh"[::-6] print(s)
  • For this one: "efgh"[::-3]
    • Start at the end with 'h'
    • Go BACKWARD g -> f -> e
    • This is 'he'
    >>> "efgh"[::-3] 'he'

Tracing

  • Perhaps easier with the string right there. s = "" s += "he" s += "efgh"[::-6] print(s)
  • For this one: "efgh"[::-6]
    • Start at the end with 'h'
    • There aren't 6 letters, so it's just 'h'.

Tracing

  • Perhaps easier with the string right there. s = "" s += "he" s += "h" print(s)
  • For this one: "efgh"[::-6]
    • Start at the end with 'h'
    • There aren't 6 letters, so it's just 'h'.

Tracing

  • We can combine the += into a big string add. s = "" + "he" + "h" print(s)

Tracing

  • We do the string add... s = "heh" print(s)

Tracing

  • That leaves us with: print("heh")
  • When reviewing, we can check in Python: >>> def enigma(s:str, i:int) -> str: ... return s[:3:-3*i] ... >>> def mystery(w:str) -> str: ... s = "" ... for i in range(1,3): ... s += enigma(w, i) ... return s ... >>> print(mystery("abcdefgh")) heh

Today

  • Review
    • Problem 1
      • ✓ Numerical Arithmetic
      • ✓ Booleans
      • ✓ String Slices
    • ✓ Problem 2: Program Tracing
    • Problem 3
      • For
      • If
      • Print
      • Return
    • Problem 4: Strings

for / if / print / return

  • From zero up to n:
    • Print "even" or "odd" for each number, and
    • Return the sum of the even numbers.
    def even_sum(n:int) -> int:

for / if / print / return

  • From zero up to n:
    • Print "even" or "odd" for each number, and
    • Return the sum of the even numbers.
    def even_sum(n:int) -> int: for i in range(n): # from zero up to n

for / if / print / return

  • From zero up to n:
    • Print "even" or "odd" for each number, and
    • Return the sum of the even numbers.
    def even_sum(n:int) -> int: for i in range(n): # from zero up to n print("even") # print even

for / if / print / return

  • From zero up to n:
    • Print "even" or "odd" for each number, and
    • Return the sum of the even numbers.
    def even_sum(n:int) -> int: for i in range(n): # from zero up to n if # something here print("even") # print even else: print("odd") # or odd
  • Even numbers are divisible by two, so...

for / if / print / return

  • From zero up to n:
    • Print "even" or "odd" for each number, and
    • Return the sum of the even numbers.
    def even_sum(n:int) -> int: for i in range(n): # from zero up to n if i % 2 == 0: print("even") # print even else: print("odd") # or odd

for / if / print / return

  • From zero up to n:
    • Print "even" or "odd" for each number, and
    • Return the sum of the even numbers.
    def even_sum(n:int) -> int: result = 0 # return sum for i in range(n): # from zero up to n if i % 2 == 0: print("even") # print even else: print("odd") # or odd return result # return sum

for / if / print / return

  • From zero up to n:
    • Print "even" or "odd" for each number, and
    • Return the sum of the even numbers.
    def even_sum(n:int) -> int: result = 0 # return sum for i in range(n): # from zero up to n if i % 2 == 0: result += i # sum of evens print("even") # print even else: print("odd") # or odd return result # return sum

for / if / print / return

  • From zero up to n:
    • Print "even" or "odd" for each number, and
    • Return the sum of the even numbers.
    >>> result = even_sum(7) even odd even odd even odd even >>> print(result) 12

Today

  • Review
    • Problem 1
      • ✓ Numerical Arithmetic
      • ✓ Booleans
      • ✓ String Slices
    • ✓ Problem 2: Program Tracing
    • ✓ Problem 3
      • ✓ For
      • ✓ If
      • ✓ Print
      • ✓ Return
    • Problem 4: Strings

Announcements

  • Midterm on Friday.
    • Practice Exams posted
    • There is a practice exam on Canvas where the exam will be hosted.
  • Sections this week on Midterm review.