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

本文介绍了Python变量的命名规则,包括变量名可以包含字母、数字和下划线,首字符不能为数字,区分大小写,以及避免使用关键字作为变量名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

变量:variable。

变量命名规则: 

  1.  变量名的长度不受限制,但其中的字符必须是字母、数字、或者下划线(_),而不能使用空格、连字符、标点符号、引号或其他字符。
  2. 变量名的第一个字符不能是数字,而必须是字母或下划线。
  3. Python区分大小写。
  4. 不能将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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值