变量和字符串拼接

python学习笔记(三)


练习3.数字和数学计算


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

 

练习4.变量和命名

 

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: 在打印输出的时候,怎样进行字符串拼接?


【Python 第14课】字符串格式化


str1 = 'good'

str2 = 'bye'

 

你可以

print str1 + str2

 

或者还可以把字符变量一个字符串相加

print 'very' + str1

print str1 + ' and ' + str2

 

但如果你想要把一个数字加到文字后面输出,比如这样

num = 18

print 'My age is' + num

程序就会报错。因为字符和数字不能直接用+相加。

 

一种解决方法是,用str()把数字转换成字符串

print 'My age is' + str(18)

num = 18

print 'My age is' + str(num)

 

还有一种方法,就是用%对字符串进行格式化

num = 18

print 'My age is %d' % num

输出的时候,%d会被%后面的值替换。输出

My age is 18

 

这里,%d只能用来替换整数。如果你想格式化的数值是小数,要用%f

print ‘Price is %f’ % 4.99

输出

Price is 4.990000

 

如果你想保留两位小数,需要在f前面加上条件:%.2f

print ‘Price is %.2f’ % 4.99

输出

Price is 4.99

 

另外,可以用%s来替换一段字符串

name = 'Crossin'

print '%s is a good teacher.' % name

输出

Crossin is a good teacher.

 

或者

print 'Today is %s.' % 'Friday'

输出

Today is 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()。


python的round函数使用

 

Q:为什么这个练习对我没有意义?

 

用你自己的数据修改脚本中的数字,看起来挺奇怪的,但是这些真实的信息能让这个练习更加真实,而且,你才刚刚开始学习,确实也不会有太大的意义,坚持做更多的练习题,你会有所收获。、

 

TIPS:

写循环时注意输入种类


考虑会否进入死循环

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值