# python学习笔记

116人阅读 评论(0)

### 设置浮点除法

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路径

0
0

个人资料
• 访问：535次
• 积分：44
• 等级：
• 排名：千里之外
• 原创：4篇
• 转载：1篇
• 译文：0篇
• 评论：2条
文章分类
文章存档
最新评论
• fastext

uchjum: @u011344625:https://github.com/salestock/fastText....

• fastext

u011344625: 你好，我最近也在学习fastText，用linux，然而我使用它运行text测试文本时却显示P@1:...