Python笔记---------------[]

列表解析

>>> [(x,y) for x in range(3) for y in range(5)]

[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]

生成器表达式(每次只加载一个元素到内存里)

返回可迭代对象

多重继承

(新式类采用广度优先,经典类采用深度优先)

s = (1,2,[2,3])

i = s#前拷贝id(i) == id(s) s的改变会影响i

i = copy.deepcopy(s)  深拷贝

>>> d = [(x,y,z,a,v,c) for x in range(9) for y in range(9) for z in range(9) for a in range(9) for v in range(9) for c in range(9)] 暴力破解

Isinstance(s, str)判断s这个对象是不是’str’object

For i in range(10:

If i == 2:

Continue

Print i

Class Girl(human):

Def __init__(self, name, old):

Print ‘We are People’, name

Switch = {

‘-’:a-b,

‘+’:a+b

}

Print Switch.get(c,’Please input +-*/’)  #用字典的get这样不报错

>>> hello[:3:-1]

'dlrow o'

>>> hello[3:-1]

'lo worl'

>>> hello[4:]

'o world'

>>>

S = 'abcdefghijk'

for i in range(0,len(S),2):

print S[i]   #小技巧

--------------------------------------------

L1 = [1,2,3,3]

L2 = [1, 2, 3, 4]

返回L1中的数值这个数值必须是L2中没有的。

Set(L1).difference(L2)     --------------------------------------------------------

enumerate()

利用enumerate()函数,可以在每次循环中同时得到下标和元素:

S = 'abcdefghijk'

for (index,char) in enumerate(S):    print index    print char

实际上,enumerate()在每次循环中,返回的是一个包含两个元素的定值表(tuple),两个元素分别赋予index和char。

with open(file) as myfile:

Print myfile.read() #不用再关闭myfile

------------------------------------------------

class num(object):

def __init__(self, value):

self.value = value

def getNeg(self):

return -self.value

def setNeg(self, value):

self.value = -value

def delNeg(self):

print("value also deleted")

del self.value

neg = property(getNeg, setNeg, delNeg, "I'm negative")

x = num(1.1)

print(x.neg) x.neg = -22

print(x.value)

print(num.neg.__doc__)

del x.neg

装饰器可以对一个函数、方法或者类进行加工。

def decorator(F):

def new_F(a, b):

print("input", a, b)

return F(a, b)

return new_F

# get square sum

@decorator

def square_sum(a, b):

return a**2 + b**2  #square_sum = decorator(square_sum)

Def func(formal_args, i = 0, *args, **kwargs):

print formal_args

print i

print args

print kwargs

以上是不带参数的装饰器

def Func1(F):

def new_fun():

print('func1')

F()

return new_fun

def Func21(F):

def new_fun2():

print('func2')

F()

return new_fun2

@Func1

@Func21

def Func4():

pass

print(0b1110) # 二进制,以0b开头

print(0o10) # 八进制,以0o开头

print(0x2A) # 十六进制,以0x开头

环境配置$export PYTHONPATH=$PYTHONPATH:/home/vamei/mylib

安装非标准包

Python的标准库随着Python一起安装。当我们需要非标准包时,就要先安装。

(1)、使用Linux repository (Linux环境)

这是安装Python附加包的一个好的起点。你可以在Linux repository中查找可能存在的Python包 (比如在Ubuntu Software Center中搜索matplot)。

(2)、使用pip。pip是Python自带的包管理程序,它连接Python repository,并查找其中可能存在的包。

比如使用如下方法来安装、卸载或者升级web.py:

$pip install -i http://mirrors.aliyuncs.com/pypi/simple web.py$pip uninstall web.py$pip install -i http://mirrors.aliyuncs.com/pypi/simple --upgrade web.py

如果你的Python安装在一个非标准的路径(使用$which python来确认python可执行文件的路径)中,比如/home/vamei/util/python/bin中,你可以使用下面方法设置pip的安装包的路径:

$pip install -i http://mirrors.aliyuncs.com/pypi/simple --install-option="--prefix=/home/vamei/util/" web.py

str1 = ['21+23=', '32/8=', '32*12=']

e=[]

for s in str1:

s2 = s + str(eval(s[:-1]))

e.append(s2)

print(e)

随学随用的python-note的更多相关文章

  1. 小姐姐带你一起学:如何用Python实现7种机器学习算法(附代码)

    小姐姐带你一起学:如何用Python实现7种机器学习算法(附代码) Python 被称为是最接近 AI 的语言.最近一位名叫Anna-Lena Popkes的小姐姐在GitHub上分享了自己如何使用P ...

  2. 2015/8/9 到家了,学完了CodeCademy的Python

    昨天坐了20多个小时的硬座回家.发现在网络信号差的火车上也是学习的好地方.如果你的手机电量不足的话,带上两本书简直是绝配.我在火车上阅读了两百多页的内容,并没有多大的疲累,那样无聊的环境里面能看书学习 ...

  3. python note

    =和C一样,为赋值.==为判断,等于.但是,在python中是不支持行内赋值的,所以,这样避免了在判断的时候少写一个出错. dictionary 的key唯一,值可以为很多类型. list的exten ...

  4. selenuim,qtp,loadrunner,jmeter有何区别,想学个脚本语言python和测试工具应该从哪里入门呢。

    selenium和qtp是 功能的自动化测试,loadrunner和jmeter是性能的自动化测试 selenium要求代码能力比较高,适合用python,QTP一般用VBS,loadrunner一般 ...

  5. 吾八哥学Selenium(一):Python下的selenium安装

    selenium简介 Selenium也是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Mo ...

  6. 零基础学Python--------第11章 使用Python操作数据库

    第11章 使用Python操作数据库 11.1 数据库编程接口 在项目开发中,数据库应用必不可少.虽然数据库的种类有很多,如SQLite.MySQL.Oracle等,但是它们的功能基本都是一样的,为了 ...

  7. python note 4

    1.使用命令行打开文件 t=open('D:\py\123.txt','r') t.read() 在python和很多程序语言中""转义符号,要想输出\要么多加一个\写成\ 要么在 ...

  8. python note 17 random、time、sys、os模块

    1.random模块(取随机数模块) # 取随机小数 : 数学计算 import random print(random.random())# 取0-1之间的小数 print(random.unifo ...

  9. python note 16 re模块的使用

    1.re模块(#regex) # 查找 # findall : 匹配所有 每一项都是列表中的一个元素 import re ret = re.findall('\d+','dawdawd154wadwa ...

  10. python note 15 正则表达式

    # 正则表达式 只和字符串打交道 # 正则表达式的规则# 规则 字符串 从字符串中找到符合规则的内容 # 字符组 : [] 写在中括号中的内容,都出现在下面的某一个字符的位置上都是符合规则的 # [0 ...

随机推荐

  1. python 基础篇(一)--linux命令篇

    期末下一门考试还有些时间,那就来看看python的视频吧,基于python2.7.6,用的是xubuntu(vm搭建虚拟机). 先花了2,3个小时安装了xubuntu,配置了搜狗输入法,gedit也配 ...

  2. Spring的IOC注解学习

    先引入jar包,common-annotations.jar 接着上代码: 1.dao接口 package com.dao; public interface OkpDao { public void ...

  3. bzoj 1912 : [Apio2010]patrol 巡逻 树的直径

    题目链接 如果k==1, 显然就是直径. k==2的时候, 把直径的边权变为-1, 然后在求一次直径. 变为-1是因为如果在走一次这条边, 答案会增加1. 学到了新的求直径的方法... #includ ...

  4. 一个简化的printf函数

    <C和指针>第7章第5道编程题: 实现一个简化的printf函数,它能够处理%d.%f.%s 和 %c 格式码,根据ANSI标准的原则,其他格式码的行为是未定义的.你可以假定已经存在函数 ...

  5. HTTP based RESTful APIs - asp.net web api

    1.HTTP http://www.w3.org/Protocols/rfc2616/rfc2616.html 2.REST是什么http://www.ics.uci.edu/~fielding/pu ...

  6. 如何将Oracle安装为Linux服务

    方法一:使用oracle自带的启动和关闭脚本 1. oracle用户修改/etc/oratab 文件: $ vi /etc/oratab orcl:/oracle/app/product/10.2.0 ...

  7. Eclipse 安装mybatis的编辑插件

    1.MyEditor安装的方式 Eclipse 安装mybatis的编辑插件有以下4种方式,您可以使用下列方法之一来安装MyBatis的编辑器: Eclipse 3.7的(市场客户机安装):此图像拖放 ...

  8. poj1458 dp入门

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37551   Accepted: 15 ...

  9. cocos2d-x 学习笔记一(概述)

    1.游戏-可控制的动画-连续的静态图像 2.关键帧驱动游戏 3.事件驱动型游戏 4.cocos2d-x 1.0 opengl-es 1.0;cocos2d-x 2.x opengl-es 2.0;co ...

  10. 启动监听报错:TNS-12537: TNS:connection closed TNS-12560: TNS:protocol adapter error TNS-00507: Connection closed Linux Error: 29: Illegal seek

    启动监听程序报错: 说明:在rhel5.8上安装完成oracle11g数据库后,使用netca创建完监听,启动监听时报错.还未使用dbca创建实例. [oracle@rusky-oracle11g ~ ...