二、python使用基础
标签: python
1. 基本概念
常量、数、字符串、变量、数据类型、逻辑行与物理行、缩进
复数的使用和表示法
eval的各种用法
什么是hash
逻辑概念和物理概念
2. 运算符与表达式
表达式在计算机中是如何展开的
a =1
b=2
'''
if a ==1:
print 1
elif a==2:
print 2
else:
print 3
'''
a = None
if a is not None:
print a
repr的各种使用
3. 逻辑控制结构
为什么Python循环控制结构会有else
'''
for x in range(100):
print x
'''
for y in xrange(5,20,3):
print y
a = ("a", 1,2,3,"asdf")
for o in a:
print o
else:
print "over"
a=1
while a<100:
print a
'''
if a>50:
continue
'''
a = a+1
4. 函数
def f1():
print 1
f1()
def f2(a):
print a
f2(1)
f2("abc")
def f3(a=1, b=2, c=3):
"""this is a, b, c"""
print 'a',a
print 'b',b
print 'c=%d'%c
return a, b+c
f3(1,2,3)
f3(b=5,c=6)
print f3(5,6)
d = f3(b=4, c=9)
#print c
print d
#alis for function
fx = f3
fx()
f4 = lambda x, y: x+y
print f4(2,3)
包闭的使用
def f1(a):
def f2(b):
return a+b
return f2
q= f1(10)
print q
p = f1(20)
print p
print q(1)
print p(1)
5. 面向对象编程
与大多数的语言不同,一个 Python 函数,方法,或属性是私有还是公有,完全取决于它的名字。
如果一个 Python 函数,类方法,或属性的名字以两个下划线开始(但不是结束),它是私有的;其它所有的都是公有的。
为什么python私有函数会这样表示,及作用
class a:
def __init__(self):
self.m=1
def add(self):
self.p=4
print self.m+self.p
class b(a):
def __init__(self):
a.__init__(self)
self.n=2
self.tt()
self.__dd()
def sum(self):
print self.m+self.n
def tt(self):
self.m=6
def __dd(self):
print "dd"
def __cc__(self):
print "cc"
c = b()
c.sum()
c.add()
##c.__dd() #__** can't be call
c.__cc__()
6. 异常处理
什么叫防御式编程
#coding=utf-8 //处理出现中文注释报错
#!/usr/bin/python2.7 //环境变量问题,指定pyton的版本
try:
func()
except Exception, e:
print e.message
7. 模块与包
init.py的妙用
mkdir testp && cd testp
touch __init__.py
cd .. && vi pktest.py
from testp.classtest import b
c = b()
c = sum()
8. 输入、输出、文件和目录操作
f= open(r"./pktest.py")
#print f.read()
while True:
line=f.readline()
print line
if line == "":
break
f.close()
write
seek
os.lisdir
os.walk