Python core Programming the next day

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.