**List:**

A list is an ordered set of elements that can be added and removed at any time. The elements in the list can be of different types, or it can be a list, nested, equivalent to a two-dimensional array. list=[' Dfs ', ' DSF ', 123], empty list=[]

Len (): The function can get the number of list elements

List[0]: element value with subscript 0

LIST[-1]: The first element of the countdown, that is, the last number

Pop (): The element that pops up at the end

Pop (i): Eject the first element

Append (): Append element to end of list

Insert (1, ""): Inserts the element into the specified position, such as the index number 1.

Sort (): Sort the elements

To replace an element with another element, you can assign a value directly to the corresponding index position

Mutable and Immutable objects:

The string is immutable, such as str= "Apple"; the result of Str.replace (' A ', ' a ') is Apple, but Str is Apple

The list is mutable: for example, List = [1,2,4,3,5]; List.sort (); The result is 12345,list and 12345.

Therefore, for an immutable object, any method that invokes the object itself does not change the contents of the object itself. Instead, these methods create a new object and return it, ensuring that the immutable object itself is always immutable.

**Tuple:**

A sequence list is called a tuple. The tuple and list are very similar, but once the tuple is initialized it cannot be modified without the append (), insert () method. Other methods to get elements are the same as lists. When you define a tuple, the elements of the tuple must be determined when defined. tuple= (' Dfs ', ' DSF ', 123), empty tuple= (); only one element of tuple= (1,)

When displaying a tuple of only 1 elements, add a comma to avoid misunderstanding the parentheses in the mathematical sense.

"Variable" tuple:tuple = (' A ', ' B ', [' A ', ' B ']); tuple[2][0]= ' x '; tuple=[2][1]= ' y '; when the output tuple is (' A ', ' B ', [' X ', ' y '])?

This tuple is defined by 3 elements, namely ' a ', ' B ', and a list.

After modifying the value of the list, change to:

The value that the tuple points to does not change, only the value that the list points to, and the list that the tuple points to is not changed to another list,tuple the so-called "invariant" is that each element of a tuple, pointing to never change. That point ' a ', it cannot be changed to point to ' B ', pointing to a list, cannot be changed to point to other objects, but the list itself is variable!

**Dictionary:**

Dictionary, equivalent to map, a key-value pair, using curly braces, Dict = {' Apple ': 1, ' Bananan ': 2, ' Orange ': 3}. The corresponding use of dict[' Apple ' will be 1.

This Key-value storage method, when put in, must be based on the key to calculate the storage location of value, so that the time can be taken by the key to get the value directly. Add value to a key multiple times, and the value below will flush the previous value out of the update. Key must be an immutable object.

Determine if there is a key: one, you can use in: ' Apple ' in dict output true. The Get method provided by Dict, if key does not exist, can return none, or its own specified value:dict.get (' Apple '), Dict.get (' Apple ',-1).

Pop (key) method, delete a key, and the corresponding value will also be removed from the Dict

Compared with list, Dict has the following features:

The speed of finding and inserting is very fast and will not increase with the increase of key;

It takes a lot of memory, and it wastes a lot of memory.

And the list is the opposite:

The time to find and insert increases as the element increases;

Small footprint and little wasted memory.

So, Dict is a way of exchanging space for time.

**Set**

Set is similar to Dict and is a set of keys, but does not store value. In set, there is no duplicate key.

To create a set, you need to provide a list as the input collection: S=set ([+/-)], and the duplicate values are automatically filtered: S=set ([1,2,2,3,3]) is set ([+/-)]

Add (Key): The method can be added to the set

Remove (Key): Method can delete element

Set can be thought of as a set of unordered and non-repeating elements in mathematical sense, so that two sets can do the intersection of mathematical sense, and the combination of a set of things (S1 & S2 | S3)

**Slice slice:**

L[0:3]:l[:3] takes the first 3 elements, starting at index 0, until index 3, but excluding index 3.

L[-2:]: Countdown two number (last two digits), the index of the final number is-1

L[-2:-1]: The penultimate number, this time does not include-1 is the last number.

L[::5]: Take one per 5 numbers,

**Iteration:**

Given a list or tuple, we can traverse the list or tuple through a for loop. For.....in

By default, the Dict iteration is key. If you want to iterate over value, you can use for value in D.itervalues (), if you want to iterate both key and value at the same time, you can use a for-K, V in D.iteritems ().

Because a string is also an iterative object, it can also be used for A For loop: for ch in ' ABC '

When you use a for loop, the for loop works as long as it works on an iterative object, and you don't have to know whether the object is a list or another data type.

How can I tell if an object is an iterative object? The method is judged by the iterable type of the collections module:

>>> from collections Import iterable

>>> isinstance (' abc ', iterable) # whether STR can iterate

True

What if you want to implement subscript loops like Java for a list? Python's built-in enumerate function can turn a list into an index-element pair so that both the index and the element itself can be iterated in the For loop:

>>> for I, value in enumerate ([' A ', ' B ', ' C '):

... print I, value ...

0 A

1 B

Python Learning notes (ii)