笨办法学python习题4 变量和命名
2017-11-05 17:35
447 查看
变量:variable。
变量命名规则:
变量名的长度不受限制,但其中的字符必须是字母、数字、或者下划线(_),而不能使用空格、连字符、标点符号、引号或其他字符。
变量名的第一个字符不能是数字,而必须是字母或下划线。
Python区分大小写。
不能将Python关键字用作变量名。
代码如下:
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区分大小写。
不能将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习题18 命名、变量、代码和函数
- 笨方法学Python 习题 4: 变量(variable)和命名
- 笨办法学python习题19 函数和变量
- 笨办法学python习题5更多的变量和打印
- 笨办法学python习题13 参数、解包和变量
- 笨办法学python习题4变量和变名
- 笨方法学习Python-习题18: 命名、变量、代码、函数
- 笨方法学Python 习题 18: 命名、变量、代码、函数
- 笨方法学习Python-习题4:变量(variable)和命名
- 笨办法学python习题5 更多的变量和打印
- 习题 5: 更多的变量和打印 | 笨办法学 Python
- python 中变量的命名方法
- 笨办法学python加分习题38
- 笨办法学python习题第26章答案
- python-变量命名规范
- python中变量命名的几个特例
- 笨办法学python习题8:打印,打印,打印
- 笨办法学python习题2注释和#号
- 笨办法学Python习题14 提示和传递
- 【零基础入门学习Python笔记003】插曲之变量和字符串|课习题答案