print('Hello World')
Hello World
def to_f(c):
return (9/5*c)+32
to_f(15)
59.0
to_f(0)
32.0
to_f(100)
212.0
def to_c(f):
return (5/9)*(f-32)
to_c(32)
0.0
to_f(to_c(25))
25.0
def to_f(c):
return (9/5*c)+32
def to_f(c):
if c<-273.15
raise ValueError("Temperature value is below absolute zero.")
return ((9/5)*c)+32
File "/tmp/ipykernel_3490/2670143301.py", line 2 if c<-273.15 ^ SyntaxError: invalid syntax
to_f(-274)
-461.2
def sign(num):
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'
'world'[-1]
'd'
def gender(name):
if name[-1]=='a':
return 'female'
if name[-1]=='o':
return 'male'
gender('Apollo')
'male'
gender('Nick')
def gender(name):
indicated='?'
if name[-1]=='a':
indicated= 'female'
if name[-1]=='o':
indicated='male'
return indicated
gender('Nick')
'?'
def tocat(hmn):
return (hmn*7)
tocat(15)
105
list(range(5))
[0, 1, 2, 3, 4]
list(range(0,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 the current value
answer
1 2 6 24 120
120
def factorial(n):
answer=1
for num in range(1,n+1):
answer=answer*num
return answer
factorial(15)
1307674368000
def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
fact(15)
1307674368000
def factfail(n):
return n*factfail(n-1)
factfail(3)
--------------------------------------------------------------------------- RecursionError Traceback (most recent call last) /tmp/ipykernel_3490/2206540228.py in <module> ----> 1 factfail(3) /tmp/ipykernel_3490/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_3490/1374097128.py in factfail(n) 1 def factfail(n): ----> 2 return n*factfail(n-1) RecursionError: maximum recursion depth exceeded
if n<0:
raise ValueError('undefined result')
return answer
factorial(-21)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_3490/1721965092.py in <module> ----> 1 factorial(-21) /tmp/ipykernel_3490/2416778145.py in factorial(n) 4 answer=answer*num 5 if n<0: ----> 6 raise ValueError('undefined result') 7 return answer ValueError: undefined result
def fact(n):
if n<0:
raise ValueError('undefined result')
if n==0:
return 1
else:
return n*fact(n-1)
fact(-1)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_3490/2874172109.py in <module> ----> 1 fact(-1) /tmp/ipykernel_3490/3327227017.py in fact(n) 1 def fact(n): 2 if n<0: ----> 3 raise ValueError('undefined result') 4 if n==0: 5 return 1 ValueError: undefined result
def doubler(sequence):
if len(sequence)==0:
return[]
else:
return[2*sequence[0]]+doubler(sequence[1:])
doubler(4)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3490/3336296941.py in <module> ----> 1 doubler(4) /tmp/ipykernel_3490/2319938741.py in doubler(sequence) 1 def doubler(sequence): ----> 2 if len(sequence)==0: 3 return[] 4 else: 5 return[2*sequence[0]]+doubler(sequence[1:]) TypeError: object of type 'int' has no len()