python使用基础

来源:互联网 发布:linux中开启ftp端口号 编辑:程序博客网 时间:2024/05/16 19:25

二、python使用基础

标签: python

1. 基本概念

常量、数、字符串、变量、数据类型、逻辑行与物理行、缩进

复数的使用和表示法

eval的各种用法

什么是hash

逻辑概念和物理概念

2. 运算符与表达式

表达式在计算机中是如何展开的

  1. a =1
  2. b=2
  3. '''
  4. if a ==1:
  5. print 1
  6. elif a==2:
  7. print 2
  8. else:
  9. print 3
  10. '''
  11. a = None
  12. if a is not None:
  13. print a

repr的各种使用

3. 逻辑控制结构

为什么Python循环控制结构会有else

  1. '''
  2. for x in range(100):
  3. print x
  4. '''
  5. for y in xrange(5,20,3):
  6. print y
  7. a = ("a", 1,2,3,"asdf")
  8. for o in a:
  9. print o
  10. else:
  11. print "over"
  12. a=1
  13. while a<100:
  14. print a
  15. '''
  16. if a>50:
  17. continue
  18. '''
  19. a = a+1

4. 函数

  1. def f1():
  2. print 1
  3. f1()
  4. def f2(a):
  5. print a
  6. f2(1)
  7. f2("abc")
  8. def f3(a=1, b=2, c=3):
  9. """this is a, b, c"""
  10. print 'a',a
  11. print 'b',b
  12. print 'c=%d'%c
  13. return a, b+c
  14. f3(1,2,3)
  15. f3(b=5,c=6)
  16. print f3(5,6)
  17. d = f3(b=4, c=9)
  18. #print c
  19. print d
  20. #alis for function
  21. fx = f3
  22. fx()
  23. f4 = lambda x, y: x+y
  24. print f4(2,3)

包闭的使用

  1. def f1(a):
  2. def f2(b):
  3. return a+b
  4. return f2
  5. q= f1(10)
  6. print q
  7. p = f1(20)
  8. print p
  9. print q(1)
  10. print p(1)

5. 面向对象编程

与大多数的语言不同,一个 Python 函数,方法,或属性是私有还是公有,完全取决于它的名字。 
如果一个 Python 函数,类方法,或属性的名字以两个下划线开始(但不是结束),它是私有的;其它所有的都是公有的。

为什么python私有函数会这样表示,及作用

  1. class a:
  2. def __init__(self):
  3. self.m=1
  4. def add(self):
  5. self.p=4
  6. print self.m+self.p
  7. class b(a):
  8. def __init__(self):
  9. a.__init__(self)
  10. self.n=2
  11. self.tt()
  12. self.__dd()
  13. def sum(self):
  14. print self.m+self.n
  15. def tt(self):
  16. self.m=6
  17. def __dd(self):
  18. print "dd"
  19. def __cc__(self):
  20. print "cc"
  21. c = b()
  22. c.sum()
  23. c.add()
  24. ##c.__dd() #__** can't be call
  25. c.__cc__()

6. 异常处理

什么叫防御式编程

  1. #coding=utf-8 //处理出现中文注释报错
  2. #!/usr/bin/python2.7 //环境变量问题,指定pyton的版本
  3. try:
  4. func()
  5. except Exception, e:
  6. print e.message

7. 模块与包

init.py的妙用

  1. mkdir testp && cd testp
  2. touch __init__.py
  3. cd .. && vi pktest.py
  4. from testp.classtest import b
  5. c = b()
  6. c = sum()

8. 输入、输出、文件和目录操作

  1. f= open(r"./pktest.py")
  2. #print f.read()
  3. while True:
  4. line=f.readline()
  5. print line
  6. if line == "":
  7. break
  8. f.close()

write 
seek 
os.lisdir 
os.walk

0 0
原创粉丝点击