exercise 2-1 练习2-1:简单消息 将一条消息赋给变量,并将其打印出来。and 2-2多条简单消息 将一条消息赋给变量,并将其打印出来;再将变量的值修改为一条新消息,并将其打印出来。

在完成下面的每个练习时,都编写一个独立的程序。保存每个程序时,使用符合标准Python约定的文件名:使用小写字母和下划线,如simple_message.py和simple_messages.py。

练习2-1:简单消息 将一条消息赋给变量,并将其打印出来。

#练习2-1:简单消息 将一条消息赋给变量,并将其打印出来。
message="你好,这是《python编程:从入门到实践(第2版)》的第一个练习题,让我们一起升级打怪吧!"
print(message)

练习2-2:多条简单消息 将一条消息赋给变量,并将其打印出来;再将变量的值修改为一条新消息,并将其打印出来。

#练习2-2:多条简单消息 将一条消息赋给变量,并将其打印出来;再将变量的值修改为一条新消息,并将其打印出来。
message="你好,这是《python编程:从入门到实践(第2版)》的第一个练习题,让我们一起升级打怪吧!"
print(message)

message="这个练习的目的,是变量的使用,通过给同一个变量指向不同的值,变量都是指向最新的值"
print(message)

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 matplotlib 库绘制子图的代码: ```python import matplotlib.pyplot as plt import numpy as np # 练习 11-2 x = np.arange(0, 10, 0.1) y = np.sin(x) fig, ax = plt.subplots() ax.plot(x, y) ax.set_title('Exercise 11-2') # 练习 11-3 x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, ax = plt.subplots() ax.plot(x, y1, label='sin(x)') ax.plot(x, y2, label='cos(x)') ax.legend() ax.set_title('Exercise 11-3') # 练习 11-4 x = np.arange(0, 10, 0.1) y = np.exp(x) fig, ax = plt.subplots() ax.semilogy(x, y) ax.set_title('Exercise 11-4') # 练习 11-5 x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.tan(x) fig, ax = plt.subplots() ax.plot(x, y1, label='sin(x)') ax.plot(x, y2, label='tan(x)') ax.legend() ax.set_title('Exercise 11-5') # 练习 11-6 x = np.linspace(-10, 10, 100) y = np.sqrt(100 - x**2) fig, ax = plt.subplots() ax.plot(x, y) ax.plot(x, -y) ax.set_title('Exercise 11-6') # 练习 11-7 x = np.linspace(-10, 10, 100) y1 = x**2 y2 = np.abs(x) fig, axs = plt.subplots(1, 2) axs[0].plot(x, y1) axs[0].set_title('y=x^2') axs[1].plot(x, y2) axs[1].set_title('y=|x|') # 练习 11-8 x = np.linspace(-10, 10, 100) y1 = np.sin(x) y2 = np.exp(-x**2) fig, axs = plt.subplots(2, 1) axs[0].plot(x, y1) axs[0].set_title('y=sin(x)') axs[1].plot(x, y2) axs[1].set_title('y=exp(-x^2)') plt.show() ``` 这段代码将练习 11-2 至 11-8 的图像绘制在一个画布中,每个图像作为一个子图显示。你可以根据需要对代码进行修改,比如调整子图的排列方式等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值