Concise Python tutorial --- 9. Data Structure
I believe that the data structure is not unfamiliar to anyone who has learned programming. Here we will only introduce the three built-in data structures in Python: List, tuples, and dictionaries.
List)
List is the data structure used to process a group of ordered objects.
Mylist = ['A', 'B', 'C', 'D'];
For element in mylist:
Print element;
In python, a method append is provided for list. You can add objects to the list.
Mylist. append ('E ');
Note that deleting an object from the list is not implemented through the list method. You must use the del statement to delete objects from the array.
Del mylist [2];
You can run the following program to observe the above situation:
Mylist = ['A', 'B', 'C', 'D'];
For element in mylist:
Print element;
Print ('------------------------');
Mylist. append ('E ');
For element in mylist:
Print element;
Print ('------------------------');
Del mylist [2];
For element in mylist:
Print element;
Tuples
Tuples are very similar to lists. The difference between them is that the tuples cannot be modified. You cannot add objects to or delete objects from tuples.
The declaration of tuples is different from that of the list. The list is declared using square brackets, while the tuples are declared using parentheses.
Mytuple = ('A', 'B', 'C', 'D ');
For element in mytuple:
Print element;
Dictionary
The dictionary is like querying the address book of a person's contact information by name. If you have learned Java, you must know map. Yes, the dictionary in python is very similar to the map function in Java.
Mydict = {'Tom ': u'u.s.', 'hangzhou': u'china '};
Print mydict ['hangzhou'];
Sequence
Lists, tuples, and strings are all sequences. But what are sequences? The two main features of the sequence are index operators and slice operators. Here is just an example to illustrate what the sequence is.
Shoplist = ['apple', 'mango', 'carrot', 'bana']
# Indexing or 'subauthorization' operation
Print 'item 0 is ', shoplist [0]
Print 'item 1 is ', shoplist [1]
Print 'item 2 is ', shoplist [2]
Print 'item 3 is ', shoplist [3]
Print 'item-1 is ', shoplist [-1]
Print 'item-2 is ', shoplist [-2]
# Slicing on a list
Print 'item 1 to 3 is ', shoplist []
Print 'item 2 to end is ', shoplist [2:]
Print 'item 1 to-1 is ', shoplist [1:-1]
Print 'item start to end is ', shoplist [:]
# Slicing on a string
Name = 'swaroop'
Print 'characters 1 to 3 is ', name []
Print 'characters 2 to end is ', name [2:]
Print 'characters 1 to-1 is ', name [1:-1]
Print 'characters start to end is ', name [:]