笨方法教你学python_笨方法学Python(1)

习题 1: 第一个程序(略)

Warning

如果你来自另外一个国家,而且你看到关于 ASCII 编码的错误,那就在你的 python 脚本的最上面加入

这一行:

# -*- coding: utf-8 -*-

这样你就在脚本中使用了 unicode UTF-8 编码,这些错误就不会出现了。

语法错误(SyntaxError)

习题2:注释和井号(略)

习题3:数字和数字计算

习题4:变量和命名

习题 5: 更多的变量和打印(格式化字符串)my_name = 'Zed A. Shaw'

my_age = 35 # not a lie

my_height = 74 # inches

my_weight = 180 # lbs

my_eyes = 'Blue'

my_teeth = 'White'

my_hair = 'Brown'

print "Let's talk about %s." % my_name

print "He's %.2f kilometres tall." % (my_height*2.54)

print "He's %.2f kilogrammes heavy." % (0.4535924*my_weight)

print "Actually that's not too heavy."

print "He's got %s eyes and %s hair." % (my_eyes, my_hair)

print "His teeth are usually %s depending on the coffee." % my_teeth

# this line is tricky, try to get it exactly right

print "If I add %d, %d, and %d I get %d." % (my_age, my_height, my_weight, my_age + my_height + my_weight)习题6:字符串和文本

%r 用来做 debug 比较好,因为它会显示变量的原始数据(raw data),而其它的符

号则是用来向用户显示输出的。

习题7:更多打印

习题8:打印打印formatter='%r %r %r %r'

print formatter % (1,2,3,4)

print formatter % ('one','two','three','four')

print formatter % (True,False,False,True)

print formatter % (formatter,formatter,formatter,formatter)

print formatter % (

"I had this thing.",

"That you could type up right.",

"But it didn't sing.",

"So I said goodnight."

)

1 2 3 4

'one' 'two' 'three' 'four'

True False False True

'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'

'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'

习题 9: 打印,打印,打印(\n)print 'How old are you ? '

age = int(float(raw_input()))

print 'How tall are you ? '

height = raw_input()

print 'How much do you weight ? '

weight = raw_input()

print 'So,you are \'%s\' old,%r tall and %r heavy.' % (age,height,weight)或者:#int(input())

其实input也是调用了raw_input,只是做了eval处理

而eval有什么作用呢?

input:会根据用户的输入来做类型的转换

raw_input:则会把用户的输入都作为一个字符串来处理

input它会根据用户输入变换相应的类型,而且如果要输入字符和字符串的时候必须要用引号包起来

raw_input则是不管用户输入什么类型的都会转变成字符型

习题 10: 那是什么?

习题 11: 提问

习题 12: 提示别人

习题 13: 参数、解包、变量

习题 14: 提示和传递

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值