2 + 2
4
17.35*1.15
19.9525
2
2
2+2
4
5-25
-20
2 +
File "/tmp/ipykernel_4412/4162224534.py", line 1 2 + ^ SyntaxError: invalid syntax
(2 + 2) * (3 - 1)
(5 + 5) * (3 - 1)
(5 + 5) * (8 - 1)
def double(sequence):
result = []
for element in sequence:
result = result + [element*2]
return result
double([1, 10, 5])
double([-42,0,42])
[-84, 0, 84]
double([24])
[48]
double([])
[]
double()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_4412/2608569975.py in <module> ----> 1 double() TypeError: double() missing 1 required positional argument: 'sequence'
# THE AUTHOR'S EXPLANATION: This function takes exactly one sequence as an argument and returns a sequence in which each of the original elements are doubled.
x = [1,2,3]
double(x)
double(5)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_4412/1555949217.py in <module> ----> 1 double(5) /tmp/ipykernel_4412/2548933476.py in double(sequence) 1 def double(sequence): 2 result = [] ----> 3 for element in sequence: 4 result = result + [element*2] 5 return result TypeError: 'int' object is not iterable
double([1,2,3])
double([1,2][2,4])
def triple(sequence):
result = []
for element in sequence:
result = result + [element*3]
return result
triple([2,4,5])
[6, 12, 15]
triple([7,3])
[21, 9]
def addtwo(sequence):
result = []
for element in sequence:
result = result + [element+2]
return result
addtwo([45])
[47]
addtwo([100])
[102]
def half(sequence):
result = []
for element in sequence:
result = result + [element/2]
return result
half([4,2,10,6])
[2.0, 1.0, 5.0, 3.0]
4+5+0.375
9.375
1500*0.08875
133.125
995.50*0.08875
88.350625
100*0.08875
8.875
def return(subtotal):
return subtotal * 0.08875
File "/tmp/ipykernel_4412/1390939789.py", line 1 def return(subtotal): ^ SyntaxError: invalid syntax
def tax(subtotal):
return subtotal * 0.08875
tax(1500)
1500 + tax(1500)
200 + 1500 + tax(1500)
1833.125
sum = 1500
sum = sum + sum * 0.08875 #Add the tax amount
sum
1633.125
def set_a():
a = 10
print('The value of a:', a)
set_a()
The value of a: 10
a = 20
print('Initially, the value of a:', a)
set_a()
print('Finally, the value of a:', a)
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
second = 11
third = 12
scoped(2,4)
6
first
10
second
11
third
12
scoped(1,2)
3
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
l
[7, 4, 2, 6]
def mean(sequence):
total = 0
for element in sequence:
total = total + element
return total / len(sequence)
temperature - 80
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_4412/3334237015.py in <module> ----> 1 temperature - 80 NameError: name 'temperature' is not defined
short = [5,5,5,5]
mean(short)
tall = [10, 8, 12, 18]
mean(tall)
(10 + 8 + 12 + 18)/4
age = 21, 23, 24, 26
mean(age)
x = 5
type(x)
str
type(5)
int
x = 'hello world'
type(x)
str
type('hello world')
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_4412/4108502181.py in <module> ----> 1 len(5) TypeError: object of type 'int' has no len()
2+'hello'
len(str(5))
str(2) + 'hello'
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
[4.0, 2.0, 3.0, 5.5]
result = []
result = result+[4.0]
double(volume)
[8.0, 4.0, 6.0, 11.0]
words = ['hello', 'world']
double(words)
['hellohello', 'worldworld']
countdown = [3,2,1, 'contact']
double(countdown)
[6, 4, 2, 'contactcontact']
double('abstraction')
['aa', 'bb', 'ss', 'tt', 'rr', 'aa', 'cc', 'tt', 'ii', 'oo', 'nn']
double(['abstraction', 'abstraction'])
['abstractionabstraction', 'abstractionabstraction']
2 + 2 == 4
True
2+2 == 3+1
True
2+2==2+1
False
greeting = 'hello'
greeting == 'hi'
False
def secret(word):
if word == 'please':
return 'Yes!'
secret('hello')
secret('world')
secret('please')
'Yes!'
secret('PLEASE')
def yesno(word):
if word == 'yes':
return True
if word == 'no':
return False
yesno('haha')
yesno('no')
False
yesno('yes')
True
yesno('Yes')
if word == 'please':
return 'yes!'
else:
return 'No.'
File "/tmp/ipykernel_4412/19293911.py", line 2 return'yes!' ^ SyntaxError: 'return' outside function
#Why SyntaxError "return outside funtcion"? Ask in class
def secret(word):
if word == 'please':
return True
else:
return False
secret('please')
True
secret('helloworld')
False
def secret(word):
return (word == 'please')
secret('please')
True
10/0
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) /tmp/ipykernel_4412/530406163.py in <module> ----> 1 10/0 ZeroDivisionError: division by zero
'hi'*2
'hihi'
'hihi'*2
'hihihihi'
def half(sequence):
result = []
for element in sequence:
result = result + [element/2]
return result
half([7, 9, 250])
[3.5, 4.5, 125.0]
def exclaim(sequence):
result = []
for element in sequence:
result = result + [element + '!']
return result
exclaim(['hello'])
['hello!']
exclaim(['world'])
['world!']
exclaim(['hello','world'])
['hello!', 'world!']
#ask about exercise 6-2, second step.