4 + 4.5 + 0.375
8.875
1500 * 0.08875
133.125
995.50 * 0.08875
88.350625
def tax(subtotal):
return subtotal * 0.08875
tax(1500)
133.125
1500 * 0.08875
133.125
200 + 1500 + tax(1500)
1833.125
sum = 1500
sum = sum + sum * 0.08875
sum
1633.125
def set_a():
a = 10
print('The value of a:', a)
# a has been set to 10
set_a()
The value of a: 10
a = 20
print('Initially, the value of a:', a) # outer universe
set_a()
print('Finally, the value of a:', a) # outer universe
Initially, the value of a: 20 The value of a: 10 Finally, the value of a: 20
def scoped(first, second):
third = second + second - first
return third
first = 10 # outer universe
second = 11 # outer universe
third = 12 # outer universe
scoped(2, 4)
6
first
10
second
11
third
12
def scoped(la, lala):
lalala = lala + lala - la
return lalala
first = 10 # outer universe
second = 11 # outer universe
third = 12 # outer universe
scoped(2, 4)
6
l = [7, 4, 2, 6]
for num in l:
print(num)
7 4 2 6
for num in l:
print(num * 2)
14 8 4 12
def mean(sequence):
for element in sequence:
total = total + element
return total / len(sequence)
def mean(sequence):
total = 0
for element in sequence:
total = total + element
return total / len(sequence)
mean([2, 6])
4.0
mean([2, 2, 2, 6, 6, 6])
4.0
x = 5
type(x)
int
type(5)
int
type('hello world')
str
x = 'hello world'
type(x)
str
type([1, 2, 3])
list
'hello' + 'world'
'helloworld'
'knock' + 'knock'
'knockknock'
a = 'hello'
b = 'world'
a + b
'helloworld'
len('hello world')
11
len([1, 2, 3])
3
len(['hello', 'world'])
2
len(5)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3047/4108502181.py in <module> ----> 1 len(5) TypeError: object of type 'int' has no len()
len(str(5))
1
2 + 'hello'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3047/2556979404.py in <module> ----> 1 2 + 'hello' TypeError: unsupported operand type(s) for +: 'int' and 'str'
str(2) + 'hello'
'2hello'
volume = [4.0, 2.0, 3.0, 5.5]
result = []
for element in volume:
result = result + [element * 2]
result
[8.0, 4.0, 6.0, 11.0]
result = []
result
[]
result = result + [4.0]
result
[4.0]
result = result + [2.0]
result
[4.0, 2.0]
result = result + [3.0]
result
[4.0, 2.0, 3.0]
result = []
result = result + 4.0
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3047/440397681.py in <module> 1 result = [] ----> 2 result = result + 4.0 TypeError: can only concatenate list (not "float") to list
words = ['hello', 'world']
result = []
for element in words:
result = result + [element * 2]
result
['hellohello', 'worldworld']
countdown = [3, 2, 1, 'go']
result = []
for element in countdown:
result = result + [element * 2]
result
[6, 4, 2, 'gogo']
def double(sequence):
result = []
for element in sequence:
result = result + [element * 2]
return result
double([3, 2, 1, 'go'])
[6, 4, 2, 'gogo']
double('why')
['ww', 'hh', 'yy']
def triple(sequence):
result = []
for element in sequence:
result = result + [element * 3]
return result
triple([3, 1, 'go'])
[9, 3, 'gogogo']
x = 5
type(x)
int
print(x)
5
y = 'hello'
type(y)
str
print(y)
hello
triple(y)
['hhh', 'eee', 'lll', 'lll', 'ooo']
x + x
10
y + y
'hellohello'
len(y) # y=hello hat die Länge 5
5
len(y + y)
10
len(y + y + y) + 75
90
len(str(x) + y) # x=5 hat die Länge 1, y=hello hat die Länge 5
6
str(x) + y # coverts x=5 which is an int into a string!
'5hello'
triple(str(x) + y)
['555', 'hhh', 'eee', 'lll', 'lll', 'ooo']
triple(str('5hello'))
['555', 'hhh', 'eee', 'lll', 'lll', 'ooo']
triple('5hello')
['555', 'hhh', 'eee', 'lll', 'lll', 'ooo']
triple('hello')
['hhh', 'eee', 'lll', 'lll', 'ooo']
triple(['hello'])
['hellohellohello']
triple([str(x) + y])
['5hello5hello5hello']
triple([123])
[369]
triple([1, 2, 3])
[3, 6, 9]
words = ['hello', 'world']
triple(words)
['hellohellohello', 'worldworldworld']
test = ['hello world ']
triple(test)
['hello world hello world hello world ']
double(test)
['hello world hello world ']
triple([str(x) + y] + test)
['5hello5hello5hello', 'hello world hello world hello world ']
countdown = [1, 2, 3, 'gooo']
triple(countdown)
[3, 6, 9, 'gooogooogooo']
double('siesta') # 'siesta' as a list
['ss', 'ii', 'ee', 'ss', 'tt', 'aa']
double([str('siesta')]) # 'siesta' as a string
['siestasiesta']
double('123siesta')
['11', '22', '33', 'ss', 'ii', 'ee', 'ss', 'tt', 'aa']
double([str('123siesta')])
['123siesta123siesta']
2 + 2 == 4
True
2 + 1 == 4
False
2 + 2
4
2 + 2 == 2 + 1
False
greeting = 'hello'
greeting == 'hello'
True
greeting == 'hi'
False
Jian = 32
Jian == 18
False
Jian == 32
True
Laura = [28, 'sad']
Laura == [28, 'sad']
True
Laura == [28]
False
def secret(word):
if word == 'please':
return 'Yes!'
secret('hello')
secret('world')
secret('please')
'Yes!'
secret('PLEASE')
def yesnomaybe(word):
if word == 'yes':
return True
if word == 'no':
return False
if word == 'maybe':
return 'WHY?'
yesnomaybe('haha')
yesnomaybe('no')
False
yesnomaybe('maybe')
'WHY?'
def yes(word):
if word == 'yes':
return True
if word == 'YES':
return True
if word == 'Yes':
return True
else:
return 'What did you say?'
yes('yes')
True
yes('YES')
True
yes('no')
'What did you say?'
def farewell(word):
return(word == 'bye')
farewell('bye')
True
farewell('ciao')
False
7/0
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) /tmp/ipykernel_3047/2063981331.py in <module> ----> 1 7/0 ZeroDivisionError: division by zero
# EXERCISE 6-1: Half of each
def half(sequence):
result = []
for element in sequence:
result = result + [element / 2]
return result
half([8])
[4.0]
half([7.5])
[3.75]
half([0])
[0.0]
def otherhalf(sequence):
result = []
for element in sequence:
result = result + [element * 0.5]
return result
otherhalf([8])
[4.0]
# EXERCISE 6-2: Exclamation
def exclamation(sequence):
result = []
for element in sequence:
result = result + [element + '!']
return result
exclamation(['hello'])
['hello!']
exclamation(['hello', 'world'])
['hello!', 'world!']
exclamation(['hello world'])
['hello world!']
exclamation([str(1), str(1.2), 'go'])
['1!', '1.2!', 'go!']
# EXERCISE 6-3: Emptiness
def has_no_elements(sequence):
if sequence == []:
return True
else:
return False
has_no_elements([])
True
has_no_elements([1])
False
has_no_elements(['Love'])
False
has_no_elements([1, 2])
False
has_no_elements([1, 2, 'Love'])
False
# EXERCISE 6-4: Sum Three
def sum_three(la, lala, lalala):
ergebnis = la + lala + lalala
return ergebnis
sum_three(1, 2, 3)
6
sum_three(1, 1, 8)
10
# EXERCISE 6-5: Ten Times Each
def ten_times_each(sequence):
result = []
for element in sequence:
result = result + [element + element * 10]
return result
ten_times_each([1, 2, 3])
[11, 22, 33]
# EXERCISE 6-6: Positive Numbers
def positive(sequence):
result = []
for element in sequence:
result = [element + element + element]
return result
positive([2, 2, 2, 2])
[6]
def positive(sequence):
result = []
for element in sequence:
result = len(sequence)
return result
positive([2, 2, -2])
3
def positive(sequence):
result = []
for element in sequence:
result = len(sequence)
if element in sequence > 1:
return result
positive([11, 2])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3047/487135158.py in <module> ----> 1 positive([11, 2]) /tmp/ipykernel_3047/3864728334.py in positive(sequence) 3 for element in sequence: 4 result = len(sequence) ----> 5 if element in sequence > 1: 6 return result TypeError: '>' not supported between instances of 'list' and 'int'
def positive(sequence):
result = []
if element in sequence > 1:
result = [len(sequence)]
return result
positive([1, 1, 2])
def positive(sequence):
result = []
for element in sequence:
if element > 0:
result = result + [element * 1]
return len(result)
positive([1, 1, -2, 1.4, -765, 1, 1])
5
def pluses(sequence):
result = []
for element in sequence:
if element > 0:
result = result + [element * 1]
return sum(result)
pluses([1, 1, 1, -3])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3047/1972571192.py in <module> ----> 1 pluses([1, 1, 1, -3]) /tmp/ipykernel_3047/1338065240.py in pluses(sequence) 4 if element > 0: 5 result = result + [element * 1] ----> 6 return sum(result) TypeError: 'float' object is not callable
def pluses(sequence):
result = []
for element in sequence:
result = result + [sum(element * 1)]
return result
pluses([1, 1, 1, 3])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3047/890866258.py in <module> ----> 1 pluses([1, 1, 1, 3]) /tmp/ipykernel_3047/3085567896.py in pluses(sequence) 2 result = [] 3 for element in sequence: ----> 4 result = result + [sum(element * 1)] 5 return result TypeError: 'float' object is not callable
def pluses(sequence):
result = []
for element in sequence:
result = result + [element * 1]
return sum(result)
pluses([1, 1, 1, 3]) # no idea
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3047/890866258.py in <module> ----> 1 pluses([1, 1, 1, 3]) /tmp/ipykernel_3047/43242412.py in pluses(sequence) 3 for element in sequence: 4 result = result + [element * 1] ----> 5 return sum(result) TypeError: 'float' object is not callable