Python3

发布时间 2023-11-15 14:18:28作者: latent_Lin
import numpy as np
x = np.array([1.0, 2.0, 3.0, 4.0])
y = x + 2
Ans:
array([3., 4., 5., 6.])
y = x > 2.0
Ans:
array([False, False, True, True])

 

A. SyntaxError - also known as parsing error. Python does not like your structure.
It is like Dr F asks you to redo your essay because of your format is wrong or missing dates.
Examples: 
    Missing ’:’, ’()’ etc. 
Displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected but maybe not be where the error is!
B. Exceptions - errors found during running
A NameError - name of function or variables notfound 
B TypeError - operations on different data types
C.ZeroDivisionError
D.FileNotFoundError
E.IndexError - wrong index used 
F. KeyError - in dictionary
G IndentationError - wrong or no indent 
H .... More
This is like Dr F accepted your essay format and started reading it but found a lot of weird problems. So, he asked you to correct your essay.
Part I:
A. SyntaxError
— check missing colons or parentheses
B. NameError
— check for typos, function definitions
C. TypeError
— check variable types
D. ValueError
— check function arguments
E. FileNotFoundError
— check that files exist

Part II:
A. IndexError
— check for nonexistent list elements (sometimes python allows and does show this error)
B. KeyError
— similar to an IndexError, but for dictionaries
C. ZeroDivisionError
D. IndentationError
— check the number of spaces and tabs infront of the commands are not mixed

 AssertionError

'2'+2 TypeError

Most of the time, we do not want errors to happen.
But it happens! Next best thing is we do not want our 
program to crash (stop executing)!
We can tell Python to try a block of code, and it will run 
normally except if something goes wrong.
# calculate square roots 
d = list( range( 10 ) ) 
r = []
for i in d: 
    try:
        r[ i ] = sqrt( d[ i ] ) 
    except:
        print( ’An error occurred.’ ) 
        break    

try:
    # the main code
    # if an error occurs, it goes into ”except:”
    immediately
except:
    # an error occurs
else: (optional)
    # if no error occurs
finally: (optional)
    # this always happens, error or no error
Note: Using except: or except XXXError: both will work.
XXXError is from the list of errors/exceptions in Python (like 
those at the first few slides in this lecture)

 

 

 

 

 

 

D. Debugging strategies
A. Start early.
B. Read the problem statement carefully.
C. Write down on paper what you need to do STEP-BYSTEP.
D. Add print statements.
E. Break the program down into parts (e.g., write functions).
F. Document functions before writing them. (# Put comments 
to remind yourself what you are trying to do)
G. Explain it to someone else.
H. Make no assumptions! If your thinking is not precise, your 
code will not be precise.
I. Start over from scratch. Take a fresh look at the problem.
Style
Why do we write comments?
For the person who next looks at the code! Also for YOU 
after you wrote the code a long time ago!
x_vals = [0,0.1,0.2,0.3,0.4] # in meters

Use descriptive variable names. 
pi = 3.141

 0.3用浮点数表示……

 0.010->0.01

0.011->0.10