笨方法学Python
文章平均质量分 76
3ndO13
伟大是熬出来的!
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
笨方法学Python 习题 34: 访问列表的元素
列表的用处很大,但只有你能访问里边的内容时它才能发挥出作用来。你已经学会了按顺序读出列表的内容,但如果你要得到第 5 个元素该怎么办呢?你需要知道如何访问列表中的元素。访问第一个元素的方法是这样的:animals = ['bear', 'tiger', 'penguin', 'zebra']bear = animals[0]你定义一个 animals 的列表,然后你用 0 来获取第一个转载 2017-08-07 15:04:03 · 835 阅读 · 0 评论 -
笨方法学Python 习题 30: Else 和 If
#!usr/bin/python# -*-coding:utf-8-*-people = 30cars = 40buses = 15if cars > people: print ("We should take the cars.")elif cars < people: print ("We should not take the cars.")else:原创 2017-08-04 16:16:18 · 549 阅读 · 0 评论 -
笨方法学Python 习题 29: 如果(if)
#!usr/bin/python# -*-coding:utf-8-*-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!")原创 2017-08-04 15:02:53 · 1379 阅读 · 0 评论 -
笨方法学Python 习题 14: 提示和传递
#!usr/bin/python# -*-coding:utf-8-*-from sys import argvscript , user_name = argv prompt = '> 'print ("Hi %s, I'm the %s script." % (user_name, script))print ("I'd like to ask you a few questio原创 2017-07-27 10:30:31 · 648 阅读 · 0 评论 -
笨方法学Python 习题 13: 参数、解包、变量
from sys import argvscript, first, second, third = argvprint ("The script is called:", script)print ("Your first variable is:", first)print ("Your second variable is:", second)print ("Your thir原创 2017-07-26 17:03:36 · 3160 阅读 · 0 评论 -
笨方法学Python 习题 12: 提示别人
age = input("How old are you? ")height = input("How tall are you? ")weight = input("How much do you weigh? ")print ("So, you're %r old, %r tall and %r heavy." % ( age, height, weight))运行原创 2017-07-26 16:51:45 · 1857 阅读 · 0 评论 -
笨方法学Python 习题 28: 布尔表达式练习
#!usr/bin/python# -*-coding:utf-8-*-True and Trueprint ("True")False and Trueprint ("False")1 == 1 and 2 == 1print ("False")"test" == "test"print ("True")1 == 1 or 2 != 1print ("True")Tru原创 2017-08-04 10:48:12 · 1173 阅读 · 2 评论 -
笨方法学Python 习题 44: 继承(Inheritance) VS 合成(Composition)
童话里经常会看到英雄打败恶人的故事,而且故事里总会有一个类似黑暗森林的场景——要么是一个山洞,要么是一篇森林,要么是另一个星球,反正是英雄不该去的某个地方。当然,一旦反面角色在剧情中出现,你就会发现英雄非得去那片破森林去杀掉坏人。当英雄的总是不得不冒着生命危险进到邪恶森林中去。你很少会碰到这样的童话故事,说是英雄机智地躲过这些危险处境。你从不会听英雄说:“等等,如果我把白富美留原创 2017-08-18 11:03:38 · 1393 阅读 · 0 评论 -
笨方法学Python 习题 43: 来自 Percal 25 号行星的哥顿人(Gothons)
#!usr/bin/python# -*-coding:utf-8-*-import randomfrom urllib import urlopenimport sysWORD_URL = "http://learncodethehardway.org/words.txt"WORDS = []PHRASES = { "class ###(###):": "原创 2017-08-18 09:29:35 · 2956 阅读 · 0 评论 -
笨方法学Python 习题 15: 读取文件
#!usr/bin/python# -*-coding:utf-8-*-from sys import argvscript, filename = argv#获取参数 给script赋值ex15.py filename赋值ex15_sample.txt txt = open(filename)#打开 ex15_sample.txt文本print ("Here's your原创 2017-07-27 14:07:37 · 1209 阅读 · 0 评论 -
笨方法学Python 习题 16: 读写文件
#!usr/bin/python# -*-coding:utf-8-*-from sys import argvscript , filename = argvprint ("We're going to arase %r." % filename) # 打印 We're going to arase test.txtprint ("If you don't want原创 2017-07-27 15:42:24 · 4735 阅读 · 3 评论 -
笨方法学Python 习题 17: 更多文件操作
#!usr/bin/python# -*-coding:utf-8-*-from sys import argvfrom os.path import existsscript , from_file , to_file = argv# 定义值 script=lx0017.py from_file=test.txt to_file=copied.txtprint ("Copt原创 2017-07-27 17:09:22 · 1034 阅读 · 0 评论 -
笨方法学Python 习题 33: While 循环
#!usr/bin/python# -*-coding:utf-8-*-i = 0numbers = []while i < 6: print ("At the top i is %d" %i) numbers.append(i) i = i + 1 print ("Numbers now: ", numbers) print ("At the bottom i is %d原创 2017-08-07 13:52:03 · 702 阅读 · 0 评论 -
笨方法学Python 习题 21: 函数可以返回东西
#!usr/bin/python# -*-coding:utf-8-*-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原创 2017-07-29 13:22:04 · 2031 阅读 · 0 评论 -
笨方法学Python 习题 20: 函数和文件
#!usr/bin/python# -*-coding:utf-8-*-from sys import argvscript , input_file = argvdef print_all(f): print (f.read())#新建print_all函数 定义print (f.read())为函数代码def rewind(f): f.seek(0)#新建rewind原创 2017-07-28 16:49:07 · 2463 阅读 · 0 评论 -
笨方法学Python 习题 46: 一个项目骨架
Python 软件包的安装你需要预先安装一些软件包,不过问题就来了。我的本意是让这本书越清晰越干净越好,不过安装软件的方法是在是太多了,如果我要一步一步写下来,那 10 页都写不完,而且告诉你吧,我本来就是个懒人。所以我不会提供详细的安装步骤了,我只会告诉你需要安装哪些东西,然后让你自己搞定。这对你也有好处,因为你将打开一个全新的世界,里边充满了其他人发布的 Python 软件。接下来原创 2017-08-20 20:05:16 · 2789 阅读 · 0 评论 -
笨方法学Python 习题 32: 循环和列表
#!usr/bin/python# -*-coding:utf-8-*-the_count = [1, 2, 3, 4, 5]fruits = ['apples', 'oranges', 'pears', 'apricots']change = [1, 'pennies', 2, 'dimes', 3, 'quarters']for number in the_count: pri原创 2017-08-05 14:36:11 · 1708 阅读 · 0 评论 -
笨方法学Python 习题 19: 函数和变量
#!usr/bin/python# -*-coding:utf-8-*-def cheese_and_crackers(cheese_count,boxes_of_crackers): print ("You have %d cheeses!" % cheese_count) print ("You have %d boxes_of_crackers!" % boxes_of_crack原创 2017-07-28 10:19:18 · 2186 阅读 · 3 评论 -
笨方法学Python 习题 18: 命名、变量、代码、函数
#!usr/bin/python# -*-coding:utf-8-*-def print_two(*args): arg1,arg2 = args print ("arg1: %r,arg2: %r" % (arg1, arg2))def print_two_again(arg1,arg2): print ("arg1: %r,arg2: %r" % (arg1,arg2))原创 2017-07-28 08:42:44 · 710 阅读 · 1 评论 -
笨方法学Python 习题 39: 字典, 可爱的字典
一、列表一组有序项目的集合。可变的数据类型【可进行增删改查】列表是以方括号“[]”包围的数据集合,不同成员以“,”分隔。列表中可以包含任何数据类型,也可包含另一个列表列表可通过序号访问其中成员二、元组不可变序列元组是以圆括号“()”包围的数据集合,不同成员以“,”分隔与列表不同:元组中数据一旦确立就不能改变三、字典键值对的集合(map)字典是以大括号“{原创 2017-08-12 13:44:00 · 1872 阅读 · 0 评论 -
笨方法学Python 习题 11: 提问
#!usr/bin/python# -*- coding:utf8 -*-print ("How old are you?"),age = input()print ("How tall are you?"),height = input("")print ("How much do you weight?"),weight = input()print ("So , you'r原创 2017-07-26 16:35:30 · 1349 阅读 · 0 评论 -
笨方法学Python 习题 10: 那是什么?
#!usr/bin/python# -*- coding:utf8 -*-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\原创 2017-07-26 16:01:20 · 1172 阅读 · 0 评论 -
笨方法学Python 习题 9: 打印,打印,打印
#!usr/bin/python# -*- coding:utf8 -*-# Here's somt new strange stuff, remember type it exactly.days = "Mon Tue Wed Thu Fri Sat Sun"months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"print ("Here原创 2017-07-26 15:55:59 · 464 阅读 · 0 评论 -
笨方法学Python 习题 41: 物以类聚
#!usr/bin/python# -*-coding:utf-8-*-class TheThing(object): def __init__(self): self.number = 0 def some_function(self): print("I got called.") def add_me_up(self, mo原创 2017-08-17 10:52:24 · 1488 阅读 · 0 评论 -
笨方法学Python 习题 37: 复习各种符号
Keywords(关键字)anddelfromnotwhileaselifglobalorwithassertelseifpassyieldbreakexceptimportprintclassexecinraisecontinuefinallyisreturndefforlam转载 2017-08-10 08:48:01 · 603 阅读 · 0 评论 -
笨方法学Python 习题 25: 更多更多的练习
#!usr/bin/python# -*-coding:utf-8-*-def break_words(stuff): """This function will break up words for us.""" """ split()函数 语法:str.split(str="",num=string.count(str))[n] 参数说明: str:表示为分隔符,默认为空格,原创 2017-08-02 17:03:22 · 3805 阅读 · 2 评论 -
笨方法学Python 习题 1: 第一个程序
print ("hello world")print ("hello Again")print ("I like typing this.")print ("This is fun.")print ('Yay! Printing')print ("I'd much rather you 'not'.")print ('I "said" do not touch this.)原创 2017-07-25 13:34:39 · 880 阅读 · 0 评论 -
笨方法学Python 习题 24: 更多练习
#!usr/bin/python# -*-coding:utf-8-*-print ("Let's practice everything.")print ("You\'d need to know \'bout escape with \\ that do \n newlines and \t tabs.")poem = """\tThe lovely worldwith log原创 2017-07-29 17:06:08 · 952 阅读 · 0 评论 -
笨方法学Python 习题 36: 设计和调试
If 语句的规则每一个“if 语句”必须包含一个 else.如果这个 else 永远都不应该被执行到,因为它本身没有任何意义,那你必须在 else 语句后面使用一个叫做 die 的函数,让它打印出错误信息并且死给你看,这和上一节的习题类似,这样你可以找到很多的错误。“if 语句”的嵌套不要超过 2 层,最好尽量保持只有 1 层。 这意味着如果你在 if 里边又有了一个 if,那你就需要原创 2017-08-08 16:10:11 · 555 阅读 · 1 评论 -
笨方法学Python 习题 35: 分支和函数
#!usr/bin/python# -*-coding:utf-8-*-from sys import exitdef gold_room(): print("This room is full of gold. How much do you take?") next = input("> ") if "0" in next or "1" in next: how_muc原创 2017-08-07 16:42:33 · 3298 阅读 · 1 评论 -
笨方法学Python 习题 23: 读代码
在网上阅读代码。这个任务初看会觉得很艰巨。我将直接把你丢到深水区呆几天,让你竭尽全力去读懂实实在在的项目里的代码。这节练习的目的不是让你读懂,而是让你学会下面的技能:找到你需要的 Python 代码。通读代码,找到文件。尝试理解你找到的代码。以你现在的水平,你还不具备完全理解你找到的代码的能力,不过通过接触这些代码,你可以熟悉真正的编程项目会是什么样子。当你做这节练习时,你可以转载 2017-07-29 16:32:25 · 895 阅读 · 0 评论 -
笨方法学Python 习题 42: 对象、类、以及从属关系
有一个重要的概念你需要弄明白,那就是“类(class)”和“对象(object)”的区别。问题在于,class 和 object 并没有真正的不同。它们其实是同样的东西,只是在不同的时间名字不同罢了。我用禅语来解释一下吧:鱼和泥鳅有什么区别?这个问题有没有让你有点晕呢?说真的,坐下来想一分钟。我的意思是说,鱼和泥鳅是不一样,不过它们其实也是一样的是不是?泥鳅是鱼的一种,所以说没什么不同,不转载 2017-08-17 11:01:34 · 1843 阅读 · 0 评论 -
笨方法学Python 习题 38: 列表的操作
#!usr/bin/python# -*-coding:utf-8-*-states = { "Oregon":"OR", "Floriad":"FL", "California":"CA", "New York":"NY", "Michigan":"MI"}cities = { "CA":"San Francisco", "MI":"Detroit", "FL":"J原创 2017-08-10 13:54:07 · 1402 阅读 · 1 评论 -
笨方法学Python 习题 26: 恭喜你,现在可以考试了!
你已经差不多完成这本书的前半部分了,不过后半部分才是更有趣的。你将学到逻辑,并通过条件判断实现有用的功能。在你继续学习之前,你有一道试题要做。这道试题很难,因为它需要你修正别人写的代码。当你成为程序员以后,你将需要经常面对别的程序员的代码,也许还有他们的傲慢态度,他们会经常说自己的代码是完美的。这样的程序员是自以为是不在乎别人的蠢货。优秀的科学家会对他们自己的工作持怀疑态度,同样,优秀的程原创 2017-08-03 14:01:47 · 676 阅读 · 0 评论 -
笨方法学Python 习题 8: 打印,打印
#!usr/bin/python# -*- coding:utf8 -*-formatter = "%r %r %r %r"print (formatter % (1 , 2 , 3 , 4))print (formatter % ("one" , "two" , "three" , "four"))print (formatter % (True , False , False ,原创 2017-07-26 15:47:20 · 1620 阅读 · 0 评论 -
笨方法学Python 习题 6: 字符串(string)和文本
#!usr/bin/python# -*- coding:utf8 -*-x = "There are %d types of people." %10binary = "binary"do_not = "don't"y = "Those who know %s and those who %s." % (binary , do_not)print (x)print (y)p原创 2017-07-26 14:15:49 · 1283 阅读 · 0 评论 -
笨方法学Python 习题 7: 更多打印
#!usr/bin/python# -*- coding:utf8 -*-print ("Mary had a little lamb.")print ("Its fleece was white as %s." % "snow")print ("And everywhere that Mary went.")print ("." * 10 ) #what'd that do?en原创 2017-07-26 14:42:50 · 832 阅读 · 0 评论 -
笨方法学Python 习题 5: 更多的变量和打印
#!usr/bin/python# -*- coding:utf8 -*- my_name = "Zed A. Shaw"my_age= 35 #not a liemy_height = 74 #inchesmy_weight = 180 #lbsmy_eyes = "Blue"my_teeth = "White"my_hair = "Brown"print ("Let's原创 2017-07-26 14:12:33 · 772 阅读 · 0 评论 -
笨方法学Python 习题 4: 变量(variable)和命名
#!usr/bin/python# -*- coding:utf8 -*- cars = 100space_in_a_car = 4.0drivers = 30passengers = 90cars_not_driven = cars - driverscars_driven = driverscarpool_capacity = cars_driven * space_in_a原创 2017-07-26 13:42:07 · 2510 阅读 · 0 评论 -
笨方法学Python 习题 3: 数字和数学计算
#!usr/bin/python# -*- coding:utf8 -*-# 打印功能 我将数我的小鸡们print ("I will now count my chickens")# 打印功能 母鸡20+30*6print ("Hens" , 25 + 30 * 6)# 打印功能 Roosters公鸡 100-25*3%4print ("Rppsters" , 100 - 25 *原创 2017-07-26 13:05:24 · 2792 阅读 · 0 评论
分享