Docsity

Lecture 03 Iteration in Python, Lecture notes of Design

Partial preview of the text

Download Lecture 03 Iteration in Python and more Lecture notes Design in PDF only on Docsity!

Lecture 03

Iteration in Python

based in part on notes from the CS-for-All curriculum developed at Harvey Mudd College

Last Time (lecture 02)

  • Conditional Statements and Flow of Control
    • if
    • if-else
    • if-elif-else
      • True/False Blocks (require indentation)
  • Variable Scope
    • Local
    • Global
  • Memory: Frames and the Stack
    • Tracing global, local, and printed output
    • Functions calling Functions

Review

Default Index/Slicing Values

s == s[:]

s[:] = s[::]

s[::] = s[0:len(s):1]

s[len(s):] == ‘’

s=‘01234’ # len(s) == 5

s[2:] == s[2:5:1]

s[:3] == s[0:3:1]

s[::2] == s[0:5:2]

s[:4:3] == s[0:4:3]

s[1::2] == s[1:5:2]

Lecture 03 Goals

  • Introduce Test Driven Design (TDD)
  • Iteration
    • Definite vs. Indefinite looping - for loops
    • Element-based vs. Index-based
  • List comprehensions
    • Generative vs. Manipulative
    • Uniform vs. Conditional

Test Driven Design Example

Write a function gap(x,y) that returns the distance between the numbers x and y? Use if statements and not a function like abs or max.

  1. Think clearly about how each function should work
    • Inputs (what are arguments) Two numbers, x and y
    • Outputs (what should be returned) The distance between x and y, i.e. |x-y|
  • Special cases If x==y, must return 0
  • Usual cases x > y or x < y

Test Driven Design Example

Write a function gap(x,y) that returns the distance between the numbers x and y? Use if statements and not a function like abs or max.

  1. Develop a function signature (def + docstring)

def gap(x,y):

’’’Returns distance between two input numbers.’’’

NOTE: The doc string should explain what the function does (and how to use it, i.e. inputs, outputs) but NOT how it does it.

Improving Tests

  1. Add/improve tests as needed
    • Creating student accounts for CS department machines
    • The code was tested and it worked, but it failed to account for cases where there were two sections of the class on CAB (CS 4) - Edge case- a case that will rarely happen, but your program should still be able to handle it
    • For CS logins, add test to make sure it works for class with two sections

Test Driven Design

Now code/test your function, design will be informed by tests that need to pass. def gap_test(): assert gap(10,10)==0, 'x==y test failed' assert gap(1, 10)==9, 'xy test failed’

def gap(x, y): # Fill in after first set of tests! ''' Returns the distance between two input numbers.''' if x > y: return x – y else: return y – x

gap_test() As you proceed keep testing,

  1. Add/improve tests as needed 11
  • A loop is a sequence of instructions to be repeated
  • Definite and Indefinite
    • Definite: repeat exactly X times
    • Indefinite: repeat until some condition changes

Iteration: Loops

This is Bijou. Bijou is demonstrating the following iteration examples:

for every front paw paw = paw + frilly blue glove while sun == shining shed_more_fur()

Definite Loops

based in part on notes from the CS-for-All curriculum developed at Harvey Mudd College

for Loops (cont.)

for i in [1, 2, 3]: print('Warning') print(i)

  • General syntax:
  • In this case, our sequence is a sequence of values , but it could be any sequence (i.e. for word in list_of_words)
  • For each value in the sequence:
  • the value is assigned to the variable
  • all statements in the body of the loop are executed using that value
  • Once all values in the sequence have been processed, the program continues with the first statement after the loop.

for in :

Executing a for Loop

for in :

execute statement after the loop

yes

no

does the more values?

assign the next value in the sequence to variable

execute the statements in the body

sequence have

for i in [1, 2, 3]: print('Warning') print(i)

Executing Our Earlier Example

(with one extra statement) for i in [ 1 , 2, 3]: print('Warning') print(i) print('That's all.')

no

does more values?

assign the next value in the sequence to i

[1, 2, 3] have more? i output/action yes 1 yes

print('That's all.')

print('Warning') print(i)

Executing Our Earlier Example

(with one extra statement) for i in [1, 2, 3]: print('Warning') print(i) print('That's all.')

yes

no

does more values?

assign the next value in the sequence to i

[1, 2, 3] have more? i output/action yes 1 Warning 1

print('That's all.')

print('Warning') print(i)