Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
8 views
Kernel: Unknown Kernel
x = float(raw_input('Enter a number: '))
Enter a number: seven
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-1-61ced4682ade> in <module>() ----> 1 x = float(raw_input('Enter a number: ')) ValueError: could not convert string to float: seven
try: s = raw_input('Enter a number: ') x = float(s) except ValueError: print("%s cannot be converted to floating point number." % s)
Enter a number: seven seven cannot be converted to floating point number.
list = [1,2,3,4] print(list[4])
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-2-8d9c2d533693> in <module>() 1 list = [1,2,3,4] ----> 2 print(list[4]) IndexError: list index out of range
from math import sqrt sqrt(-2)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-03202d5db7db> in <module>() 1 from math import sqrt ----> 2 sqrt(-2) ValueError: math domain error
x = 0 y = 2 a = y/x
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-4-bb76b55e818d> in <module>() 1 x = 0 2 y = 2 ----> 3 a = y/x ZeroDivisionError: integer division or modulo by zero
a = 2 print(A)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-5-23441cc05d89> in <module>() 1 a = 2 ----> 2 print(A) NameError: name 'A' is not defined
a =* 2
File "<ipython-input-6-d6ea3953da97>", line 1 a =* 2 ^ SyntaxError: invalid syntax
a = '2' b = 3.0 try: print(a*b) except TypeError: print('Please do not multiply strings by floats.')
Please do not multiply strings by floats.
a = 'abc' b = 10 print(a*b)
abcabcabcabcabcabcabcabcabcabc
value = 0 counter = 0 while value is 0 and counter < 3: try: value = float(raw_input("Enter a number: ")) except ValueError: print('That\'s not a number.') counter += 1 print(value)
Enter a number: seven That's not a number. Enter a number: aardvaark That's not a number. Enter a number: seven aardvaarks That's not a number. 0
def leftInt(f,a,b,n=100): if type(a) != float: a = float(a) #Will raise an exception if a cannot be converted to float. if type(b) != float: b = float(b) #Will raise an exception if b cannot be converted to float. if (type(n) != int) or (n <= 0): raise ValueError("number of rectangles must be a positive integer") #We don't want to convert n into an int...if the user passed in a non-int or negative, there clearly is a problem. deltax = (b-a)/n x = a sum = 0 for i in range(n): try: sum += f(x) except TypeError: raise TypeError("integrand is not a valid function") #Sometimes we want to catch a particular exception and re-raise it with a more specific error message. x += deltax return deltax*sum
def f(x): return 3*x**2 leftInt(f,0,4,3.5)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-1f37c601bc1e> in <module>() 2 return 3*x**2 3 ----> 4 leftInt(f,0,4,3.5) <ipython-input-2-b28beb750b2d> in leftInt(f, a, b, n) 5 b = float(b) #Will raise an exception if b cannot be converted to float. 6 if (type(n) != int) or (n <= 0): ----> 7 raise ValueError("number of rectangles must be a positive integer") #We don't want to convert n into an int...if the user passed in a non-int or negative, there clearly is a problem. 8 deltax = (b-a)/n 9 x = a ValueError: number of rectangles must be a positive integer
def g(x): return "You're a jerk!" leftInt(g,0,4,3)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-2b6f8aa6e3b6> in <module>() 2 return "You're a jerk!" 3 ----> 4 leftInt(g,0,4,3) <ipython-input-2-b28beb750b2d> in leftInt(f, a, b, n) 13 sum += f(x) 14 except TypeError: ---> 15 raise TypeError("integrand is not a valid function") #Sometimes we want to catch a particular exception and re-raise it with a more specific error message. 16 x += deltax 17 return deltax*sum TypeError: integrand is not a valid function
import L11approximations def f(x): return x**3 + 2*x a = 0 b = 4 print(L11approximations.leftInt(f,0,4,10)) print(L11approximations.rightInt(f,0,4,10)) print(L11approximations.trapInt(f,0,4,10))
66.24 95.04 80.64
from L11approximations import * print(leftInt(f,0,4,100))
78.5664
import numpy as np dataList = [1,2,3,4,5,6] dataArray = np.array(dataList) print(dataArray)
[1 2 3 4 5 6]
dataList = [1,2,3.1,4,5,6] dataArray = np.array(dataList) print(dataArray)
[ 1. 2. 3.1 4. 5. 6. ]
[1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
dataArray2 = np.array([1,2,3,4,5.0,6.2]) dataArray = dataArray + dataArray2 print(dataArray)
[ 2. 4. 6.1 8. 10. 12.2]
zeroArray = np.zeros(12) print(zeroArray)
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
intervalArray = np.linspace(1,5,20) print(intervalArray)
[ 1. 1.21052632 1.42105263 1.63157895 1.84210526 2.05263158 2.26315789 2.47368421 2.68421053 2.89473684 3.10526316 3.31578947 3.52631579 3.73684211 3.94736842 4.15789474 4.36842105 4.57894737 4.78947368 5. ]
def g(x): return x**3 print(g(intervalArray))
[ 1. 1.77387374 2.8696603 4.34334451 6.25091121 8.64834524 11.59163143 15.13675463 19.33969966 24.25645138 29.94299461 36.45531419 43.84939496 52.18122175 61.50677941 71.88205278 83.36302668 96.00568596 109.86601545 125. ]
from numpy import * print(exp(intervalArray))
[ 2.71828183 3.35525011 4.1414776 5.11193983 6.30980809 7.78836987 9.61339939 11.86608357 14.64663368 18.07874325 22.31509059 27.54413077 33.99847904 41.96525883 51.79887449 63.93677707 78.91892444 97.41180148 120.23806881 148.4131591 ]
print(cos(pi*intervalArray))
[-1. -0.78914051 -0.24548549 0.40169542 0.87947375 0.9863613 0.67728157 0.08257935 -0.54694816 -0.94581724 -0.94581724 -0.54694816 0.08257935 0.67728157 0.9863613 0.87947375 0.40169542 -0.24548549 -0.78914051 -1. ]
print(dot(intervalArray,intervalArray)) print(dot(np.array([1,2,3,4]), np.array([1,1,1,1])))
209.473684211 10
print(cross(array([1,2,3]),array([2,5,7])))
[-1 -1 1]
A = array([[1,2,3],[3,-4,5],[5,6,7]]) print(A)
[[ 1 2 3] [ 3 -4 5] [ 5 6 7]]
eigenvalues, eigenvectors = linalg.eig(A) print(eigenvalues) print(eigenvectors)
[ 11.25664784 -0.89351035 -6.36313749] [[ 0.32531663 0.85160988 0.09637145] [ 0.35164354 -0.02060987 -0.92099553] [ 0.87779036 -0.5237708 0.37746494]]
linalg.det(A)
63.999999999999979
print(A*A)
[[ 1 4 9] [ 9 16 25] [25 36 49]]
print(dot(A,A))
[[22 12 34] [16 52 24] [58 28 94]]