Head of the Python learning notes 2016-04-07__python

Source: Internet
Author: User
Tags clear screen readline
python terminology:

3. Press F5 in the idle editor to "load" the module's code into the idle shell, where the module name is specifically imported into the idle namespace, which is important when using idle, as well as explicit use of the import statement in your code.
4, contains the end= "as a print () bif, and the parameter closes its default behavior (that is, automatically include line wrapping in the input)
5, Idle provides the Help->python Docs menu option (this will open the entire document in your Web browser), you can use this menu to search the entire Python document, if you just want to see a method or function related to the document, you can in the Dile Use Help () BIF in the shell
6, in Python contains two lists, one is called variable list surrounded by brackets [], such as [names], a kind of called immutable list, also called the Yuan Zu, surrounded by parentheses, such as: (names), you can think that Ganso is a list of constants.
7, Exception: Due to run-time errors, will produce a traceback.
8, Traceback: is a detailed description of the error occurred.
9, the third chapter of the new method of finishing:
Open () Method: Opens a disk file, creates an iterator to read data from a file, one line at a time.
ReadLine () Method: Reading a row of data from an open file
Seek () method: Returns the file to its starting position
Split () Method: Splits a string into a list of substrings
Close () method: Closes a file that was previously opened
Find () Method: finds a specific substring in a string
Not keyword: reverse
The Try/except statement provides an exception handling cutoff, which protects certain lines of code that can cause Run-time errors.
Pass statement: Is the empty statement or NULL statement in Python, it does nothing. 13, use range () to control the number of iterations specified

For NUM in range (4):
    print (num)
14. Upload code to PyPI

1, create an account in the http://pypi.python.org/
2, in the Local DOS window input: C:\Users\Tony\AppData\Local\Programs\Python\Python35-32\python.exe setup.py Register #登录刚刚注册的账号
C:\Users\Tony\AppData\Local\Programs\Python\Python35-32\python.exe setup.py sdist upload 15, after each iteration of the Python module version , you have to regenerate the module once, in order to normal use, that is, in the module folder open the DOS window open, execute the following two commands:

C:\Users\Tony\AppData\Local\Programs\Python\Python35-32\python.exe setup.py sdist
C:\Users\Tony\AppData\Local\Programs\Python\Python35-32\python.exe setup.py Install
You can implement version compatibility by setting the default value (level=0) for the parameter:

1.0.0 version:
def print_lol (the_list): For
    item in The_list:
        if Isinstance (item, list):
            print_lol (item)
        Else:
            print (item)
1.1.0 version:
def  print_lol (The_list, level=0): #level =0 is the default value for
    Each_item in the_list:
        if Isinstance ( Each_item, list):
            print_lol (Each_item, level+1)
        else: for
            tab_stop in range (level):
                print ("\ t", end= ')
            print (Each_item)

In this case, even if the module is upgraded, overwriting a function with only one parameter in the previous module, the Print_lol reference in other Python programs will not be affected.

"1.0.0 and 1.1.2 versions can coexist"
nester.print_lol (movies,0)
nester.print_lol (Movies)
nester.print_lol (movies,2)
" If you find that the version of the new iteration is not available, copy the source of the iteration directly, and then generate a new modual. '''
16. How to complete the preload of some modules when opening the Python.exe. take the pre-loading clear screen function as an example:

1. New startup.py path at any location
Content:

You

can also use the From

modulename import functionname
functionname () when you want to import other files from the My import CLS CLS ().

form to call the function ""

2. Create my.py files in the same folder
Content:

Import OS
def CLS ():
    os.system ("CLS");

3. Add an environment variable named Pythonstartup to the computer's environment variable: The property is the startup.py path: E:\Python_WrokSpace\package\startup.py
When using the Python interpreter, we may need to execute some commands each time the interpreter starts. You can include the command you want to execute in a file, and set an environment variable named Pythonstartup to specify the file

Once this is done, it is possible to start Python to automatically load the contents of some preset functions. 17, the use of Python to read files 1, create the folder in the outer folder of the Python.exe file/headfirstpython/chapter3, then create Sketch.txt file in Chapter3, there are several lines of text content in the Sketch.txt file.

As follows: Man:is This right room for a argument?
Other man:i ' ve told you once.
Man:no you haven ' t!
Other Man:yes I have.
Man:when?
Other man:just now.
Man:no you didn ' t!
Other Man:yes I did!
Man:you didn ' t!
Other man:i ' m telling for you, I did!
Man:you did not!
Other Man:oh I ' m Sorry, are this a five minute argument, or the full half hour? man:ah! (taking out his wallet and paying)
Just the five minutes. The other man:just the five minutes.
Thank you.
Other Man:anyway, I did.
Man:you most certainly did not!
Other Man:now let ' s get one thing quite clear:i most definitely told you!
Man:oh no you didn ' t!
Other Man:oh Yes I did!
Man:oh no you didn ' t!
Other Man:oh Yes I did!
Man:oh look, this is isn ' t an argument!
(pause)
Other Man:yes it is!
Man:no it isn ' t!
(pause)
Man:it ' s just contradiction!
Other man:no it isn ' t!
Man:it is!
Other Man:it is not!
Man:you just contradicted me!
Other man:no I didn ' t!
Man:you did!
Other Man:no No no!
Man:you did just then! Other Man:nonsense!
Mans: (exasperated) Oh, this is futile!!
(pause)
Other man:no it isn ' t! Man:yes it is!
2. In the Python interpreter, enter:
Import OS #查看当前的工作空间
Os.chdir ('.. /headfirstpython/chapter3 ') #将工作空间转移到chapter3里
os.getcwd ()
data = open (' sketch.txt ') #打开文本
print ( Data.readline (), end= ')
data.seek (0) #使用seek方法回到文件的第一行, of course, for Python files, you can also use the Tell method for
each_line in data:
    print (Each_line, end= ') #使用for循环来打印sketch text data in. txt
data.close () #最后, the text is finished, remember to turn it off
18, the use of built-in functions BIF processing data 1. The split () method returns a list of strings, which are assigned to a list of target identities. This is called multiple assignment (multiple assignment)
For each_line in data:
# Each_line: The previous part was assigned to role, and the latter part was assigned to Line_spoken (role
    , line_spoken) = Each_ Line.split (': ') print (role
    , end= ') print
    (' said: ', end= ')
    print (Line_spoken, end= ')

Enter in idle: Help (Each_line.split) to view the split assistance document
by S.split ([sep[, Maxsplit]), there is an optional option in the Split function. Known as Maxsplit, when the maxsplit=1 will be divided into two parts each_line, with the maxslipt=3 when the each_line will be divided into four.

#将上面的代码改成如下所示: For
each_line in data:
    [role, Line_spoken] = Each_line.split (': ', 1)
    print (role, end= ')
    print (' said: ', end= ')
    print (Line_spoken, end= ')
#这样的话, no matter how many in a row of data: are processed into two parts
#对代码进行更进一步的改进, you can each_line in a row without an error, and normally run for
each_line in data:
    #注意if取反的方式是加一个关键字: Not, and that is, don't forget to add :
    If not each_line.find (': ') = =-1:
        [role, Line_spoken] = Each_line.split (': ', 1)
        print (role, end= ")
        print (' said: ', end= ')
        print (Line_spoken, end= ')
#通过异常捕获机制来完成代码改进, when a row of data is not: the time can still not complain, and normal operation. For
each_line in data:
    if not each_line.find (': ') = = 1:
        try:
            (role, line_spoken) = Each_line.split (': ', 1)
            Print (role, end= ')
            print (' said: ', end= ')
            print (Line_spoken, end= ")
        except: Pass
            #当捕获异常的时, For example, the whole line of Each_line is not: When the start of the except content, the error pass away, continue to perform the next operation.

To design exception handling code to handle a particular type of error, be sure to specify the type of error on the except code. As a result, the generalized exception handling code can be turned into an exception handling code with attributes.

Try:
    data = open (' sketch.txt ')
    for each_line in data:
        try:
            (role, Line_spoken) = Each_line.split (': ', 1 Print (Role
            , end= ')
            print (' said: ', end= ')
            print (Line_spoken, end= ')
        except valueerror:# The type of error is specified here as ValueError, so that when a valueerror error occurs, it is automatically skipped.
            pass
except IOError: #在这里指定错误的类型为IOError, so that when a ioerror error occurs, it automatically prints ' The data file is misssing! '
    Print (' The data file is misssing! ')

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.