Python笔记:基础

python的基础用法和需要注意的地方

Python 赋值

python中的赋值机制与其他语言有所区别,使用时需要注意。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
>>>x = [1,2,3]
>>>y = x
>>>x[1] = 100
>>>y
[1,100,3]

>>>x=500
>>>y=x
>>>y
500
>>>y='foo'
>>>x
500
>>>y
foo

>>>x = [500, 501, 502]
>>>y = x
>>>print('x = ',x,' y= ',y)
x = [500, 501, 502] y= [500, 501, 502]
>>>y[1] = 700
>>>print('x = ',x,' y= ',y)
x = [500, 700, 502] y= [500, 700, 502]
>>>y = [100,200]
>>>print('x = ',x,' y= ',y)
x = [500, 700, 502] y= [100, 200]

条件控制

在C++等语言中用{ }定义,Python中用:以及缩进控制代码块。

1
2
3
4
5
6
if condtion:
statement
elif condition2:
statement2
else:
statement3

实用功能

列表推导式

1
2
3
4
5
#列表推导式,也适用于集合、字典
values = [10,21,3,5,12]
squares = [x**2 for x in values if x<=10]
square_set = {x**2 for x in values if x<=10}
square_dict = {x: x**2 for x in values if x<=10}

函数

用def开头,不需要指定返回值,return语句返回值,否则默认空值None。
变量也不需要指定类型

1
2
def func_name(pram1, param2 = default_value, ....):
return x

文档注释

用三重引号表示多行字符串。使用是可以通过$times.doc$来获取。

1
2
3
4
5
6
7
8
9
10
11
def times(a,b):
'''a multiplies b.

key arguments:
a -- number
b -- number

returns: number
'''
a*b

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#接受不定参数
def add(x, *args):
total = x
for arg in args:
total += arg
return total

>>> add(1,2,3,5,)
11

#使用包含关键词的不定参数
def add(x, **args):
total = x
for arg, value in args.items():
print('adding ', arg)
total += value
return total

>>>add(1,x=1,y=3,z=100)
105

将函数fun应用到seq序列上的每个元素
map(fun, seq)

模块和包

可以通过import module,或者通过from module import part调用全部或部分模块

__name__属性

一个.py文件,可能是模块,也可以当作脚本使用(测试用)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#文件名称 mymodule.py
PI = 3.1416

def sum(lst):
""" Sum the values in a list
"""
tot = 0
for value in lst:
tot = tot + value
return tot

def add(x, y):
" Add two values."
a = x + y
return a

def test():
w = [0,1,2,3]
assert(sum(w) == 6)
print 'test passed.'

if __name__ == '__main__':
test()

这样的文件既可以当模块调用,也可以当脚本运行。

1
2
3
4
5
>>>run mymodule.py
test passed
>>>import mymodule as my
>>>my.PI
3.1416

包(package)

假设我们有这样的一个文件夹:

foo/

  • __init__.py
  • bar.py (defines func)
  • baz.py (defines zap)

这意味着 foo 是一个package,可以这样导入其中的内容:

1
2
from foo.bar import func
from foo.baz import zap

barbaz 都是 foo 文件夹下的 .py 文件。

导入包要求:

  • 文件夹 fooPython的搜索路径中
  • __init__.py 表示 foo 是一个包,它可以是个空文件。

异常exception

通过try….except来避免系统报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import math

while True:
try:
text = raw_input('> ')
if text[0] == 'q':
break
x = float(text)
y = 1 / math.log10(x)
print "1 / log10({0}) = {1}".format(x, y)
except ValueError as exc:
if exc.message == "math domain error":
print "the value must be greater than 0"
else:
print "could not convert '%s' to float" % text
except ZeroDivisionError:
print "the value must not be 1"
except Exception as exc:
print "unexpected error:", exc.message