您的位置:首页 > 编程语言 > Python开发

笨办法学python习题4 变量和命名

2017-11-05 17:35 447 查看
变量:variable。

变量命名规则: 
 变量名的长度不受限制,但其中的字符必须是字母、数字、或者下划线(_),而不能使用空格、连字符、标点符号、引号或其他字符。
变量名的第一个字符不能是数字,而必须是字母或下划线。
Python区分大小写。
不能将Python关键字用作变量名。
代码如下:
#-*- coding:utf-8 -*-
cars = 100            #将100赋值给cars
space_in_a_car = 4.0  #将4.0赋值给space_in_a_car,一个车有4个作为
drivers = 30          #将30赋值给drivers,司机人数30
passengers = 90       #乘客人数=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"
运行结果:

mystuff simengred$ 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 in each car
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python variable