1. Standard input and output:
Import Sys
saveout=sys.stdout# Saving current output state
Logfile=open (' E://log.txt ', ' R ') # Open File
sys.stdout=logfile# Defining output files
sys.stderr=logfile# defining an exception output file
print ' Helloword ' #向日志写入 ' Helloword '
Raise IOError, ' helloword ' # ' writes exception information to the log ' Helloword '
sys.stdout=saveout# return to normal output
print ' Hellword ' #控制台输出helloword
2. Logical operator: and OR not
3, the expression by priority: *,/,/,%, +,-
4, Not Equal! = (<>)
5, >>5<6<7
>>true
6, >>true+1
>>2
>>false+1
>>1
7, Increment assignment: A+=5,x*=6 ...
8. Numeric type: Signed integer, Boolean, constant integer, floating point, plural
9. Decimal package represents the decimals floating point type
>>decimal. Decimal (' 1.1 ')
>>1.1
10. String +, *
11, a= ' Helloword '
>>len (a)
>>9
>>a[0:]
>> ' Helloword '
>>A[-1]
>> ' d '
10, tuple tuple a= (1,2,3,4,5,6,7)
11. Dictionary: Items (), values (), keys ()
Print Layout
>>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. List parsing
>>p=[x for x in range (10)]
>>p
>>[1,2,3,4,5,6,7,8,9,10]
>>p=[x for x in range (ten) if not x%2]
>>p
>>[0,2,4,6,8]
14. File Operation:
Try
File=open (' E://log.txt ', ' R ') #默认打开方式为读, W for write, A for append
For line in File.readlines (): #for the line in file:
Print line,
Except Ioerror,e#raise IOError, ' Helloword '
Print E
File.close ()
Python core Programming the next day