Get started with Python with C or C + + basics: Python Crash Course 4 action list 4.4--4.5

Source: Internet
Author: User

Previous post.

4.4 Using part of the list

  One, slice

Tangent edges, as the name implies, are part of the process of working with lists .

We can contact a C + + statement: for (int i = 0; i < n-2; ++i) cout << a[i];

This statement is a 1--N-2 element that accesses an array of n elements (subscript 0--n-3).

This implementation is implemented in Python as follows:

 

1Players = ['Charles','Martina','Peter','Mina']2 Print(Players[:4])3 Print(Players[0:3])4 Print(Players[1:3])5 Print(players[2:])6 Print(players[-3:])

The codes from 2 to 6 are:

    1. Prints the first to fourth elements of the list (subscript 0 to 3). Note that ": X" means subscript from 0 to x-1. "x:" means the subscript from X to n-1.
    2. ' Print the first to third element of the list (subscript 0 to 2).
    3. Prints the second to third element of the list (subscript 1 to 2).
    4. Prints the list element to the end element (subscript from 2 to n-1) starting from the third element.
    5. Prints the bottom three elements of the list (subscript n-3 through n-1). Note that the value is negative when the number starts from the back.

  Second, copy the list

Let's look at the following code:

1names = ['Peter','Mina','Mpeter','Katherine']2My_friends =names[:]3 4 Print('I have a list of my friends:')5 Print(names)6 Print("however Mina have a same list!")7 Print(my_friends)8 Print("But Mike just has both in my list")9Mike_friends = Names[0:2]Ten Print(mike_friends)

The principle of copying a list is actually simple:

  Declares a list, and assigns a value, and then declares a list B, and then iterates through list A and passes the value to the end of List B.

4.5 Yuan zu

  In Python, the immutable list is called the ancestor.

In fact, it is similar to the Const definition constant Group in C + + and cannot be modified once defined.

The definition rules are as follows: ( defined by the "()" bracket )

Dimensions = (1, +, +)print (dimensions[0])print(dimensions[1])

And if you want to change, you will get an error!

1 dimensions = (1,3,2), print(dimensions[0) (dimensions[1])4#error!! 5 # Dimensions[0] = 2 6 # print (dimensions[0])

As with the normal list, the progenitor can also traverse:

Const_numbers = (1, 2, 3, 4, 5, 6) for in const_numbers:    Print(number)

If you want to change the value of the progenitor, you can only redefine it like this:

Const_numbers = (2, 3, 4, 5, 6, 7) for in const_numbers:    Print( Number

                      To be Continued ...

If there is any mistake, please comment!

Get started with Python with C or C + + basics: Python Crash Course 4 action list 4.4--4.5

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.