- 博客(41)
- 收藏
- 关注
原创 键盘输入监听
Python键盘输入键位码表:字母和数字键 数字小键盘的键 功能键 其它键 键 键码 键 键码 键 键码 键 键码 A 65 0 96 F1 112 Backspace 8 B 66
2017-03-19 05:41:53 461
原创 ex44 继承还是组成
继承的使用原则:我们要尽量简单的使用继承,或者用组成代替继承,而且做好不要使用多继承。父类和子类有三个相互影响的方法:子类的动作暗含(imply)了父类的动作子类的动作覆盖(override)了相应的父类动作子类的动作改变(alter)了相应的父类动作暗含继承 Implicit Inheritance先在父类定义一个implicit函数,子类什么也不做。Fi
2017-01-30 13:26:55 271
原创 exercise 43 基本的面向对象分析和设计
用python面向对象编程的过程(一般可以这么用,不一定所有问题都是这个思路解决)1.Write or draw about the problem. 写下或画出问题2.Extract key concepts from 1 and research them.从1开始提取关键概念并研究他们3.Create a class hierarchy and object map for th
2017-01-23 14:30:42 483
原创 exercise 42 继承 包含 对象和类
is-a就是对象和类之间通过类的关系相关联;继承has-a是对象和类相关联是因为他们彼此参考。包含is-a是三文鱼和鱼的关系;has-a是三文鱼和鳃的关系总结:记住!!一个新的顶级类必须继承object## Animal is-a object (yes, sort of confusing) look at the extra creditclass A
2017-01-23 12:36:47 373
原创 exercise 41 学习面向对象
对一些词语的理解:class:告诉python创造一个新的东西object:两个意思:最基本的东西和任何实例化的东西。instance:创建一个类得到的东西。def:在类中创建一个函数。self:在类里面的函数中使用,是实例和object能访问的变量。inheritance:继承,一个类可以继承另一个类,像你和你的父母。composition:一个类可以包含另外一个类,就像汽车包含轮胎。a
2017-01-19 03:53:13 494
原创 exercise 40 模块 类 对象
一、模块:一个python文件,里面有很多变量和函数你可以导入这个文件你可以用 . 来访问模块中的函数和变量范例:想象一下,我有一个包含apple函数和变量tangerine的模块文件mystuff.py:def apple(): print "I am apples!"tangerine = "Living reflection of a dre
2017-01-16 14:38:31 335
原创 exercise 39 字典
list的使用:>>> things = ['a', 'b', 'c', 'd']>>> print things[1]b>>> things[1] = 'z'>>> print things[1]z>>> things['a', 'z', 'c', 'd']>>> stuff = {'name': 'Zed', 'age': 39, 'heig
2017-01-16 09:50:03 209
原创 exercise 38 列表操作
ten_things="Apples Orange Crows Telephone Light Sugar" # no comma between two wordsprint "Wait there are not 10 things in that list. Let's fix that."more_stuff="Day Night Song Frisbee Corn Banana
2017-01-16 09:34:15 407
原创 exercise 35 分支和函数-待续
from sys import exitdef gold_room(): print "This room is full of gold. How much do you take?" choice = raw_input("> ") if "0" in choice or "1" in choice: how_much = int(choice)
2017-01-15 13:07:24 328
转载 exercise 37 符号复习-待续
KeywordsKeywordDescriptionExampleandLogical and.True and False == FalseasPart of the with-as statement.with X as Y: passassertAssert (ensure) that somethi
2017-01-15 03:04:10 381
原创 exercise 36 设计和调试-作业待续
if语句的规则1. if语句中必须要有else2. 如果因为else语句没有任何意义,那么可以在else里面打印错误信息,并且停止程序。3. 不要嵌套if语句超过两层,最好是把里面的if语句写到另外的函数中。4. 给if语句的前后留空白的行,让它看上去像一个段落。5. 如果if中的布尔运行很复杂的话,把它们写到一个函数中。家庭作业用一个星期的时间写一个类似上
2017-01-15 02:36:27 307
原创 exercise 34 访问列表中的元素
animals=['bear','python','peacock','kangaroo','whale','platypus']The animal at 1. pythonThe 3rd animal. peacockThe 1st animal. bearThe animal at 3. kangarooThe
2017-01-14 11:38:18 429
原创 exercise 33 while循环
while循环有可能一直不会停止,所以我们列出了一下规则:不到万不得已不要使用while,可以用for代替。仔细检查你的while声明,确保有条件让它返回False。如果有怀疑的话,在代码段的头部和底部打印变量的值来判别。i = 0numbers = []while i 6: print "At the top i is %d" % i numbers
2017-01-14 11:14:36 266
原创 exercise 32 列表和循环
the_count = [1, 2, 3, 4, 5]fruits = ['apples', 'oranges', 'pears', 'apricots']change = [1, 'pennies', 2, 'dimes', 3, 'quarters']# this first kind of for-loop goes through a listfor number in the
2017-01-14 02:47:25 308
转载 exercise 31 做决定
print "You enter a dark room with two doors. Do you go through door #1 or door #2?"door = raw_input("> ")if door == "1": print "There's a giant bear here eating a cheese cake. What do you do
2017-01-12 05:00:55 261
原创 exercise 30 else and if 语句
people = 30cars = 40trucks = 15if cars > people: print "We should take the cars."elif cars < people: print "We should not take the cars."else: print "We can't decide."if trucks >
2017-01-12 04:44:59 276
原创 exercise 29
people = 20cats = 30dogs = 15if people < cats: print "Too many cats! The world is doomed!"if people > cats: print "Not many cats! The world is saved!"if people < dogs: print "The
2017-01-12 04:13:02 253
原创 exercise28 布尔运算练习
True and True trueFalse and True false1 == 1 and 2 == 1 false"test" == "test" true1 == 1 or 2 != 1 trueTrue and 1 == 1 trueFalse and 0 != 0 falseTrue or 1 == 1 true"test" == "test
2017-01-12 04:05:22 1244
原创 exercise 27 记忆逻辑关系
真值术语在python中,我们使用下面的术语去判断一个东西的True和False,计算机的逻辑判断就是当某些字符和变量组合在一起的时候是True还是False。andornot!===>=TrueFalse真值表NOTTrue?not FalseTruenot TrueFalse
2017-01-12 03:53:57 612
原创 exercise 26 test
def break_words(stuff): """This function will break up words for us.""" words = stuff.split(' ') return wordsdef sort_words(words): """Sorts the words.""" return sorted(words)de
2017-01-12 02:56:41 267
原创 exercise 25 更多更多练习
def break_words(stuff): """This function will break up words for us.""" words = stuff.split(' ') #单引号中表示分隔符 默认为空格 不可为空 return wordsdef sort_words(words): """Sorts the words."""
2017-01-11 03:41:01 292
原创 exercise 24 更多练习
print "Let's practice everything."print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'poem = """\tThe lovely worldwith logic so firmly plantedcannot discern \n the
2017-01-11 02:53:03 200
原创 exercise 22&23 总结+读代码
Here's what you do:Go to bitbucket.org, github.com, or gitorious.org with your favorite web browser and search for "python."Pick a random project and click on it.Click on the Source tab and browse
2017-01-09 12:36:20 275
原创 exercise 21 函数能返回信息
def add(a, b): print "ADDING %d + %d" % (a, b) return a + bdef subtract(a, b): print "SUBTRACTING %d - %d" % (a, b) return a - bdef multiply(a, b): print "MULTIPLYING %d *
2017-01-09 12:23:44 198
原创 exercise 20 函数和文件
from sys import argvscript, input_file = argvdef print_all(f): print f.read()def rewind(f): f.seek(0) ##seek参数为0时 表示将文件指针移到起始位置---- 同倒带rewinddef print_a_line(line_count, f):
2017-01-08 13:38:22 277
原创 exercise 19 函数和变量
##########################################ex19def cheese_and_crackers(cheese_count, boxes_of_crackers): print "You have %d cheeses!" % cheese_count print "You have %d boxes of crackers!" %
2017-01-08 10:14:56 257
原创 exercise18 命名 变量 代码 函数
函数做3件事:他们给代码命名,就像给字符串和数字赋给一个变量名。他们接收参数,就像脚本中的argv。使用第一和第二条实现你自己的迷你脚本或者小型方法。# this one is like your scripts with argvdef print_two(*args): #给函数定义了一个变量*args arg1, arg2 = args
2017-01-08 08:32:50 214
原创 exercise 17 读取文件_2
源程序:from sys import argvfrom os.path import exists ## 从os.path模块中导入existsscript, from_file, to_file = argvprint "Copying from %s to %s" % (from_file, to_file)# we could do these two on
2017-01-08 04:03:27 343
转载 os.path 模块
os.path.abspath(path) #返回绝对路径os.path.basename(path) #返回文件名os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径。os.path.dirname(path) #返回文件路径os.path.exists(path) #路径存在则返回True,路径损坏返回False
2017-01-08 04:01:17 181
原创 exercise17 更多文件
源程序:from sys import argvfrom os.path import exists ##从os.path模块中导入exists命令script from_file, to_file = argvprint "Coping from %s to %s" %(from_file, to_file)in_file=open(from_file)in
2017-01-08 02:47:24 327
原创 exercise16 读写文件
需要记住的常用命令:close -- 关闭文件,相当于编辑器中的File->Saveread -- 读取文件内容分配给一个变量readline -- 读取一行内容truncate -- 清空文件,小心使用这个命write(stuff) -- 写入文件。python open 使用方法f=open('/tmp/hello','w')#open(路径+文件名
2017-01-04 08:54:06 188
原创 exercise15 读取文件
运行文件ex15.pyex15_sample.txt //记事本的内容This is stuff I typed into a file.It is really cool stuff.Lots and lots of fun to have in here.from sys import argvscript, filename=argv
2017-01-04 08:34:00 207
原创 exercise14 提示 传递
script, user_name = argvprompt = '> ' #加一个提示print "Hi %s, I'm the %s script." % (user_name, script)print "I'd like to ask you a few questions."print "Do you like me %s?" % user_namelikes =
2017-01-04 08:24:07 342
原创 exercsie13 参数 解包 变量
from sys import argv #从python功能库中导入功能 “features"真正的名称是:modules / librariesscript, first, second, third = argv # argv是“参数变量”,解变量print "The script is called:", sc
2017-01-04 03:32:11 285
原创 exercise 12 提示
age = raw_input("How old are you? ")height = raw_input("How tall are you? ")weight = raw_input("How much do you weigh? ")print "So, you're %r old, %r tall and %r heavy." % ( age, height, we
2017-01-04 03:24:10 255
原创 exercise11
print"How old are you?", #print 后面逗号表示 在屏幕显示的时候 键入在同一行,如果没有逗号 键入会新起一行age = raw_input()print"How tall are you?",height= raw_input()print"How much do you weigh?",weight= raw_input(
2017-01-04 03:04:45 261
原创 exercise 10
print "I am 6'2\" tall." print 'I am 6\'2" tall.'tabby_cat="\t I'm tabbed in." persian_cat="I'm split\non a line."backslash_cat="I'm \\ a \\ cat."fat_cat = """I'll do a list:\t* Cat food
2016-12-27 08:32:30 258
原创 exercise9
days = "Mon Tue Wed Thu Fri Sat Sun"months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"print "Here are the days: ", days # print语句和显示的变量之间有“,”号 exercise3中也有类似语句,但没注意过print "Here are the months: ", m
2016-12-27 07:34:05 207
原创 ex8
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, form
2016-12-27 07:19:37 274
原创 exercise7 more printing
print "Mary had a little lamb."print "Its fleece was white as %s." % 'snow'print "And everywhere that Mary went."print "." * 10 输出10 个.end1 = "C"end2 = "h"end3 = "e"end4 = "e"end5 = "s"end6
2016-12-27 07:05:20 362
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人