Exercise 4: Variables And Names 对用惯了C/C++等等高级语言的人来说,本节的东西会让他们感到python用起来非常简单。本节主要讲Python中变量的使用。诸如C/C++等语言都是强类型的语言,Python类型是弱类型,即不需要为变量特地指明数据类型。如下面的代码:
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."
|
其中,space_in_car变量为float类型,python在读到赋值语句之后就知道它是float类型。若是C/C++,该语句应该写成“float space_in_car = 4.0”。
时间原因,今天就先只看一个exercise吧。。。
阅读(758) | 评论(0) | 转发(0) |