



































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
1 / 75
This page cannot be seen from the preview
Don't miss anything!




































































based in part on notes from the CS-for-All curriculum developed at Harvey Mudd College
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]
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.
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.
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.
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,
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()
based in part on notes from the CS-for-All curriculum developed at Harvey Mudd College
for i in [1, 2, 3]: print('Warning') print(i)
for in :
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)
(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)
(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)