python学习 ---简明 Python 教程
为什么要用python,开发效率高,可移植.
语法:
字符串:
1,使用单引号(')==使用双引号(")==java双引号(")
2,使用三引号('''或""") ,可以合并多行
3,转义,与java的‘\’类似。注意(在一个字符串中,行末的单独一个反斜杠表示字符串在下一行继续,而不是开始一个新的行。)
4,自然字符串通过给字符串加上前缀
r
或R
来指定。5,字符串不可变,与java类似。
6,字符串会按字面意义级联。
变量:(与java类似)
对象:在python中任何东西都是对象
强烈建议:坚持在每个物理行只写一句逻辑行
python对缩进要求严格
运算符:与java类似。
流程控制:
if:
if
guess == number:
'Congratulations, you guessed it.'
# New block starts here
"(but you do not win any prizes!)"
# New block ends here
elif
guess < number:
'No, it is a little higher than that'
# Another block
# You can do whatever you want in a block ...
else
:
'No, it is a little lower than that'
# you must have guess > number to reach here
while:
while
running:
guess =
int
(
raw_input
(
'Enter an integer : '
))
for :
for
i
in
range
(
1
,
5
):
i
break:
break
语句是用来 终止 循环语句的,即哪怕循环条件没有称为False
或序列还没有被完全递归,也停止执行循环语句。一个重要的注释是,如果你从
for
或while
循环中 终止 ,任何对应的循环else
块将不执行。continue:
continue
语句被用来告诉Python跳过当前循环块中的剩余语句,然后 继续 进行下一轮循环。
函数:
#!/usr/bin/python
# Filename: func_doc.pydef
printMax
(x, y):
'''Prints the maximum of two numbers.
The two values must be integers.'''x =
int
(x)
# convert to integers, if possible
y =
int
(y)
if
x > y:
x,
'is maximum'
else
:
y,
'is maximum'
printMax(
3
,
5
)
printMax.__doc__
模块:
import __name__==‘__main__’ 表示主程序
你可以使用内建的
dir
函数来列出模块定义的标识符。标识符有函数、类和变量。
数据结构:
数组:zoo = (
'wolf'
,
'elephant'
,
'penguin'
),zoo[1],zoo[0]
map:注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以不可变或可变的对象作为字典的值。基本说来就是,你应该只使用简单的对象作为键。
ab = {
'Swaroop'
:
'swaroopch@byteofpython.info'
,
'Larry'
:
'larry@wall.org'
,
'Matsumoto'
:
'matz@ruby-lang.org'
,
'Spammer'
:
'spammer@hotmail.com'
}
ab[‘Larry’],len(ab)
list:
shoplist = [
'apple'
,
'mango'
,
'carrot'
,
'banana'
]
shoplist.sort(),
shoplist.append(
'rice'
)
list
和数组
十分类似,只不过数组
和字符串一样是 不可变的 即你不能修改数组.
序列:list、数组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。
shoplist[
1
:
3
],
shoplist[
2
:],shoplist[:],
shoplist[
-1
],
shoplist[
0
],这些返回的都是拷贝。
引用;当你创建一个对象并给它赋一个变量的时候,这个变量仅仅 引用 那个对象,而不是表示这个对象本身!
面向对象:
self == java的this
__init__
方法:类似java的构造方法__del__:对象消失的时候调用。类型java的finalizne
类的变量 由一个类的所有对象(实例)共享使用。只有一个类变量的拷贝,所以当某个对象对类的变量做了改动的时候,这个改动会反映到所有其他的实例上。
对象的变量 由类的每个对象/实例拥有。因此每个对象有自己对这个域的一份拷贝,即它们不是共享的,在同一个类的不同实例中,虽然对象的变量有相同的名称,但是是互不相关的。通过一个例子会使这个易于理解。
Python中所有的类成员(包括数据成员)都是 公共的 ,所有的方法都是 有效的 。
只有一个例外:如果你使用的数据成员名称以 双下划线前缀 比如__privatevar
,Python的名称管理体系会有效地把它作为私有变量。
这样就有一个惯例,如果某个变量只想在类或对象中使用,就应该以单下划线前缀。而其他的名称都将作为公共的,可以被其他类/对象使用。记住这只是一个惯例,并不是Python所要求的(与双下划线前缀不同)。
同样,注意__del__
方法与 destructor 的概念类似。
继承:
class
Teacher
(SchoolMember):
'''Represents a teacher.'''
def
__init__
(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary'(Initialized Teacher: %s)'
% self.name
def
tell
(self):
SchoolMember.tell(self)'Salary: "%d"'
% self.salary
输入输出:
文件:
你可以通过创建一个
file
类的对象来打开一个文件,分别使用file
类的read
、readline
或write
方法来恰当地读写文件。对文件的读写能力依赖于你在打开文件时指定的模式。最后,当你完成对文件的操作的时候,你调用close
方法来告诉Python我们完成了对文件的使用。
f =
file
(
'poem.txt'
,
'w'
)
# open for 'w'riting
f.write(poem)
# write text to file
f.close()
# close the file
存储器:
Python提供一个标准的模块,称为
pickle
。使用它你可以在一个文件中储存任何Python对象,之后你又可以把它完整无缺地取出来。这被称为 持久地 储存对象。
import
cPickle
as p
#import pickle as p
shoplist = [
'apple'
,
'mango'
,
'carrot'
]
# Write to the file
f =
file
(shoplistfile,
'w'
)
p.dump(shoplist, f)# dump the object to a file
f.close()
# Read back from the storage
f =
file
(shoplistfile)
storedlist = p.load(f)
异常:
class
ShortInputException
(Exception):
'''A user-defined exception class.'''
def
__init__
(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleasttry
:
s = raw_input
(
'Enter something --> '
)
if
len
(s) <
3
:
raise ShortInputException(len
(s),
3
)
# Other work can continue as usual here
except
EOFError:
print
'\nWhy did you do an EOF on me?'
except
ShortInputException, x:
print
'ShortInputException: The input was of length %d, \
was expecting at least %d' % (x.length, x.atleast)
finally:
print
'No exception was raised.'
标准库,and more others