python核心编程 第二天

1、标准输入输出:

import sys

saveout=sys.stdout#保存当前输出状态

logfile=open('E://log.txt','r')# 打开文件

sys.stdout=logfile#定义输出文件

sys.stderr=logfile#定义异常输出文件

print 'helloword'#向日志写入‘helloword’

raise IOError ,'helloword'#''‘向日志写入异常信息‘helloword’''

sys.stdout=saveout#恢复正常输出

print 'hellword' #控制台输出helloword

2、逻辑操作符:and or not

3、表达式按优先级:* 、/、  //、 %、 +、 -

4、不等于!=(<>)

5、>>5<6<7

    >>True

6、>>True+1

  >>2

  >>False+1

  >>1

7、增量赋值:a+=5,x*=6...

8、数值类型:有符号整型、布尔型、常整型、浮点型、复数

9、decimal包表示十进制浮点型

>>decimal.Decimal('1.1')

>>1.1

10、字符串+、*

11、a='helloword'

>>len(a)

>>9

>>a[0:]

>>'helloword'

>>a[-1]

>>'d'

10、元组tuple a=(1,2,3,4,5,6,7)

11、字典:items(),values()、keys()

12 print 布局

>>a=(1,2,3,4,5,6,7,8,9)

>>for i in range(len(a)):

...  print a,

>>1 2 3 4 5 6 7 8 9

>>print 'hello %d %d

... %s'%[1,2,'helloword'*100]

>>hello12helloword........

>>for i in range(0,1,2):

...print i,

>>0 1 2

>>for i ,j in enumerate(a):

... print i,j

>>0 1

>>1 2

>>...

13、列表解析

>>p=[x for x in range(10)]

>>p

>>[1,2,3,4,5,6,7,8,9,10]

>>p=[x for x in range(10) if not x%2]

>>P

>>[0,2,4,6,8]

14、文件操作:

try:

   file=open('E://log.txt','r')#默认打开方式为读,w代表写,a代表追加

   for line in file.readlines():#for line in file:

   print line,

except IOError,e#raise IOError,'helloword'

  print e

file.close()

原文地址:https://www.cnblogs.com/wangzhiqiang003/p/5585931.html