---- 6.2 a function abstracts a bunch of code ----
def tax(subtotal):
return subtotal * 0.08875
tax(700)
62.125
700 + tax(700)
762.125
sum = 700
sum = sum + sum * 0.08875 #add taxes
sum
762.125
---- 6.3 functions have a scope ----
def set_a():
a = 10
print('the value of a is', a)
set_a()
the value of a is 10
a = 20
print('initial value of a is', a)
set_a()
print('new value of a is', a)
initial value of a is 20 the value of a is 10 new value of a is 20
def scoped(a, b): #a and b are like input pins
third= b + b - a
return third
a = 10
b = 20
third = 12
scoped(2,4)
6
---- 6.4 iterationsszz ----
l = [3, 4 ,6, 9]
for num in l:
print(num)
3 4 6 9
for e in l:
print(e)
3 4 6 9
for num in l:
print(num*2)
6 8 12 18
def mean(sequence):
total = 0
for element in sequence:
total = total + element
return total / len(sequence)
mean([3, 4, 5])
4.0
tryout = [6, -6, 6]
mean(tryout)
2.0
---- 6.5 polymorphism abstracts across types ----
x = 5
type(x)
int
type(5)
int
x = 'bazooka'
type(x)
str
a = 'knock'
b = ' knock'
print(a+b)
knock knock
len('ciao') #returns how many characters the string has
4
len(['helo', 'spippolo']) #returns the n of elements
2
len('spippolo')
8
len(5)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_4562/4108502181.py in <module> ----> 1 len(5) TypeError: object of type 'int' has no len()
len(str(5))
1
'9' + 'HELLO' + str(8)
'9HELLO8'
---- 6.6 revisiting double-double ----
vol = [4.0, 2.0, 3.0, 5.5]
result = []
for element in vol:
result = result + [element * 2]
print(result)
[8.0, 4.0, 6.0, 11.0]
def double(sequence):
result = []
for i in sequence:
result = result + [i*2]
return result
words = ['ciao', 'panzanella']
double('words')
['ww', 'oo', 'rr', 'dd', 'ss']
double(words)
['ciaociao', 'panzanellapanzanella']
---- testing eqality true false ----
2+2 == 4
True
2+2==3
False
2+2 == 3+1
True
'hello' == 'hello'
True
'hello' == 'ciao'
False
ciao = 'hello'
ciao == 'hello'
True
---- 6.8 IF ----
def secret(word):
if word == 'struzzo':
return 'oh yeah'
else:
return 'prrrrrr'
secret('guess')
'prrrrrr'
secret('struzzo')
'oh yeah'
def secret(word):
return (word == 'struzzo')
secret('struzzo')
True
---- 6.9 division a special error and types ----
4/0
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) /tmp/ipykernel_4562/1580332654.py in <module> ----> 1 4/0 ZeroDivisionError: division by zero
'hi'*2
'hihi'
'hihi'/2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_4562/2892899560.py in <module> ----> 1 'hihi'/2 TypeError: unsupported operand type(s) for /: 'str' and 'int'
#half of each
def half(sequence):
result = []
for element in sequence:
result = result + [element/2]
return result
half([4, 5, 7])
[2.0, 2.5, 3.5]
def onethird(seq):
result = []
for i in seq:
result = result + [i/3]
return result
onethird([5, 6, 8])
[1.6666666666666667, 2.0, 2.6666666666666665]
#exclaim
def exclaim(seq):
result = []
for i in seq:
result = result + [i + '!']
return result
exclaim(['orco', 'diga'])
['orco!', 'diga!']
exclaim(['ciccio', str(8)])
['ciccio!', '8!']
#emptyness
def has_no_elements(sequence):
return (sequence == [])
has_no_elements([2])
False
#sum three
def sum_three(a, b, c):
d = a + b + c
return d
sum_three(2,3,4)
9
#ten times each
def ten_t_each(sequence):
result = []
for i in sequence:
result = result + [i*10]
return result
ten_t_each([3, 45])
[30, 450]
#positive numbers
def positive(seq):
result = []
for i in seq:
if i > 0 :
result = result + [i]
return len(result)
positive([1,-2,-4,5])
2
def pulse(seq):
result = 0
for i in seq:
if i > 0 :
result = result + i
return result
pulse([3, -9, 5])
8
#free-proj doubledouble pro 2
def doubledouble(seq):
result = []
for i in seq:
result = result +[i*4]
return result
doubledouble(['ahy', 'amor'])
['ahyahyahyahy', 'amoramoramoramor']
def doubledouble2(seq):
result = []
for i in seq:
result = result + ['nice '+i]
return result
doubledouble2(['t-shirt', 'shoes', 'book'])
['nice t-shirt', 'nice shoes', 'nice book']