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
File "/tmp/ipykernel_3202/2574803935.py", line 1 def tax(subtotal) ^ SyntaxError: invalid syntax
def tax(subtotal):
return subtotal * 0.08875
return()
File "/tmp/ipykernel_3202/2760087742.py", line 1 return() ^ SyntaxError: 'return' outside function
tax(1500)
133.125
tax(1000)
88.75
1500+tax(1500)
1633.125
300+1500+tax(1500)
1933.125
sum = 1500
sum + sum + sum * 0.08875 # add the tax amount
3133.125
sum
1500
def set_a():
a=10
print('the value of a:', a)
set_a()
the value of a: 10
a=20
print('the value of a:', a)
set_a()
print('Finally, the value of a:', a)
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
second = 11
third = 12
scoped(2,4)
6
first
10
second
11
third
12
scoped(2,4)
6
scoped(13, 12)
11
scoped(100,210)
320
first = 200
scoped(100, 500)
900
first
200
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)
temperature - 80
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_3202/3334237015.py in <module> ----> 1 temperature - 80 NameError: name 'temperature' is not defined
def mean(sequence):
total = 0
for element in sequence:
total = total + element
return total / len(sequence)
short = [7, 7, 7, 7, 7,]
mean(short)
7.0
short = [-15, 0, 15]
mean(short)
0.0
long = [100, 0, -100, 100, 200, -200, -100, 300, -300, 2.2, -2.2]
mean(long)
0.0
x = 6
print(x)
6
type(x)
int
x=6
type(x)
int
# why my answer is different?, this means 'integers'
type(6)
int
x = 'hey world'
type(x)
str
type(hey world)
File "/tmp/ipykernel_3202/3118624774.py", line 1 type(hey world) ^ SyntaxError: invalid syntax
type('hey world')
str
'hey'+'ho'+'yay'
'heyhoyay'
'knock '+ 'knock'
'knock knock'
a = 'Chaeyoung is '
b = 'an artist'
a+b
'Chaeyoung is an artist'
c = 'Korean'
a+c
'Chaeyoung is Korean'
d='a beginner in coding and programming'
a+d
'Chaeyoung is a beginner in coding and programming'
len(a)
13
len(a+b+c+d)
64
len([1, 3, 4, 6, 8])
5
len(['hello', 'world'])
2
len(5)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3202/4108502181.py in <module> ----> 1 len(5) TypeError: object of type 'int' has no len()
len('5')
1
len([5])
1
2+'hello'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3202/2596768515.py in <module> ----> 1 2+'hello' TypeError: unsupported operand type(s) for +: 'int' and 'str'
'2'+'morrow'
'2morrow'
len(str(5))
1
#what is the difference between line 62, 63, 66?
str(2)+'morrow'
'2morrow'
#what is the difference between line 65 and 68?
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+5.5
result
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3202/1172380077.py in <module> ----> 1 result = result+5.5 2 result TypeError: can only concatenate list (not "float") to list
result = result + [5.5]
result
[4.0, 2.0, 3.0, 5.5]
result = result + [7.0]
result
[4.0, 2.0, 3.0, 5.5, 7.0]
def double(sequence):
result = []
for element in sequence:
result = result + [element*2]
return result
sequence = x
x = [4.0, 2.0, 3.0, 5.5, 7.0]
double(x)
[8.0, 4.0, 6.0, 11.0, 14.0]
words = ['chae', 'young']
double(words)
['chaechae', 'youngyoung']
double(x)+double(words)
[8.0, 4.0, 6.0, 11.0, 14.0, 'chaechae', 'youngyoung']
random = ['hey ', 2, '22', 0.19293]
double(double(random))
['hey hey hey hey ', 8, '22222222', 0.77172]
double(random)
['hey hey ', 4, '2222', 0.38586]
double('abstraction')
['aa', 'bb', 'ss', 'tt', 'rr', 'aa', 'cc', 'tt', 'ii', 'oo', 'nn']
#whoooa!
double(abstraction)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_3202/2423191121.py in <module> ----> 1 double(abstraction) NameError: name 'abstraction' is not defined
abstraction = 'abstraction'
double(abstraction)
['aa', 'bb', 'ss', 'tt', 'rr', 'aa', 'cc', 'tt', 'ii', 'oo', 'nn']
2+2 == 4
True
'right-hand' == 'left-hand'
False
'right-hand' == 'right-hand'
True
right-hand == left-hand
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_3202/3973249329.py in <module> ----> 1 right-hand == left-hand NameError: name 'right' is not defined
2+2 == 3+1 == 5-1
True
2+2 == 3+1 == 5-3
False
greeting = 'hello'
greeting == 'hello'
True
greeting == 'hi'
False
def secret(word):
if word == 'please':
return 'YES!'
secret('plz')
secret('word')
secret('please')
'YES!'
def secret(word):
if word == 'please':
return 'YES!'
if word != 'please':
return 'say the magic word ~_~'
secret('plz')
'say the magic word ~_~'
secret('word')
'say the magic word ~_~'
secret('please')
'YES!'
secret('Please')
'say the magic word ~_~'
def yesno(word):
if word == 'yes':
return True
if word == 'no':
return False
yesno(word)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_3202/2485441838.py in <module> ----> 1 yesno(word) NameError: name 'word' is not defined
yesno('word')
yesno('haha')
yesno('YES')
yesno('yes')
True
yesno('no')
False
def yesno(word):
if word == 'please':
return 'Yes!'
else:
return 'No! Say the magic word!'
yesno('hmm')
'No! Say the magic word!'
yesno('please')
'Yes!'
def secret(word):
return (word=='please')
secret('word')
False
secret('please')
True
# need to digest and reflect on these codes(from 100-136)
2 / 0
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) /tmp/ipykernel_3202/2685369145.py in <module> ----> 1 2 / 0 ZeroDivisionError: division by zero
'hey'*2
'heyhey'
'hey '*2
'hey hey '
'heyhey'/2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3202/1596976081.py in <module> ----> 1 'heyhey'/2 TypeError: unsupported operand type(s) for /: 'str' and 'int'
def half(sequence):
result = []
for element in sequence:
result = result + [element*0.5]
return result
half([5, 2, 10, 7.8])
[2.5, 1.0, 5.0, 3.9]
def half(sequence):
result= []
for element in sequence:
result = result + [element/2]
return result
half([5, 2, 10, 7.8])
[2.5, 1.0, 5.0, 3.9]
def exclaim(sequence):
result = []
for element in sequence:
result = result + [element + '!']
return result
exclaim(['Hey', 'Ho', 'Yo'])
['Hey!', 'Ho!', 'Yo!']
exclaim(['Hello', 'world'])
['Hello!', 'world!']
exclaim([1, 2.2, True])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3202/1761221605.py in <module> ----> 1 exclaim([1, 2.2, True]) /tmp/ipykernel_3202/1849259270.py in exclaim(sequence) 2 result = [] 3 for element in sequence: ----> 4 result = result + [element + '!'] 5 return result TypeError: unsupported operand type(s) for +: 'int' and 'str'
exclaim([1, 2.2])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3202/3356193766.py in <module> ----> 1 exclaim([1, 2.2]) /tmp/ipykernel_3202/1849259270.py in exclaim(sequence) 2 result = [] 3 for element in sequence: ----> 4 result = result + [element + '!'] 5 return result TypeError: unsupported operand type(s) for +: 'int' and 'str'
def exclaim(sequence):
result = []
for element in sequence:
result = result + [element + '!']
return result
exclaim(1, 2.2)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3202/3190319864.py in <module> ----> 1 exclaim(1, 2.2) TypeError: exclaim() takes 1 positional argument but 2 were given
exclaim(['1', '2.2', 'True'])
['1!', '2.2!', 'True!']
def has_no_elements(sequence):
return (sequence==[])
has_no_elements([])
True
has_no_elements([1,2])
False
def sum_three(first, second, third):
fourth = first + second + third
return fourth
sum_three(0, 1, 2)
3
def ten_times_each(sequence):
result = []
for element in sequence:
result = result + [element*10]
return result
ten_times_each([1, 20, 5, -100, 4.5])
[10, 200, 50, -1000, 45.0]
def ten_times_each(sequence):
result = []
for elements in sequence:
result = result + [element*10]
return result
ten_times_each([1, 20, 5, -100, 4.5])
[55.0, 55.0, 55.0, 55.0, 55.0]
#I wonder what is happening in line 172 - 173
def positive(sequence):
result = [len(sequence)]
return result
positive([-1, -222, 1, 2, 1938092, -0.3])
[6]
def positive(sequence):
result = []
for element in sequence:
if element > 0:
result = result + [element*1]
return result
positive([-1, -222, 1, 2, 1938092, -0.3])
[1, 2, 1938092]
def pluses(sequence):
result = []
for element in sequence:
result = sum([])
return result
pluses([100, 200, 300])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3202/95591977.py in <module> ----> 1 pluses([100, 200, 300]) /tmp/ipykernel_3202/3050963900.py in pluses(sequence) 2 result = [] 3 for element in sequence: ----> 4 result = sum([]) 5 return result TypeError: 'int' object is not callable
sum(1, 2, 3)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3202/17035033.py in <module> ----> 1 sum(1, 2, 3) TypeError: 'int' object is not callable
pluses([1, 2, 3])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3202/831023849.py in <module> ----> 1 pluses([1, 2, 3]) /tmp/ipykernel_3202/936943801.py in pluses(sequence) 1 def pluses(sequence): ----> 2 return sum(pluses) TypeError: 'int' object is not callable
#I do not know how to add all the elements