*笨办法学python3 学习笔记 习题4-6


习题4 变量和命名


# 汽车辆数为100
cars = 100
# 车内座位数为4.0
space_in_a_car = 4.0
# 司机人数为30
drivers = 30
# 乘客人数为90
passengers = 90
# 无司机的汽车辆数 = 汽车辆数 - 司机人数
cars_not_driven = cars - drivers
# 有司机的汽车辆数 = 司机人数
cars_driven = drivers
# 可运载量 = 有司机的汽车辆数 * 车内座位数
carpool_capacity = cars_driven * space_in_a_car
# 每辆车的平均载客量 = 乘客数 / 有司机的汽车辆数
average_passengers_per_car = passengers / cars_driven


# 以下打印字符串及变量
print("There are", cars, "cars available.")
print("There are only", drivers, "drivers available.")
print("There will be", cars_not_driven, "empty cars today.")
print("We can transport", carpool_capacity, "people today.")
print("We have", passengers, "to carpool today.")
print("We need to put about", average_passengers_per_car, "in each car.")
PS D:\pythonp> python ex4.py
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3.0 in each car.


  1. 解释出错信息

# 错误的跟踪信息
Traceback (most recent call last):
# 文件ex4.py的第8行有一个错误,错误来源于此执行模块
    File "ex4.py", line 8, in <module>
# 出错语句为average_passengers_per_car = car_pool_capacity / passenger
        average_passengers_per_car = car_pool_capacity / passenger
# 名称错误:变量car_pool_capacity未被定义
NameError: name 'car_pool_capacity' is not defined

  1. 有必要使用4.0作为space_in_a_car的值吗?如果只用4会发生什么?
  • 4.0是“浮点数”。
  • 没有必要,因为汽车内座位数只可能是整数,数值不可能更精确。
  • 若只用4,则carpool_capacity输出数值为120




=:等于

  • 作用

为数据(数值、字符串等)取名,将右边的值赋给左边的变量名


==:比较等于

  • 作用

检查左右两边的值是否相等


_:下划线字符(underscore)

  • 作用

在变量里通常被用作假象的空格,用来隔开单词



习题5 更多的变量和打印


my_name ='Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hari = 'Brown'

print(f"Let's talk about {my_name}.")
print(f"He's {my_height} inches tall.")
print(f"He's {my_weight} pounds heavy.")
print("Actually that's not too heavy.")
print(f"He's got {my_eyes} eyes and {my_hari} hair")
print(f"His teeth are usually {my_teeth} depending on the coffee.")

# this line is tricky, try to get it exactly right.

total = my_age + my_height + my_weight
print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.")
PS D:\pythonp> python ex5.py
Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.

  1. 试着使用变量将英寸和磅转换成厘米和千克
name ='Zed A. Shaw'
age = 35 # not a lie
height1 = 74 # inches
height = height1 * 2.54 #cm
weight1 = 180 # lbs
weight = weight1 * 0.4535924 #kg
eyes = 'Blue'
teeth = 'White'
hari = 'Brown'

print(f"Let's talk about {name}.")
print(f"He's {height} cm tall.")
print(f"He's {weight} kg heavy.")
print("Actually that's not too heavy.")
print(f"He's got {eyes} eyes and {hari} hair")
print(f"His teeth are usually {teeth} depending on the coffee.")

# this line is tricky, try to get it exactly right.

total = age + height + weight
print(f"If I add {age}, {height}, and {weight} I get {total}.")
PS D:\pythonp> python ex5.py
Let's talk about Zed A. Shaw.
He's 187.96 cm tall.
He's 81.646632 kg heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair
His teeth are usually White depending on the coffee.
If I add 35, 187.96, and 81.646632 I get 304.606632.


习题6 字符串和文本


types_of_people = 10
x = f"There are {types_of_people} types of people."

binary = "binary"
do_not = "don't"
y = f"There who know {binary} and those who {do_not}."

print(x)
print(y)

print(f"I said: {x}")
print(f"I also said: '{y}'")

hilarious = "False"
joke_evaluation = "Isn't that joke so funny?! {}"

print(joke_evaluation.format(hilarious))

w = "This is the left side of..."
e = "a string with a right side."

print(w + e)
PS D:\pythonp> python ex6.py
There are 10 types of people.
There who know binary and those who don't.
I said: There are 10 types of people.
I also said: 'There who know binary and those who don't.'
Isn't that joke so funny?! False
This is the left side of...a string with a right side.

  1. 每一行的上面使用#为自己写一个注释,说明这一行的作用
# 设置变量types_of_people,值为10
types_of_people = 10
# 设置x为格式化字符串
x = f"There are {types_of_people} types of people."

# 定义两变量
binary = "binary"
do_not = "don't"
# 设置y为格式化字符串
y = f"There who know {binary} and those who {do_not}."

# 打印x,y
print(x)
print(y)

# 打印格式化字符串
print(f"I said: {x}")
print(f"I also said: '{y}'")

# 设置布尔型变量
hilarious = "False"
# 需要将该字符串格式化,所以预先在字符串内部为变量留出位置
joke_evaluation = "Isn't that joke so funny?! {}"

# 输出格式化字符串
# 取joke_evaluation字符串,调用它的format函数,将hilarious作为参数传递给format函数,令其与joke_evaluation变量中的{}匹配
# 其结果是一个新字符串,原变量中的{}被hilarious变量替换掉
print(joke_evaluation.format(hilarious))

# 设置变量
w = "This is the left side of..."
e = "a string with a right side."

# 合并两变量并输出
print(w + e)

  1. 找出所有“把一个字符串放进另一个字符串”的位置,共有四处
# 第一处
x = f"There are {types_of_people} types of people."
# 第二处
y = f"There who know {binary} and those who {do_not}."
# 第三处
print(f"I said: {x}")
# 第四处
print(f"I also said: '{y}'")

  1. 确定只有4处吗?

确定只有四处,余下一处形似的为格式化布尔型变量

# 设置布尔型变量
hilarious = "False"
# 该行代码去掉{}则hilarious不输出,可能是为布尔型变量预留位置?不清楚
joke_evaluation = "Isn't that joke so funny?! {}"

# 格式化一个布尔型变量
print(joke_evaluation.format(hilarious))

  1. 解释一下为什么w和e用+连起来就可以生成一个更长的字符串。

+可以作连接用。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
办法Python3》是一本教授Python编程基础的书籍,它分为七个部分,分别是打印与输入、文件操作、函数、数据容器与程序结构、面向对象的类、项目骨架与自动测试以及搭建简易的网站。这本书通过一系列的练习和示例,帮助读者逐步学习Python的基本语法和编程概念。 在第一部分《打印与输入》中,你将学习如何使用print函数打印输出内容,以及如何使用input函数获取用户的输入。 在第二部分《文件操作》中,你将学习如何打开、读取和写入文件,以及如何处理文件中的数据。 在第三部分《函数》中,你将学习如何定义和调用函数,以及如何传递参数和返回值。 在第四部分《数据容器与程序结构》中,你将学习如何使用列表、字典和元组等数据容器,以及如何使用条件语句和循环结构控制程序的执行流程。 在第五部分《面向对象的类》中,你将学习面向对象编程的基本概念,包括类的定义、对象的创建和方法的调用。 在第六部分《项目骨架与自动测试》中,你将学习如何使用项目骨架来组织你的代码,并学习如何编写自动化测试来验证代码的正确性。 在第七部分《搭建简易的网站》中,你将学习如何使用Python搭建一个简单的网站,并学习一些与网站开发相关的知识。 通过《办法Python3》,你可以系统地学习Python编程的基础知识,并通过练习提升自己的编程能力。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值