Tony102's Blog - Python
type: channel
link: https://tony102.com/index.php/tag/Python/
items: Python 学习中的一些积累

Python 学习中的一些积累
type: item
date: 2023-08-08T23:24:00+08:00
description: try - except 语法这个是一个用来防止系统错误的一个好方法。比如说溢出、越界之类的错误可以用这个语法来处理。我们把可能发生的错误放在 try 模块里， 用 except 来处理异常。注意，每一个 try 都必须有一个 except 。下面是一个简单的示例：#vector indexing operations on 1-D vectors a = np.arange(10) print(a) #access an element print(f"a[2].shape: {a[2].shape} a[2] = {a[2]}, Accessing an element returns a scalar") # access the last element, negative indexes count from the end print(f"a[-1] = {a[-1]}") #indexs must be within the range of the vector or they will produce and error try: c = a[10] except Exception as e: print("The error message you'll see is:") print(e)