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:
        print 'Received redundant parameters: ',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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值