无意之间看到这样一本好书,非常喜欢《learnPythonHardWay》,电子版免费。

http://learnpythonthehardway.org/book

Strings and Text

见到了一种非常奇怪但是也很简洁的方式将数据组合在一起

a=10

b=11

c="%s years and %s months ago"

print(c%(a,b))#注意中间是逗号隔开

Exercise 7: More Printing

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?

end1 = "C"

end2 = "h"

end3 = "e"

end4 = "e"

end5 = "s"

end6 = "e"

end7 = "B"

end8 = "u"

end9 = "r"

end10 = "g"

end11 = "e"

end12 = "r"

#print 中逗号往往意味着中间的空格

# watch that comma at the end. try removing it to see what happens

print end1 + end2 + end3 + end4 + end5 + end6,

print end7 + end8 + end9 + end10 + end11 + end12

Exercise 9: Printing, Printing, Printing

# Here's some 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 are the days: ", days

#可以顺利地打印出回车符

print "Here are the months: ", months

print """

There's something going on here.

With the three double-quotes.

We'll be able to type as much as we like.

Even 4 lines if we want, or 5, or 6.

"""

Exercise 11:Asking Questions

#逗号意味着可以不用换行,但是在python3中已经过时了

print "How old are you?",

age = raw_input()

print "How tall are you?",

height = raw_input()

print "How much do you weigh?",

weight = raw_input()

print "So, you're %r old, %r tall and %r heavy." % (

age, height, weight)

Exercise 13:Parameters, Unpacking, Variables

这一章的比较重要!

from sys import argv

script, first, second, third = argv

print "The script is called:", script

print "Your first variable is:", first

print "Your second variable is:", second

print "Your third variable is:", third

调用的参数

$python ex13.py first 2nd 3rd

Exercise 18: Names, Variables, Code, Functions

关于函数的参数调用的,也很重要

区分了位置参数,默认参数,可选参数,关键字参数。推荐看

这里。

原文讲得有些太经验了。

Exercise 24: More Practice

def secret_formula(started):

jelly_beans = started * 500

jars = jelly_beans / 1000

crates = jars / 100

return jelly_beans, jars, crates

#这里返回多个值,其实是把他们整合成了一个[]

start_point = 10000

beans, jars, crates = secret_formula(start_point)

Exercise 37: Symbol Review

这一章推荐看看,把常用的关键字都列举出来了,甚至包括了print的参数

这里

还谈到了yield关键字:pause here and return to caller。这真的是对协程最直白的表述啊!

接下来讲到的都是一些关键的比如list的用法

Exercise 38: Doing Things to Lists

用了mystuff.append('hello')这样一个简单的语句打头。介绍了python是如何识别你所想做的事情的。

When you write mystuff.append('hello') you are actually setting off a chain of events inside Python to cause something to happen to the mystuff list. Here's how it works:

#首先必须要找到mystuff,必须在之前要声明过

Python sees you mentioned mystuff and looks up that variable. It might have to look backward to see if you created with =, if it is a function argument, or if it's a global variable. Either way it has to find the mystuff first.

#然后根据类型进行函数匹配

Once it finds mystuff it reads the . (period) operator and starts to look at variables that are a part of mystuff. Since mystuff is a list, it knows that mystuff has a bunch of functions.

#调用函数

It then hits append and compares the name to all the names that mystuff says it owns. If append is in there (it is) then Python grabs that to use.

Next Python sees the ( (parenthesis) and realizes, "Oh hey, this should be a function." At this point it calls (runs, executes) the function just like normally, but instead it calls the function with an extra argument.

#注意不是mystaff.append()而应该是append(mystuff,'hello')

That extra argument is ... mystuff! I know, weird, right? But that's how Python works so it's best to just remember it and assume that's the result. What happens, at the end of all this, is a function call that looks like: append(mystuff, 'hello') instead of what you read which is mystuff.append('hello').

最后这一点很值得回味

ten_things = "Apples Oranges Crows Telephone Light Sugar"

print "Wait there are not 10 things in that list. Let's fix that."

#split()函数

stuff = ten_things.split(' ')

more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]

while len(stuff) != 10:

next_one = more_stuff.pop()

print "Adding: ", next_one

stuff.append(next_one)

print "There are %d items now." % len(stuff)

print "There we go: ", stuff

print "Let's do some things with stuff."

print stuff[1]

print stuff[-1] # whoa! fancy

print stuff.pop()

#这两个用法非常NB。。。

print ' '.join(stuff) # what? cool!

print '#'.join(stuff[3:5]) # super stellar!

join这个函数什么意思呢?使用>>>help('str.join')就可以看到文档啦!

join(…)

| S.join(iterable) -> string

|

| Return a string which is the concatenation of the strings in the

| iterable. The separator between elements is S.

Exercise 39: Dictionaries, Oh Lovely Dictionaries

主要讲了Dict的用法

比较重要的是

dict的添加方式是dict[key]=value

key的类型可以不同。(不同的类型也可以hash,很厉害啊)

# create a mapping of state to abbreviation

states = {

'Oregon': 'OR',

'Florida': 'FL',

'California': 'CA',

'New York': 'NY',

'Michigan': 'MI'

}

# create a basic set of states and some cities in them

cities = {

'CA': 'San Francisco',

'MI': 'Detroit',

'FL': 'Jacksonville'

}

# add some more cities

cities['NY'] = 'New York'

cities['OR'] = 'Portland'

# print out some cities

print '-' * 10

print "NY State has: ", cities['NY']

print "OR State has: ", cities['OR']

# print some states

print '-' * 10

print "Michigan's abbreviation is: ", states['Michigan']

print "Florida's abbreviation is: ", states['Florida']

# do it by using the state then cities dict

print '-' * 10

print "Michigan has: ", cities[states['Michigan']]

print "Florida has: ", cities[states['Florida']]

# print every state abbreviation

print '-' * 10

for state, abbrev in states.items():

print "%s is abbreviated %s" % (state, abbrev)

# print every city in state

print '-' * 10

for abbrev, city in cities.items():

print "%s has the city %s" % (abbrev, city)

# now do both at the same time

print '-' * 10

for state, abbrev in states.items():

print "%s state is abbreviated %s and has city %s" % (

state, abbrev, cities[abbrev])

print '-' * 10

# safely get a abbreviation by state that might not be there

state = states.get('Texas')

#如果是为None

if not state:

print "Sorry, no Texas."

# get a city with a default value

city = cities.get('TX', 'Does Not Exist')

print "The city for the state 'TX' is: %s" % city

Exercise 40: Modules, Classes, and Objects

这一章也讲得非常有趣,其实在python中,Modules和Classes的关系很接近,Modules可以看做是一个静态的Classes

思想还是很值得一看的:

http://learnpythonthehardway.org/book/ex40.html

# dict style

mystuff['apples']

# module style

mystuff.apples()

print mystuff.tangerine

# class style

thing = MyStuff()

thing.apples()

print thing.tangerine

这里不详细说了,看看继承的主要语法:

class Parent(object):

def override(self):

print "PARENT override()"

def implicit(self):

print "PARENT implicit()"

def altered(self):

print "PARENT altered()"

class Child(Parent):

def override(self):

print "CHILD override()"

def altered(self):

print "CHILD, BEFORE PARENT altered()"

super(Child, self).altered()

print "CHILD, AFTER PARENT altered()"

dad = Parent()

son = Child()

dad.implicit()

son.implicit()

dad.override()

son.override()

dad.altered()

son.altered()

Exercise 46: A Project Skeleton

不得不说都是干货!这一章主要教你怎么建立一个自己的Module

作者最后说的话也很棒。

You’ve finished this book and have decided to continue with programming. Maybe it will be a career for you, or maybe it will be a hobby. You’ll need some advice to make sure you continue on the right path and get the most enjoyment out of your newly chosen activity.

I’ve been programming for a very long time. So long that it’s incredibly boring to me. At the time that I wrote this book, I knew about 20 programming languages and could learn new ones in about a day to a week depending on how weird they were. Eventually though this just became boring and couldn’t hold my interest anymore. This doesn’t mean I think programming is boring, or that you will think it’s boring, only that I find it uninteresting at this point in my journey.

作者学习能力也是够强。

What I discovered after this journey of learning is that it’s not the languages that matter but what you do with them. Actually, I always knew that, but I’d get distracted by the languages and forget it periodically. Now I never forget it, and neither should you.

语言本身不重要,时间才比较重要

Which programming language you learn and use doesn’t matter. Do not get sucked into the religion surrounding programming languages as that will only blind you to their true purpose of being your tool for doing interesting things.

别陷入语言的坑里了

Programming as an intellectual activity is the only art form that allows you to create interactive art. You can create projects that other people can play with, and you can talk to them indirectly. No other art form is quite this interactive. Movies flow to the audience in one direction. Paintings do not move. Code goes both ways.

我觉得这里说得很棒,编程是门艺术,我们是手艺人。

Programming as a profession is only moderately interesting. It can be a good job, but you could make about the same money and be happier running a fast food joint. You’re much better off using code as your secret weapon in another profession.

People who can code in the world of technology companies are a dime a dozen and get no respect. People who can code in biology, medicine, government, sociology, physics, history, and mathematics are respected and can do amazing things to advance those disciplines.

。。。

Of course, all of this advice is pointless. If you liked learning to write software with this book, you should try to use it to improve your life any way you can. Go out and explore this weird, wonderful, new intellectual pursuit that barely anyone in the last 50 years has been able to explore. Might as well enjoy it while you can.

Finally, I’ll say that learning to create software changes you and makes you different. Not better or worse, just different. You may find that people treat you harshly because you can create software, maybe using words like “nerd.” Maybe you’ll find that because you can dissect their logic that they hate arguing with you. You may even find that simply knowing how a computer works makes you annoying and weird to them.

To this I have just one piece of advice: they can go to hell. The world needs more weird people who know how things work and who love to figure it all out. When they treat you like this, just remember that this is your journey, not theirs. Being different is not a crime, and people who tell you it is are just jealous that you’ve picked up a skill they never in their wildest dreams could acquire.

You can code. They cannot. That is pretty damn cool.

learnpythonthehardway.org_Python学习笔记LearnPythonHardWay相关推荐

  1. PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 call

    您的位置 首页 PyTorch 学习笔记系列 PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 发布: 2017年8月4日 7,195阅读 ...

  2. 容器云原生DevOps学习笔记——第三期:从零搭建CI/CD系统标准化交付流程

    暑期实习期间,所在的技术中台-效能研发团队规划设计并结合公司开源协同实现符合DevOps理念的研发工具平台,实现研发过程自动化.标准化: 实习期间对DevOps的理解一直懵懵懂懂,最近观看了阿里专家带 ...

  3. 容器云原生DevOps学习笔记——第二期:如何快速高质量的应用容器化迁移

    暑期实习期间,所在的技术中台-效能研发团队规划设计并结合公司开源协同实现符合DevOps理念的研发工具平台,实现研发过程自动化.标准化: 实习期间对DevOps的理解一直懵懵懂懂,最近观看了阿里专家带 ...

  4. 2020年Yann Lecun深度学习笔记(下)

    2020年Yann Lecun深度学习笔记(下)

  5. 2020年Yann Lecun深度学习笔记(上)

    2020年Yann Lecun深度学习笔记(上)

  6. 知识图谱学习笔记(1)

    知识图谱学习笔记第一部分,包含RDF介绍,以及Jena RDF API使用 知识图谱的基石:RDF RDF(Resource Description Framework),即资源描述框架,其本质是一个 ...

  7. 计算机基础知识第十讲,计算机文化基础(第十讲)学习笔记

    计算机文化基础(第十讲)学习笔记 采样和量化PictureElement Pixel(像素)(链接: 采样的实质就是要用多少点(这个点我们叫像素)来描述一张图像,比如,一幅420x570的图像,就表示 ...

  8. Go 学习推荐 —(Go by example 中文版、Go 构建 Web 应用、Go 学习笔记、Golang常见错误、Go 语言四十二章经、Go 语言高级编程)

    Go by example 中文版 Go 构建 Web 应用 Go 学习笔记:无痕 Go 标准库中文文档 Golang开发新手常犯的50个错误 50 Shades of Go: Traps, Gotc ...

  9. MongoDB学习笔记(入门)

    MongoDB学习笔记(入门) 一.文档的注意事项: 1.  键值对是有序的,如:{ "name" : "stephen", "genda" ...

最新文章

  1. [洛谷P1268]树的重量
  2. 曹大带我学 Go(10)—— 如何给 Go 提性能优化的 pr
  3. 前端学习(2673):vite
  4. 条件变量 ---C++17 多线程
  5. MaxCompute中如何使用OSS外部表读取JSON数据?
  6. JS window事件全集解析 (转载)
  7. python下载包没用_Python下载各种功能包出问题
  8. 声音模仿_澳洲这种鸟堪称“超级声音模仿秀”,比八哥还牛,却正遭山火毁灭...
  9. 永磁同步电机模型之坐标变换
  10. 从12306获取全国火车站的字典
  11. 100句激励自己的英文名言
  12. win10关闭自动更新
  13. 南京工资个税计算机,最新南京工资扣税标准
  14. WIN10如何进入BIOS界面
  15. 李彦宏清华经管学院演讲:11年创业心路历程与人生感悟
  16. VBS脚本实现宽带上网加网页认证上网双验证
  17. Diskgenius恢复硬盘误删文件及数据
  18. uni-app二维码生成,点击按钮弹框展示二维码
  19. 学习记录贴2:libpng16.so.16找不到,libc.so.6找不到
  20. 董导微博rust视频_如何评价综艺节目《歌手2019》第十一期?

热门文章

  1. 基于51单片机SJA1000 CAN通讯实现(代码+原理图)
  2. HTML+CSS画一朵向日葵
  3. python字符串常用操作方法(二)
  4. 文献综述怎么写?有哪些准备工作和内容要求
  5. 如何一台服务器虚拟多人用,使用服务器如何实现多台虚拟主机
  6. 自定义字体 Typeface ttf
  7. TFN F1 工程 高校 通信都在用的光时域反射仪
  8. wordpress更改主页
  9. java futuretask 单例_集群环境下java单例查询多了就异常
  10. 【使用 arm-poky-linux-gnueabi-gcc -v 指令可以查看 gcc 版本时报错】