posts - 195,  comments - 30,  trackbacks - 0
Tutorial : http://www.tutorialspoint.com/python/python_files_io.htm 

Python IO
输出 print
str = raw_input("Enter your input: "); print "Received input is : ", str


Python只有三种变量类型 int, string, float? 
typeof(1.5)
貌似不支持隐式类型转换
print str(2.5)  对
print 2.5 错
print '2.5' 对

python 定义方法是
def MethodName(para,para2): #注意这里的冒号
      if 

python注释
#单行注释
"""  三个双引号是多行注释 """

python 引用
include math


Python的str
str(var)类型转换
len(var)
var.upper()
var.lower()
var[2] 第三个(注意下标从0开始)元素,类似于list
var[:3] 前三个元素,实际上指的是0截止到3-1的元素
var[2:4]下标是2到4-1的所有元素

Python的list
exampe=[a,b,c,d,e,f];
len(exampe)
自带sort方法

Python的dictionary
key -value对应
value可以是一个list
注意方括号[]里面只能使用key
区分 del dict['Name']


del dict['Name']; # remove entry with key 'Name' dict.clear();     # remove all entries in dict del dict ;        # delete entire dictionary
自带的方法:

1cmp(dict1, dict2)
Compares elements of both dict.
2len(dict)
Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.
3str(dict)
Produces a printable string representation of a dictionary
4type(variable)
Returns the type of the passed variable. If passed variable is dictionary then it would return a dictionary type.




Python类的定义和类方法的定义
定义类不需要用def class,直接

class ClassName(object):
每个类都有 __init__(self,arg):
方法,注意是 左右各两个下划线,总共4根下划线
 
类方法都需要包含self这个参数,但是使用的时候不需要self,见下例
                  
例如
class Adder(object):
def __init__(self):
self.baseNum = 2
def prnt_num(self):
print self.baseNum
def add_to_base(self, arg):
# Your code here
self.baseNum+=arg
print self.baseNum
objectVar = Adder()
objectVar.prnt_num()
# Your code here
objectVar.add_to_base(3)

Python中的类变量不能 self.xxx来引用,但是成员变量可以
Class variables are special because they belong to the 
class; the objects created do not get their own copies of the class variable. Class variables are accessed using the class name and dot notation.
ClassName.classVar
Class variables are created outside of__init__ 
例如:
class Widget(object):
objID = 0
def __init__(self):
Widget.objID += 1
# Your code here
self.myID=Widget.objID

常犯错误:indentation is very important  
python indentation error expected an indented block
还有一个错误就是 类方法,必须使用 self参数,即使没有参数!!
另外一个常错的地方就是 __init__(self,arg) 一定是四根下划线



不仅要记得留dent 还要记得 缩进






posted on 2012-09-09 14:47 luis 阅读(359) 评论(0)  编辑 收藏 引用 所属分类: Python

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

友情链接

搜索

  •  

最新评论

阅读排行榜

评论排行榜