一、问题1:请简要解释Python中的列表(List)和元组(Tuple)的区别?
答:
- 列表是可变的(mutable)序列类型,可以对列表中的元素进行修改、添加、删除等操作。例如:
my_list = [1, 2, 3]
my_list.append(4)
- 元组是不可变的(immutable)序列类型,一旦创建,就不能修改元组中的元素。例如:
my_tuple = (1, 2, 3)
# 下面这行代码会引发TypeError异常
my_tuple[0] = 4
二、问题2:如何在Python中实现多线程?
答:
在Python中,可以使用标准库中的threading
模块来实现多线程操作。以下是一个简单的示例,创建两个线程分别打印不同的信息:
import threading
def print_hello():
print("Hello from thread!")
def print_world():
print("World from thread!")
if __name__ == "__main__":
t1 = threading.Thread(target=print_hello)
t2 = threading.Thread(target=print_world)
t1.start()
t2.start()
t1.join()
t2.join(