learn python the hard way (personal) -- Ex38-

learn python the hard way -personal -- Ex38-

Ex 38

append

首先回忆一下之前学过的append,在Ex32中,我们使用过append,但还不清楚它的原理,接下来将详细说明。
mystuff.append(‘hello’)为例
1.首先python会回溯你创建的mystuff是什么(function argument or global variable)
2.找到mystuff之后,开始读取.,并考虑mystuff中的变量。(这里mystuff是list,有很多功能)。
3.之后来到append,将这个名和mystuff可使用的功能的名作比较。如果其中有append,就将它拿来使用。
4.之后来到(,意识到这里有一个函数,调用这个函数,或者用额外的参数(extra argument)调用这个函数。
5.这个额外的参数就是mystuff,着听起来很奇怪,但是python就是这样工作的。这个函数的调用其实接近:append(mystuff,‘hello’) 这个形式。

了解这个原理可以帮助大家理解python的一些报错,如下:

$ python3.6
>>> class Thing(object):
...     def test(message):
...             print(message)
...
>>> a = Thing()
>>> a.test("hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: test() takes exactly 1 argument (2 given)
>>>

提示我们test() takes exactly 1 argument (2 given),用test(a,“hello”)来理解,说明在某处有混淆并且没有给a加上参数。

list

接下来会有关于list练习来帮助我们理解。

ten_things = "Apples Oranges Crows Telephone Light Sugar"

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

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(f"There are {len(stuff)} items now.")

print("There we go: ", stuff)

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

print(stuff[1])
print(stuff[-1]) # whoa! fancy
print(stuff.pop())
print(' '.join(stuff)) # what? cool!
print('#'.join(stuff[3:5])) # super stellar!

数据结构(data structure)
可以简单的理解为是一种组织数据的形式方法。
list是最为常用的一种数据结构,是一种有序的列,你希望通过索引(index)来随机或线形地存储或访问数据。

Study Drills

1.Take each function that is called, and go through the steps for function calls to translate them to what Python does. For example, more_stuff.pop() is pop(more_stuff).
2.Translate these two ways to view the function calls in English. For example, more_stuff.pop() reads as, “Call pop on more_stuff.” Meanwhile, pop(more_stuff) means, “Call pop with argument more_stuff.” Understand how they are really the same thing.
3.Go read about “object-oriented programming” online. Confused? I was too. Do not worry. You will learn enough to be dangerous, and you can slowly learn more later.
4.Read up on what a “class” is in Python. Do not read about how other languages use the word “class.” That will only mess you up.
5.Do not worry If you do not have any idea what I’m talking about. Programmers like to feel smart, so they invented object-oriented programming, named it OOP, and then used it way too much. If you think that’s hard, you should try to use “functional programming.”
6.Find 10 examples of things in the real world that would fit in a list. Try writing some scripts to work with them.

Ex39

Dictionaries

类似于list但可以自己设置索引。

>>> stuff = {'name': 'Zed', 'age': 39, 'height': 6 * 12 + 2}
>>> print(stuff['name'])
Zed
>>> print(stuff['age'])
39
>>> print(stuff['height'])
74
>>> stuff['city'] = "SF"
>>> print(stuff['city'])
SF
>>> stuff[1] = 'Wow'
>>> stuff
{'name': 'Zed', 'age': 39, 'height': 74, 'city': 'SF', 1: 'Wow'}
>>> stuff.pop('city')
'SF'
>>> stuff.pop(1)
'Wow'
>>> stuff.pop(2)
'Neato'
>>> stuff
{'name': 'Zed', 'age': 39, 'height': 74}

dict一般是没有顺序的但是可以了解collections.OrderedDict是有顺序的。

import collections
d1 = collections.OrderedDict()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值