学习笔记
weixin_45938096
这个作者很懒,什么都没留下…
展开
-
笨办法学python3 学习笔记 习题43 基本的面向对象分析和设计
习题43 基本的面向对象分析和设计通过面向对象编程(OOP)构建一些东西的流程(常用)(自顶向下)把要解决的问题写下来,或者画出来。将第一条中的关键概念提取出来并加以研究。创建一个类层次结构和对象图。用代码实现各个类,并写一个测试来运行它们。重复上述步骤并细化代码。(自底向上)取出要解决的问题中的一小块,写些代码让它差不多能工作。加上类和自动测试,细化代码让它更为正式。把关键概念抽取出来然后研究它们。把真正需要实现的东西描述出来。回去细化代码,有可能需要全部丢弃重头做起。在问原创 2020-06-15 08:07:55 · 383 阅读 · 1 评论 -
笨办法学python3 学习笔记 习题42 对象、类及从属关系
类中的self在类中定义函数时,第一参数永远是类的本身实例变量self,并且调用时,不用传递该参数。原创 2020-06-15 08:07:03 · 247 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题41 学习面向对象术语
习题41 学习面向对象术语专有词汇练习类(class):告诉 Python创建新类型的东西。对象( object):两个意思,即最基本的东西,或者某样东西的实例。实例( Instance):这是让 Python创建一个类时得到的东西。def:这是在类里边定义函数的方法。self:在类的函数中,self指代被访问的对象或者实例的一个变量。继承( inheritance):指一个类可以继承另ー个类的特性,和父子关系类似。组合( composition):指一个类可以将别的类作为它的部件构建起来原创 2020-06-14 16:20:54 · 450 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题40 模块、类和对象
class Song(object): def __init__(self,lyrics): self.lyrics = lyrics def sing_me_a_song(self): for line in self.lyrics: print(line)# 对象1,列表作为lyrics需要的参数传入happy_bday = Song(["Happy birthday to you",原创 2020-06-14 16:20:13 · 244 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题39 字典,可爱的字典
习题39 字典,可爱的字典# create a mapping of state to abbreviationstates = { 'Oregon':'OR', 'Florida':'FL', 'California':'CA', 'New York':'NY', 'Michigan':'MI',}# create a basic set of states and some cities in themcities = { 'CA':'San原创 2020-06-14 16:19:39 · 269 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题38 列表的操作
习题38 列表的操作ten_things = "Apples Oranges Crows Telephone Light Sugar"print("Wait there are not 10 things in that list. Let's fix that.")# 将ten_things中的内容按空格分隔开stuff = ten_things.split(' ')# 定义变量more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn",原创 2020-06-14 16:17:59 · 232 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题37 复习各种符号
习题37 复习各种符号关键字关键字描述示例and逻辑与True and False == Falseaswith-as语句的一部分with X as Y: passassert断言(确保)某东西为真assert False, “Error!”break立即停止循环while True: breakclass定义类class Person(object)continue停止当前循环的后续步骤,再做一次循环while True: c原创 2020-06-14 16:17:19 · 159 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题36 设计和调试
if语句的规则每一条if语句必须包含一个else如果这个else永远都不应该被执行到,因为它本身没有任何意义,那你必须在else语句后面使用一个叫die的函数,让它打印出出错消息并且“死”给你看,这和上一个习题类似,这样你可以找到很多的错误if语句的嵌套不要超过两层,最好尽量保持只有一层将if语句当作段落来对待,其中的每一个if、elif和else组合就跟一个段落的句子组合一样。在这种组合的最前面和最后面留一个空行以作区分你的布尔测试应该很简单,如果它们很复杂,你需要在函数里将它们的运算事先放到原创 2020-06-14 16:16:39 · 145 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题35 分支和函数
# 从sys库中导入exit函数from sys import exit# 定义gold_room()函数def gold_room(): print("This room is full of gold. How much do you take?") # 定义局部变量choice choice = input(">") # 创建if块,若choice中含有0 / 1,运行下一行代码 if "0" in choice or "1" in choic原创 2020-06-14 16:15:25 · 304 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题34 访问列表的元素
序数等于有序,从1开始基数等于随机选取,从0开始。animals = ['bear', 'python3.6', 'peacock', 'kangaroo', 'whale', 'platypus']print("位置1的动物:第二个动物", animals[1])print("第三只动物:位置2的动物", animals[2])print("第一只动物:位置0的动物", animals[0])print("位置3的动物:第四个动物", animals[1])print("第五只动物:位.原创 2020-05-18 22:16:22 · 211 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题33 while循环
遵循规则以使用while循环尽量少用 while循环,大部分时候for循环是更好的选择。重复检查你的 while语句,确定你测试的布尔表达式最终会变成 False。如果不确定,就在 while循环的开始和结尾打印岀你要测试的值,看看它的变化。i = 0numbers = []# 开始一个while循环while i < 6: print(f"At the top i is {i}") # 将i的值追加写在列表的最后 numbers.append(i)原创 2020-05-18 22:13:26 · 348 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题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_count: print(f"This is count {number}")# same as a原创 2020-05-18 22:15:55 · 445 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题31 做出决定
print("""You enter a dark room with two doors.Do you go through door #1 or door #2?""")door = input(">")if door == "1": print("There's a giant bear here eating a cheese cake.") print("What do you do?") print("1. Take the cake.") pri原创 2020-05-18 22:12:19 · 191 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题30 else和if
people = 40cars =30trucks = 15if cars > people: print("We should take the cars.") print(">>>>>>>>>>after cars > people")elif cars < people: print("We should not take the cars.") print(">>&g原创 2020-05-18 22:11:50 · 129 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题29 if语句
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 world is drooled on!")if people > dogs:原创 2020-05-18 22:11:13 · 157 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题28 布尔表达式练习
>>> True and TrueTrue>>> False and TrueFalse>>> 1 == 1 and 2 == 1False>>> "test" == "test"True>>> 1 == 1 or 2 != 1True>>> True and 1 == 1True>>> False and 0 != 0False>>>原创 2020-05-18 22:10:27 · 138 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题26
print("How old are you?", end=' ')age = input()print("How tall are you?", end=' ')height = input()print("How much do you weigh?", end=' ')weight = input()print(f"So, you're {age} old, {height} tall and {weight} heavy.")from sys import argvscript,原创 2020-05-18 22:09:44 · 215 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题25
习题25 更多更多的练习# 定义break_words函数def break_words(stuff): # 加注释 """This function will break up words for us.""" # 将sutff按空格分隔——words words = stuff.split(' ') return words# 定义sort_wo...原创 2020-05-03 15:29:54 · 448 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题24
习题24 更多的练习# 打印字符串print("Let's practice everything.")# 打印字符串,包括转义序列print("You\'d need to know\'bout escapes with \\ that do:")# 打印字符串,包括换行符和制表符print('\n newlines and \t tabs.')# 为poem赋值,使用三引号显示...原创 2020-05-03 15:28:47 · 393 阅读 · 1 评论 -
笨办法学python3 学习笔记 习题1-3
习题1 第一个程序print("Hello World!")print("Hello Again")print("I like typing this.")print("This is fun.")print('Yay!Priting.')print("I'd much rather you 'not'.")print('I "said" do not touch this.')...原创 2020-01-17 21:16:05 · 282 阅读 · 0 评论 -
*笨办法学python3 学习笔记 习题4-6
习题4 变量和命名# 设定cars数值为100cars = 100# 设定 space_in_a_car数值为4.0space_in_a_car = 4.0# 设定drivers数值为30drivers = 30# 设定passengers数值为90passengers = 90# 设定 cars_not_driven = cars - driverscars_not_dr...原创 2020-01-22 16:44:00 · 342 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题7-9
习题7 更多打印print("Mary had a little lamb.")print("Its fleece was white as {}.".format('snow'))print("And everywhere that Mary went.")print("." * 10) # what'd that do?end1 = "C"end2 = "h"end...原创 2020-01-22 22:07:02 · 251 阅读 · 0 评论 -
*笨办法学python3 学习笔记 习题10-13
习题10 那是什么tabby_cat = "\tI'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\t* Fishies\t* Catnip\n\t* Grass"""...原创 2020-02-07 17:03:08 · 496 阅读 · 0 评论 -
*笨办法学python3 学习笔记 习题14-16
习题14from sys import argvscript, user_name, age = argvprompt = "Your answer: "print(f"Hi {user_name}, I'm the {script} script.")print("I'd like to ask you a few questions.")print(f"Do you like...原创 2020-03-01 17:07:25 · 732 阅读 · 1 评论 -
*笨办法学python3 学习笔记 习题17-19
习题17 更多文件操作在运行程序时报错,称编码失败。原代码:from sys import argvfrom os.path import existsscript, from_file, to_file = argvprint(f"Copying from {from_file} to {to_file}")# We could do these two on one l...原创 2020-04-02 13:15:35 · 1498 阅读 · 0 评论 -
笨办法学python3 配套视频学习1-19
逗号print()函数中的逗号相当于给它一个列表,告诉print让它逐一打印python遇到逗号以后,会分别打印出来意义分隔一个清单原创 2020-04-10 14:35:14 · 462 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题20-21
习题二十 会话# 将sys解包from sys import argv# 给argv赋值script, input_file = argv# 定义print_all()函数,该函数接收一个参数def print_all(f): # 读取给定文件的内容,并将其打印出来 print(f.read())# 定义rewind()函数,该函数接收一个参数def rewi...转载 2020-04-26 09:16:22 · 472 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题22
词作用print()将内容打印到屏幕上format格式化字符串。取欲格式化的字符串,调用format函数(执行一个format命令行命令),将n个参数传递给format函数,这些参数与欲格式化字符串中的{}相匹配。在字符串上调用format函数的结果是一个新字符串,其中的{}被n个参数替换掉了input()接收一个标准输入数据,返回为字符串类型open(...原创 2020-04-26 19:32:19 · 225 阅读 · 0 评论 -
笨办法学python3 学习笔记 习题23
习题23 字符串,字节串和字符编码# 将argv解包,与之前的形式不太一样,但内容相同,import sysscript, encoding, error = sys.argv # 定义main函数,含三个变量def main(language_file, encoding, errors): # 每次读取文件的一行,赋值给line变量 ...转载 2020-04-27 18:02:21 · 968 阅读 · 3 评论