A preliminary discussion of Python list

Source: Internet
Author: User
Tags python list

Today we're going to say a new concept.--list, Chinese can be translated into lists, which are data structures used to process a set of ordered items. Imagine your shopping list, Todo job, phone address book, and so on, all of which can be viewed as a list. To say that it is a new concept is not exact, because we have used it before, in this statement:

For I in range (1, 10):

#此处略过数行代码

Do you see where the list is? You try it:

Print Range (1,10)

The resulting results are:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

This is a list. It is generated by range. Write the For loop statement above:

L = Range (1, 10)

For I in L:

The effect is the same.

As you can see, the for loop is actually going through every item in a list, each time the loop assigns the current item to a variable (here i) until the end of the list.

We can also define our own lists, in the form of a set of values enclosed in brackets and separated by commas:

L = [1, 1, 2, 3, 5, 8, 13]

You can output this list with print:

Print L

You can also use for...in to traverse the list and output each item in the list in turn:

For I in L:

Print L,

The elements in the list can also be other types, such as:

L = [' meat ', ' egg ', ' fish ', ' milk ']

Even different types of mixing:

L = [365, ' Everyday ', 0.618, True]

L as a list, there are some unique features, this we'll talk about next.

Action list

The list was opened last week and the list was known. Suppose we now have a list:

L = [365, ' Everyday ', 0.618, True]

What else can we do besides using for...in to traverse the elements in L?

1. Accessing elements in the list

Each element in the list corresponds to an incremented ordinal number. Unlike real-world serial numbers, the count in a computer usually starts with 0, and Python is no exception. If you do not remember this and cause the error, please go to Stefanie Sun's "Love From scratch".

To access the 1th element in L 365, just use l[0]. In turn,

Print L[1]

It will output ' everyday '

Note that you cannot access a nonexistent element, such as l[10], and the program will error, prompting you to cross the index.

2. Modify the elements in the list

To modify an element in the list, simply assign a value to that element:

L[0] = 123

Output L, get [123, ' everyday ', 0.618, True], 1th element has been changed from 365 to 123.

3. Adding elements to the list

The list has a Append method that can add elements. Take the list of L as an example, the method to invoke is:

L.append (1024)

Output L, you will see [123, ' everyday ', 0.618, True, 1024],1024 was added to the L and became the last element. (The first element was changed to 123 in the previous step)

You can then use l[4] to get 1024.

4. Delete an element in the list

To delete an element in the list, use the Del:

Del L[0]

Output L, get [' everyday ', 0.618, True, 1024]. Then call L[0], will get ' everyday ', the other elements of the sequence number is also corresponding to advance.

Above these commands, you can try it directly in the Python shell.

#==== Penalty Game ====#

I'm going to start today and talk about this little game every day. There are many ways to do this, and I just provide a reference. You can do it the way you like, so she's your game.

Let's start with the direction setting. My idea is simple, that is, in the left and right three directions, with a string to represent. When shooting or extinguishing, enter the direction directly. So here I'm going to use Raw_input. A classmate is to use 1-8 of the number to represent eight directions, each time to enter a number, which is also possible. But the chances are that the goalkeeper will be a little more likely to pounce.

As for the random direction of computer selection, if you are using numbers, just use the randint we talked about earlier to do it randomly. But here's another way I'm going to use random: choice. Its role is to randomly pick an element from a list.

So, the process of free throws can be written like this:

From random import choice

print ' Choose one side to shoot: '

print ' left, center, right '

you = Raw_input ()

print ' You kicked ' +

Direction = [' Left ', ' center ', ' right ']

com = choice (direction)

print ' computer saved ' + com

If you! = com:

print ' goal! '

Else

print ' Oops ... '

And vice versa, not to repeat.

List has two types of common operations: Index and slice (slice).

Yesterday we said that the method of using [] ordinal access is the index operation.

In addition to indexing at the specified location, the list can also handle the index of negative numbers. Continue with yesterday's example:

L = [365, ' Everyday ', 0.618, True]

L[-1] Represents the last element in L.

L[-3] Represents the 3rd element of the countdown.

The slice operator provides a pair of optional numbers within [], separated by:. The number before the colon indicates where the slice begins, and the number after the colon indicates where the slice ends. Likewise, the count starts from 0.

Note that the start position is included in the slice, and the end position is not included.

L[1:3]

The resulting result is [' everyday ', 0.618].

If you do not specify the first number, the slice begins with the first element of the list.

If you do not specify a second number, it is the end of the last element.

is not specified, a copy of the entire list is returned.

L[:3]

L[1:]

l[:]

As with indexes, numbers in a slice can also use a negative number. Like what:

L[1:-1]

Get [' everyday ', 0.618]

#==== Penalty Game ====#

Yesterday I had a free throw, and today I let it loop 5 times and record the score. Don't judge the outcome first.

Use Score_you to show your score and score_com to show your computer score. Start at 0, plus 1 for each goal.

From random import choice

score_you = 0

score_com = 0

Direction = [' Left ', ' center ', ' right ']

For I in range (5):

print ' = = Round%d-you kick! = = =% (i+1)

print ' Choose one side to shoot: '

print ' left, center, right '

you = Raw_input ()

print ' You kicked ' +

com = choice (direction)

print ' computer saved ' + com

If you! = com:

print ' goal! '

Score_you + = 1

Else

print ' Oops ... '

print ' Score:%d (you)-%d (COM) \ n '% (score_you, score_com)

print ' = = Round%d-you save! = = =% (i+1)

print ' Choose one side to save: '

print ' left, center, right '

you = Raw_input ()

print ' You saved ' +

com = choice (direction)

print ' computer kicked ' + com

If you = = com:

print ' saved! '

Else

print ' Oops ... '

Score_com + = 1

print ' Score:%d (you)-%d (COM) \ n '% (score_you, score_com)

Note: The code on your phone may be wrapped.

There are two sections in this code that have a high similarity, and think about whether there are ways to separate them with a function.

A preliminary discussion of Python list

Related Article

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.