这几年,校园或者小区,快递柜是越来越多了。快递小哥会把我们的快递直接放到快递箱了,我们就会收到短信或者微信的消息推送,科技使我们的生活越来越便捷了。

快递箱是承载快递的载体,而变量就是承载各种值的载体,变量就相当于一个快递箱,而变量的值相当于快递箱中的快递。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
cars = 100
space_in_a_car = 4.0
drivers = 30
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.")
上面这段程序,上面的一部分,主要定义了司机载客的一系列的变量,下面的部分则是将定义的这些变量,以一种人类可读的方式输出出来。
运行程序,验证下结果

本习题中的程序不难,但是需要关注的问题是:Python中变量的命名。在Python中的变量名可以包括字母、数字、下划线,但是数字不能做为开头。
小结
- Python中变量的命名规则。
网友评论