随笔分类 - [32] Python
摘要:《python基础教程(第二版)》学习笔记 类和对象(第7章)定义类class Person: def setName(self,name): self.name=name def getName(self): return self.namefoo=Person()foo.setName('AAA...
阅读全文
摘要:《python基础教程(第二版)》学习笔记 函数(第6章)创建函数:def function_name(params): block return values记录函数:def function_name(params): 'NOTE' #注释 block return valuesfunction...
阅读全文
摘要:《python基础教程(第二版)》学习笔记 语句/循环/条件(第5章)print 'AB', 123 ==> AB 123 # 插入了一个空格print 'AB', 'CD' ==> AB CD # 插入了一个空格print 1,2,3 ==> 1 2 3print (1,2,3) ==> (1, ...
阅读全文
摘要:《python基础教程(第二版)》学习笔记 字典(第4章)创建字典:d={'key1':'value1','key2':'value2'}lst=[('key1','value1'),('key2','value2')]; d=dict(lst)d=dict(key1='value1', key2=...
阅读全文
摘要:《python基础教程(第二版)》学习笔记 字符串(第3章)所有的基本的序列操作(索引,分片,乘法,判断成员资格,求长度,求最大最小值)对字符串也适用。字符串是不可以改变的;格式化输出字符串:使用%,%左侧是格式字符串,%右侧是需要格式化的值例如:print '%s=%d' % ('x',100) ...
阅读全文
摘要:《python基础教程(第二版)》学习笔记 列表/元组(第2章) 序列中的下标从0开始x='ABC' ==> x[0]='A', x[1]='B', x[2]='C'负数索引从右边开始。最后一个元素下标是-1;x[-1]='C''ABC'[0] ==> 'A'raw_input("x:")[3] 输
阅读全文
摘要:《python基础教程(第二版)》学习笔记 基础部分(第1章)python常用的IDE:Windows: IDLE(gui), Eclipse+PyDev; Python(command line);Linux/Unix: python1/2=0 # 整除结果为0from __future__ im...
阅读全文
摘要:num_lines = sum(1 for line in open(input_file_name))
阅读全文
摘要:1.open 打开文件使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。 file_object = open('D:\test.txt') try: all_the_text = file_object.read( ) fi
阅读全文
摘要:PyDev for Eclipse, 经过测试,一般在线安装会失败(不能访问某些网站所致)以下为离线安装步骤1 下载 PyDev 2.8.2, 链接:http://sourceforge.net/projects/pydev/files/2 解压 PyDev 2.8.2 文件到 ...\MyEcli...
阅读全文
摘要:#!/usr/bin/python## get subprocess module import subprocess## call date command ##p = subprocess.Popen("date", stdout=subprocess.PIPE, shell=True)## T...
阅读全文
摘要:import subprocessoutput =Popen(["mycmd","myarg"], stdout=PIPE).communicate()[0]import subprocessp = subprocess.Popen(['ls','-a'], stdout=subprocess.PI...
阅读全文
摘要:Python提供的基本数据类型主要有:布尔类型、整型、浮点型、字符串、列表、元组、集合、字典、日期等等函数 描述 type(x) x的数据类型 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 ...
阅读全文
摘要:import os os.path.isfile('test.txt') # 如果不存在就返回False os.path.exists(directory) # 如果目录不存在就返回Falseos.makedirs(targetDir) os.path.join(targetDir, fi...
阅读全文
摘要:生成字符串变量str='python String function' 字符串长度获取:len(str)例:print '%s length=%d' % (str,len(str))连接字符串sStr1 = 'strcat'sStr2 = 'append'sStr1 += sStr2print sS
阅读全文
摘要:1.格式化输出整数python print也支持参数格式化,与C言的printf似,strHello = "the length of (%s) is %d" %(Hello World,len(Hello World))print strHello#输出果:the length of (Hello...
阅读全文
摘要:python中 getopt 模块 (import getopt),该模块是专门用来处理命令行参数的函数getopt(args, shortopts, longopts = [])参数args一般是sys.argv[1:]shortopts 短格式 (-) longopts 长格式(--) 命令行中...
阅读全文
摘要:错误:eclipse+pydev 配置出错,就是在选择python interpreter那一步:See error log for details.com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: ...
阅读全文
摘要:from Tkinter import * root = Tk() root.mainloop()运行出现错误:>>> Traceback (most recent call last): File "E:/××××/Python/test", line 1, in f...
阅读全文