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])
[2, 20, 10]
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.
'hello' + '2'
'hello2'
print('Hello World')
Hello World
cd
/home/mirischoeb
ls
80-columns.ipynb public_html/ TheRules_Miriam.ipynb Miriam_4567.ipynb shared@
cd Desktop
[Errno 2] No such file or directory: 'Desktop' /home/mirischoeb
cd explore
[Errno 2] No such file or directory: 'explore' /home/mirischoeb
cd
/home/mirischoeb
ls
80-columns.ipynb public_html/ TheRules_Miriam.ipynb Miriam_4567.ipynb shared@
python hi.py
File "/tmp/ipykernel_3293/20599173.py", line 1 python hi.py ^ SyntaxError: invalid syntax
cd~
/home/mirischoeb
def to_f(c):
return c * 180/100 +32
def to_c(f):
return (5/9)*(f-32)
to_f(25)
77.0
to_c(92)
33.333333333333336
to_f(to_c(25))
25.0
to_c(to_f(90))
90.0
to_f(15)
59.0
def to_f(c):
if c < -273.15:
raise ValueError("Temperature value is below zero.")
return ((9/5)*c)+32
to_f(1)
33.8
to_f(-13)
8.599999999999998
to_f(-35)
-31.0
to_f(-280)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_3293/1067846516.py in <module> ----> 1 to_f(-280) /tmp/ipykernel_3293/129781623.py in to_f(c) 1 def to_f(c): 2 if c < -273.15: ----> 3 raise ValueError("Temperature value is below zero.") 4 return ((9/5)*c)+32 ValueError: Temperature value is below zero.
def to_c(f):
if f < -459.67:
raise ValueError("Temperature value is below zero.")
return c * 180/100 +32
to_c(15)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_3293/328908655.py in <module> ----> 1 to_c(15) /tmp/ipykernel_3293/1124446668.py in to_c(f) 2 if f < -459.67: 3 raise ValueError("Temperature value is below zero.") ----> 4 return c * 180/100 +32 NameError: name 'c' is not defined
to_c(90)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_3293/2091289154.py in <module> ----> 1 to_c(90) /tmp/ipykernel_3293/1124446668.py in to_c(f) 2 if f < -459.67: 3 raise ValueError("Temperature value is below zero.") ----> 4 return c * 180/100 +32 NameError: name 'c' is not defined
to_c(-500)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_3293/3118250758.py in <module> ----> 1 to_c(-500) /tmp/ipykernel_3293/1124446668.py in to_c(f) 1 def to_c(f): 2 if f < -459.67: ----> 3 raise ValueError("Temperature value is below zero.") 4 return c * 180/100 +32 ValueError: Temperature value is below zero.
to_c(-700)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_3293/2588592664.py in <module> ----> 1 to_c(-700) /tmp/ipykernel_3293/1124446668.py in to_c(f) 1 def to_c(f): 2 if f < -459.67: ----> 3 raise ValueError("Temperature value is below zero.") 4 return c * 180/100 +32 ValueError: Temperature value is below zero.
to_c(-500)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_3293/3118250758.py in <module> ----> 1 to_c(-500) /tmp/ipykernel_3293/1124446668.py in to_c(f) 1 def to_c(f): 2 if f < -459.67: ----> 3 raise ValueError("Temperature value is below zero.") 4 return c * 180/100 +32 ValueError: Temperature value is below zero.
to_c(60)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_3293/2307819180.py in <module> ----> 1 to_c(60) /tmp/ipykernel_3293/1124446668.py in to_c(f) 2 if f < -459.67: 3 raise ValueError("Temperature value is below zero.") ----> 4 return c * 180/100 +32 NameError: name 'c' is not defined
to_c(1)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_3293/2198870725.py in <module> ----> 1 to_c(1) /tmp/ipykernel_3293/1124446668.py in to_c(f) 2 if f < -459.67: 3 raise ValueError("Temperature value is below zero.") ----> 4 return c * 180/100 +32 NameError: name 'c' is not defined
def sign(num):
answer = '?'
if num > 0:
answer = '+'
if num < 0:
answer = '-'
if num == 0:
answer = ''
return answer
sign(4)
'+'
sign(-20)
'-'
sign(0)
''
sign(1)
'+'
def sign(num):
answer = '?'
if num > 0:
answer = '+'
elif num <0:
answer = '-'
else:
answer = ''
return answer
name = 'Paula'
name[-1]
'a'
word = 'hello'
word[-1]
'o'
word = 'Miriam'
word[-1]
'm'
def gender(name):
if name[-1]=='a':
return 'female'
if name[-1]=='o':
return 'male'
gender('paula')
'female'
gender('apollo')
'male'
gender('ladygaga')
'female'
def gender(name):
indicated = '?'
if name[-1]=='a':
indicated = 'female'
if name[-1]=='o':
indicated = 'male'
return indicated
gender('erica')
'female'
gender('miriam')
'?'
def to_chf(eur):
return eur * 1.0808
def to_eur(chf):
return chf * 0.9247
to_eur(20)
18.494
to_eur(1)
0.9247
to_chf(1)
1.0808
list(range(5))
[0, 1, 2, 3, 4]
list(range(1,6))
[1, 2, 3, 4, 5]
list(range(15,18))
[15, 16, 17]
answer = 1
for num in range(1,6):
answer = answer*num
print(answer)#display th current value
answer
1 2 6 24 120
def factorial(n):
answer = 1
for num in range(1,n+1):
answer = answer*num
return answer
factorial(5)
120
def fact(n):
if n== 0:
return 1
else:
return n*fact(n-1)
fact(5)
120
fact(0)
1
def factfail(n):
return n*factfail(n-1)
factfail(3)
--------------------------------------------------------------------------- RecursionError Traceback (most recent call last) /tmp/ipykernel_3293/2206540228.py in <module> ----> 1 factfail(3) /tmp/ipykernel_3293/1374097128.py in factfail(n) 1 def factfail(n): ----> 2 return n*factfail(n-1) ... last 1 frames repeated, from the frame below ... /tmp/ipykernel_3293/1374097128.py in factfail(n) 1 def factfail(n): ----> 2 return n*factfail(n-1) RecursionError: maximum recursion depth exceeded
factorial(-1)
1