python--lists and functions

Source: Internet
Author: User

Before the touch of a little python, long time no see, recently want to learn again, while recording their own learning process, warm so know new. first knowledge of Python

Python is an interpretive programming language, the source file is a text file ending in *.py, interpreted and executed by the Python interpreter (interpreter), and does not need to be compiled (for the user).

In fact, the Python interpreter first compiles the script into a bytecode file (ending in *.pyc) before executing the script.

We can also manually implement this process via Python's self-contained module library (py_compile), and write the following script:

Import Py_compile
py_compile.compile (R ' D:\workspace\test.py ')
#文件名前面的r表示: The string is not escaped

Oneself in the study process, the grammar need to notice the point, roughly lists:
(1) No nested statements are in the first column.
(2) in interactive mode, the interaction interpreter automatically prints the result of the expression, but in the *.py script file, the print statement must be used for output.
(3) In interactive prompt mode, ends the compound statement with a blank line. By inserting a blank line to tell interactive Python, multiline statements have been entered. (no blank lines are required in the *.py file, empty rows are ignored)
(4) The file extension (*.py) is required to execute the script file on the command line, but the file name extension cannot be used when the module is imported.
(5) In Linux, the first line is usually #. /usr/bin/python, which specifies the path to the Python interpreter. A better approach is to search through the ENV program for all directories listed in the PATH environment variable, the first line written as #. /usr/bin/env, so it's easy to transplant.
(6) By adding the input statement at the end of the *.py file, you can make the command prompt window that appears when you double-click to run the script not "flash over" (automatically quits after the program completes).

Refer to the example in the first Python chapter of head, the use of lists and functions

-Lists (list)
The use of lists is similar to the arrays in C, and the list is more powerful.
The list has no fixed size and type constraints, the same list can contain different types of data, you can index, slice, add elements, delete elements, sort, and so on.

Definition of list:

Movies = ["The Holy Grail", "The Life of Brain", "The Meaning of life"]

instance, outputting each item in the list line by row:

Movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam",, 
            ["Graham Chapman", ["Michael Palin", "John Cl Eese ",
                    " Terry Gilliam "," Eric Idle "," Terry Jones "]]"

print movies for

each_item in movies:
    if Isinstance (Each_item, list): For
        Each_item_inner in Each_item:
            if Isinstance (Each_item_inner, list):
                for Each_item_inner_deeper in Each_item_inner:
                    print each_item_inner_deeper
            else:        
                print Each_item_inner
    Else:
        print Each_item

-Functions (function)
Definition of function:

def function_name ([arguments]):
    ' Optional documentation string '
    Function_suite

Because the list is nested with a child list, the code structure above can become complex and redundant if there is too many nesting layers.
We define a function, replace the duplicate code with a function, and, by recursive invocation, solve the problem of nesting that may be too deep.

Define a function, recursive invocation, to output each item in the list line by row:

def print_lol (the_list): For
    Each_item in the_list:
        if Isinstance (Each_item, list):
            print_lol (Each_item)
        Else:
            print Each_item

By calling the Print_lol () function, you can implement row-by-line output list content.

The above code involves the following:
-For * in * iteration, traversing the usage of the list (similar to the foreach usage in Java);
-Isinstance (,) to determine whether a particular identifier contains a particular type of data;
-If Else's syntax

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.