python
tingtang13
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
class2,迭代
super 内置方法:__len__(self), __getitem(self, key) , __setitem__(self, key,value), __delitem__(self, key) property: 4个参数分别是fget, fset, fdel doc class rect: def __init__(self): self.width =原创 2016-05-27 17:50:47 · 232 阅读 · 0 评论 -
python os
os.getcwd() os.getcwdb() >>> os.getcwd() 'D:\\Users\\dyan\\AppData\\Local\\Programs\\Python\\Python35' >>> os.getcwdb() b'D:\\Users\\dyan\\AppData\\Local\\Programs\\Python\\Python35'原创 2016-06-06 19:47:25 · 494 阅读 · 0 评论 -
python 网络编程
1 server: bind->listen client:connect server: import socket s = socket.socket() host = socket.gethostname() port = 1234 s.bind((host, port)) s.listen(5) while True: c, addr = s.accept()原创 2016-06-05 18:04:38 · 398 阅读 · 0 评论 -
python sqlite3
1.建立,连接: >>> import sqlite3 >>> conn = sqlite3.connect('sd.db') >>> curs = conn.cursor()2.提交: >>> conn.commit()每次修改都提交,而不是关闭时才提交。 3.关闭: >>> conn.close()原创 2016-06-05 13:46:41 · 391 阅读 · 0 评论 -
python 文件操作
1 open 第三个参数是 是否缓冲。False 无缓冲,直接操作硬盘。 True 操作内存,速度快。flash或close 才会更新硬盘数据 >>> f = open('somefile.txt', 'w') >>> f.write("hello. ") 7 >>> f.write("World!") 6 >>> f.close()hello. World! >>> f = open(原创 2016-06-05 11:14:07 · 315 阅读 · 0 评论 -
python: re
. 可以匹配任何字符(除了换行) [pj]ython 匹配python, jython。[a-zA-Z0-9] 只能匹配一个这样的字符。 [^abc]匹配除了a,b,c之外的字符。 python|perl == p(ython|erl) 子模式 子模式后面加 ?。就是非必须。 >>> r'(http://)?(www\.)?python\.org'(pattern)* 0次以上原创 2016-06-04 15:52:49 · 252 阅读 · 0 评论 -
python 常用模块
1.告诉解释器哪里找模块: >>> import sys >>> sys.path.append('c:/python')unix要绝度路径 只有第一次导入执行。 >>> __name__ '__main__' 2. 当做包,必须包含一个命名为__init__py的文件(模块) 3. dir看模块里有什么 下划线开始,不是给模块外部用的。过滤: >>> impor原创 2016-06-03 21:09:20 · 234 阅读 · 0 评论 -
最终解决:idle your python may not be configured for tk
1.安装完python3报告题目错误。 2.按照网上升级匹配的tk,还是错。 3再装一遍python3 就好了。原创 2016-06-02 14:39:34 · 1781 阅读 · 0 评论 -
python 变量赋值的理解
再看python 基础教程,里面讲 x=3 这种赋值。两种解释: 第1种说法是把3 赋值给x, 和c 一样。 第2中说法是把x 绑定到值(或者对象)3 上面。 这种区别像是谁先存在内存中。我觉得第二种说法更好,体现了pyhton 无处不对象的原则。另外getrefcount 也体现了这点。原创 2016-05-18 06:53:55 · 474 阅读 · 0 评论 -
生成器
任何包含yield 语句的函数称为生成器。 yield不像return 返回值,而是每次产生多个值。每次产生一个值时,函数就会被冻结。被激活后从停止点开始. def flatten(nested): for sublist in nested: for element in sublist: yield element >>> nested =原创 2016-05-27 18:02:25 · 193 阅读 · 0 评论 -
python 写文件
file_ex = open('data.txt', 'w') 如果文件已存在,完全清除现有内容。如果不存在,则创建。 追加到文件,使用 a。 读和写,不清除,用 w+原创 2016-05-23 07:05:34 · 293 阅读 · 0 评论 -
异常
>>> raise Exception Traceback (most recent call last): File "", line 1, in raise Exception Exception >>> raise Exception('overlaod') Traceback (most recent call last): File "", line 1, in原创 2016-05-27 10:51:18 · 263 阅读 · 0 评论 -
class
>>> from random import choice >>> x=choice(['hello world', [1,2,'e','e',4]]) >>> x.count('e') 1 >>> 让方法变成私有,在名字前面加双下划线: >>> class secret: def __inacc(self): print("cannot") def acc(self):原创 2016-05-27 10:05:57 · 341 阅读 · 0 评论 -
python 函数
callable判断 一个东西 是否可以调用。感觉没啥用!!! >>> x=1 >>> import math >>> y=math.sqrt >>> callable(x) False >>> callable(y) True __doc__ >>> def square(x): 'calculate the square of the number x' return x*原创 2016-05-26 20:50:19 · 257 阅读 · 0 评论 -
python语句
>>> values (1, 2, 3) >>> x,y,z=values >>> x 1 >>> y 2 >>> z 3 函数可以返回一个以上的值,打包成tuple。接受数量要一致。 is是同一性预算符。 >>> x=y=[1,2,3] >>> z=[1,2,3] >>> x is y True >>> x is z False >>> 因为x和y绑定到原创 2016-05-26 17:31:24 · 203 阅读 · 0 评论 -
字典
>>> items = [('name', 'Gumby'), ('age', 42)] >>> d =dict(items) >>> d {'age': 42, 'name': 'Gumby'} >>> d = dict(name="gumby", age=42) key 没有引号 >>> d {'age': 42, 'name': 'gumby'} >>> >>> len(d原创 2016-05-26 15:34:48 · 211 阅读 · 0 评论 -
字典
键可以是数字,字符串,甚至元组。原创 2016-05-25 09:38:13 · 149 阅读 · 0 评论 -
string
from string import Template find返回最左端索引,没有-1 >>> 'with moo-moo here'.find('moo') 5 >>> 'with moo-moo here'.find('mooo') -1 可选起始点和结束点,包含第一个index,不包括第二个index,和切片一样。 >>> sub = '$$$ Get rich原创 2016-05-25 09:36:14 · 272 阅读 · 0 评论 -
元祖
逗号很重要: >>> 42 42 >>> (42) 42 >>> (42,) (42,) >>> 3*(34+3) 111 >>> 3*(34+3,) (37, 37, 37) >>> >>> tuple('abc') ('a', 'b', 'c')原创 2016-05-25 08:25:00 · 247 阅读 · 0 评论
分享