Python Learning notes (i)

Source: Internet
Author: User

Write a recent learning point of Knowledge
1. File read/write
    open a file:
F=open (' path ', ' R ', encoding= ' utf-8 ')
    file to read ' R ' opens, can read the contents of the file, but cannot write
F=open (' Path ', ' W ', encoding= ' utf-8 ')
    file to write ' W ' way open, can write the contents of the file, but cannot read out the contents of the file
    open the file as read, call the Read () method can read the entire contents of the file at one time, Python content
read into memory, Use a str object to represent
    open files in a written manner, and call the Write () method to write the contents into a file
    finally call the Close () method to close the file. The file must be closed because the file object occupies the operating system's
resources, and the number of files that the operating system can open at the same time is limited
    Python introduces the With statement to automatically help us call the Close () method:
with  Open (' path ', ' R ') as F:
    print (F.read ())

    text file to be written to a specific encoding, To pass the encoding parameter to the open () function, the string is automatically converted to the
specified encoding.
    Note:
1. When reading a file, the first time you read the cursor will move to the end, want to continue to read again to move the cursor back to the beginning of
2. Call the Tell () function to get the position of the cursor, Call the Seek () function to move the cursor to the specified position
3. To append ' a ' to open the file, both readable and writable
4. To read one line at a time, call the ReadLine () function
5. Call the ReadLines () function to read the contents of the file in a branch , and save as a list, each row as an
element of the list.


2. use of circular statements
Circular statements are commonly used while and for two forms, drawing flowchart helps us to clear up ideas, better practice using
Cycle
For x in ... The loop is to iterate over each element in the list or tuple, substituting the variable, and then performing the indentation
The statement of the block
For x in range () to generate an integer sequence

Python Learning notes (i)

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.