Zero-based Python tutorial - how to modify the list of elements

In order to better learn how to modify the elements of the list, this time we will use a simple little game as an example, we now want to create a game that requires players to shoot enemies from the sky; this end, at the beginning of some enemy store, and then whenever an enemy is killed, it will be deleted from the list in the list, and each time a new enemy appears on the screen, it will be added to the list. Throughout the game runs the length of the list of enemies will continue to change.

We will use throughout the game always envisaged, amend the list of elements, add elements in this list, deleted explain the elements of the list, first, we look at how to modify the elements in the list.

Python, the syntax is similar to modify the list of elements and access list syntax elements. To modify the list of elements, you can specify a list of names and index you want to modify elements, and then specify a new value of the element. For example, suppose you have a list of the motorcycle in which the first element is 'honda', how to modify the value of it? 

motorcycles.py

motorcycles = ['honda', 'yamaha', 'suzuki']

print(motorcycles)

motorcycles[0] = 'ducati' print(motorcycles)

We first define a list of the motorcycle in which the first element is 'honda'. Next, we will change the value of the first element of 'ducati'. The output shows that the value of the first element has indeed changed, but the values ​​of the other elements of the list has not changed:

[ 'Honda', 'yamaha', 'suzuki'] [ 'Ducati', 'yamaha', 'suzuki']

Of course, you can modify any value in the list of elements, not just the first element of a value.

Disclaimer: The content and images from the network, belongs to original author, if your original copyright infringement, please inform, we will remove the content as soon as possible.

Well, today's share is over here, if you need more technical articles have direct access to the European Marco education official website!

Guess you like

Origin www.cnblogs.com/woshijiuke/p/12101565.html