python笔记[更新中]

0.帮助系统的使用

比如,我想知道下面的join的帮助文档

>>> def concat(*args, sep="/"):
...    return sep.join(args)

>>> help(str.join)
Help on method_descriptor:
join(...)
    S.join(iterable) -> str
    Return a string which is the concatenation of the strings in the
    iterable.  The separator between elements is S.

>>> str.join.__doc__
'S.join(iterable) -> str\n\nReturn a string which is the concatenation of the st
rings in the\niterable.  The separator between elements is S.'

>>> print(str.join.__doc__)
S.join(iterable) -> str
Return a string which is the concatenation of the strings in the
iterable.  The separator between elements is S.
>>> help("".join) //不知道类型名的情况
Help on built-in function join:
join(...)
    S.join(iterable) -> str
    Return a string which is the concatenation of the strings in the
    iterable.  The separator between elements is S.


1.字符串的片选操作word[0:2],2这个位置不算作结果,相当于end()。

 +---+---+---+---+---+
 | H | e | l | p | A |
 +---+---+---+---+---+
 0 1 2 3 4 5
-5 -4 -3 -2 -1

The slice from i to j consists of all characters between the edges labeled i and j, respectively。

2.multiple assignment

a,b=b,a+b

the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.

3.for循环修改列表例子中,a[:]表示一个拷贝。
>>> # Measure some strings:
... a = ['cat', 'window', 'defenestrate']
>>> for x in a[:]: # make a slice copy of the entire list
... if len(x) > 6: a.insert(0, x)
...
>>> a

['defenestrate', 'cat', 'window', 'defenestrate']

4.range(10)返回一个object,而不是一个list。

>>> print(range(10))
range(0, 10)

range() behaves as if it is a list, but in fact it isn’t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really make the list, thus saving space.

5.循环中的else语句

>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print(n, 'equals', x, '*', n//x)
...             break
...     else:
...         # loop fell through without finding a factor
...         print(n, 'is a prime number')

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.

即for语句迭代完,或while语句条件变为假,但由break跳出的除外。

6.docstring"""Print a Fibonacci series up to n."""

>>> def fib(n):    # write Fibonacci series up to n
...     """Print a Fibonacci series up to n."""
...     a, b = 0, 1
...     while a < n:
...         print(a, end=' ')
...         a, b = b, a+b
...     print()
...

7.函数语义

1)变量引用顺序

whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. 

2)默认参数以定义处5为准

i = 5
def f(arg=i):
    print(arg)
i = 6
f()

3)默认值为list,dictionary时的副作用

def f(a, L=[]):
    L.append(a)
    return L
print(f(1))
print(f(2))
print(f(3))

This will print
[1]
[1, 2]
[1, 2, 3]

为了避免副作用,默认参数尽量采用不可修改的类型。

4)keyword Arguments

指的是调用函数的时候的结合方式,通常调用函数是根据位置关系对应的,可以用形参名字指定对应的实参。

需要注意的是非keyword argument跟着keyword argument是非法的。如parrot(voltage=5.0, 'dead')  # non-keyword argument following keyword

5)*name,**name

**name接收所有除了对应其它形参的keyword arguments.

*name接收positional argument

8.作为脚本执行

作为脚本执行时,会将__name__改为__main__,从而可以在module中添加如下代码。

if __name__ == "__main__": 

import sys

fib(int(sys.argv[1]))

只有作为脚本执行时,才有效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值