# python学习笔记

### 设置浮点除法

from future import division

### sort方法

>>> x = [3,34,4,53,8,2,543,12,234,9]
>>> y = x.sort()
>>> print y
None

>>> x = [3,34,4,53,8,2,543,12,234,9]
>>> y = x[:]
>>> y.sort()
>>> x
[3,34,4,53,8,2,543,12,234,9]
>>> y
[2, 3, 4, 8, 9, 12, 34, 53, 234, 543]
#如果只是简单的把x赋值给y是没有用的，因为这样做就让x和y都指向同一个列表了
>>> y = x
>>> y.sort()
>>> x
[2, 3, 4, 8, 9, 12, 34, 53, 234, 543]
>>> y
[2, 3, 4, 8, 9, 12, 34, 53, 234, 543]

#另一种获取已排序的列表副本的方法，sorted
>>> x = [3,34,4,53,8,2,543,12,234,9]
>>> y = x.sorted()
>>> x
[3,34,4,53,8,2,543,12,234,9]
>>> y
[2, 3, 4, 8, 9, 12, 34, 53, 234, 543]

### 字典

clean（）

>>> x = {}
>>> y = x
>>> x['key'] = 'value'
>>> y
{'key':'value'}
>>> x={}
>>> y
{'key':'value'}

#使用clean清空原始字典的所有元素
>>> x = {}
>>> y = x
>>> x['key'] = 'value'
>>> y
{'key':'value'}
>>> x.clean()
>>> y
{}

setdefault（）

>>> d = {}
>>> d.setdefault('name','N/A')
'N/A'
>>> d
{'name':'N/A'}
>>> d['name']='Gumby'
>>> d.setdefault('name','N/A')
'Gumby'
>>>d
{'name':'Gumby'}

### is同一性运算符

>>> x = y =[1,2,3]
>>> z = [1,2,3]
>>> x == y
True
>>> x == z
True
>>>x is y
True
>>>x is z
False

### del

>>> x = y =[1,2,3]
>>> del y
>>> x
[1,2,3]
>>> y
NameError                                 Traceback (most recent call last)
<ipython-input-24-009520053b00> in <module>()
----> 1 y

NameError: name 'y' is not defined

### 函数参数

def story(**kwds):
return 'Once upon a time. there was a '\
'%(job)s called %(name)s.' %kwds

>>> print story(job='King',name='Gumby')
Once upon a time. there was a King called Gumby.

>>> print story(name='Sir Robin',job='brave knight')
Once upon a time. there was a brave knight called Sir Robin.

>>> params={'job':'language','name':'Python'}

>>> print story(**params)
Once upon a time. there was a language called Python.

def power(x,y,*others):
if others:
return pow(x,y)

>>> power(2,3)
8

>>> power(y=3,x=2)
8

def interval(start,stop=None,step=1):
'Imitates range() for step>0'
if stop is None:
start,stop=0,start
result=[]
i=start
while  i<stop:
result.append(i)
i+=step
return result

>>> interval(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> interval(1,5)
[1, 2, 3, 4]

>>> interval(1,23,3)
[1, 4, 7, 10, 13, 16, 19, 22]

### class类

_metaclass_=type

class Person():
def setName(self,name):
self.name=name
def getName(self):
return self.name
def greet(self):
print "Hello.word! I'm %s."%self.name


### try异常

while True:
try:
x = input('Enter the first number: ')
y = input('Enter the second number: ')
value = x/y
print 'x/y is', value
except:
print 'Invalid input. Please try again.'
else:
break


### 模块

import os
#获取当前工作目录
>>>os.getcwd()
#更改当前工作目录
>>>os.chdir('d:\')
>>>os.getcwd()

### error

Cannot remove entries from nonexistent file /opt/anaconda2/lib/python2.7/site-packages/easy-install.

curl https://bootstrap.pypa.io/ez_setup.py -o - | /opt/anaconda2/bin/python #python路径

#### Python学习笔记

2010年02月23日 596KB 下载

#### Python 学习笔记

2016年05月22日 1.49MB 下载

#### python 学习笔记资料

2018年01月23日 1.67MB 下载

#### Python学习笔记-

2017年09月01日 1.56MB 下载

#### Python学习笔记整理（python 3）

2018-01-08 13:37:37

#### python 学习笔记

2008年06月30日 1.39MB 下载

#### Python 学习笔记.pdf

2007年05月18日 2.15MB 下载

#### Python练习题 10-1 Python 学习笔记

2017-06-03 23:36:44

#### Python 学习笔记之一——Python简介

2016-08-01 16:15:18