Chinaunix首页 | 论坛 | 博客
  • 博客访问: 82448
  • 博文数量: 40
  • 博客积分: 1820
  • 博客等级: 上尉
  • 技术积分: 395
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-26 16:12
文章分类

全部博文(40)

文章存档

2011年(8)

2010年(32)

我的朋友

分类: Python/Ruby

2011-04-21 23:00:42

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) |
给主人留下些什么吧!~~