码迷,mamicode.com
首页 > 编程语言 > 详细

笨方法学Python(1)

时间:2016-03-09 01:27:39      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

习题 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: 提示和传递

 


 

笨方法学Python(1)

标签:

原文地址:http://www.cnblogs.com/Unix-Python/p/5256388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!