python学习笔记(三)
print "I will now countmy chickens:"
print "Hens",25+30/6
print "Is it true that 3+2<5-7?"
print 3+2<2-7
print "Is it grester?",5>-2
PS D:\GigiLee\python\hardway_code> python ex3.py
I will now count mychickens:
Hens 30
Is it true that 3+2<5-7?
False
Is it grester? True
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_passenger_per_car = passengers/cars_driven
print "There are",cars,"cars avalible"
print "There are only",drivers,"driversavailable"
print "There will be",cars_not_driven,"empty carstoday"
print "We can transport",carpool_capacity,"peopletoday"
print "We have",passengers,"to carpool today"
print "We need to put about",average_passenger_per_car,"ineach car"
PS D:\GigiLee\python\hardway_code> python ex4.py
There are 100 cars avalible
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
Q: 我们能用x=100 代替x = 100吗?
当然可以, 但是这种写法不好。你应该在操作符的两边加上空格,这样能提高你的代码易读性。
Q: 在打印输出的时候,怎样进行字符串拼接?
这里,%d只能用来替换整数。如果你想格式化的数值是小数,要用%f
print '%s is a good teacher.' % name
print 'Today is %s.' % 'Friday'
注意区分:有引号的表示一段字符,没有引号的就是一个变量,这个变量可能是字符,也可能是数字,但一定要和%所表示的格式相一致。
Q:试着使用更多的格式化字符。
例如 %r就是是非常有用的一个,它的含义是“不管什么都打印出来”。
Q:试着使用变量将英寸和磅对应转换成厘米和千克。不要直接键入答案。使用 Python 的计算功能来完成。
#1英寸 = 2.54厘米,1磅 = 0.4536千克
my_height_centimeter = my_height * 2.54
my_weight_kilo = my_weight * 0.4536
print "He's %d centimeters tall." % my_height_centimeter
print "He's %d kilos heavy." % my_weight_kilo
PS D:\GigiLee\python\hardway_code> python ex5.py
Let's talk about Gigi Lee.
She's 62 inches tall.
She's 58 kg heavy.
Actually that's not too heavy.
She's got black eyes and black hair.
If I add 21,159,and 58 I get 238
Q:我可以定义一个类似1 = 'Zed Shaw'的变量吗?
不可以,1不是一个合法的变量名.变量需要以字母开头 ,比如a1才是正确的变量命名。
Q:怎样生成一个浮点数?
你可以像这样round(1.7333) 使用函数round()。
Q:为什么这个练习对我没有意义?
用你自己的数据修改脚本中的数字,看起来挺奇怪的,但是这些真实的信息能让这个练习更加真实,而且,你才刚刚开始学习,确实也不会有太大的意义,坚持做更多的练习题,你会有所收获。、
TIPS:
写循环时注意输入种类
考虑会否进入死循环