Key points of the fourth lesson of the itpub Python course:
Lesson 4 notes Python Common Data Structures
List:
Define ex: A = [1, 2, 3, 4, 5]
One element can be of multiple types
2 elements can also be of the list type, or multiple Nesting is also allowed.
Multiple Generation Methods A = [X for X in range (10)]
Common Operations:
A. append (x) append operation
A. merge two extend (x) lists
A. Count (x) counts the number of elements
A. insert (index, x) insert the corresponding element at the specified position
A. Pop () removes the last element
A. Remove (x) deletes an element. Each time the first element is deleted
A. Reverse () Reverse the list element order
A. Sort () sorts the list elements.
Slice operation
Tuple:
Define ex: B = (1, 2, 3, 4, 5)
Many operations are similar to list operations, but tuples are immutable objects. List is suitable for arrays that are frequently added, deleted, and modified. tuples are not suitable for set operations, such as passing values.
Slice operation
>>> A = ([2, 3], 33) >>> print a ([2, 3], 33) >>> print a = 4 file "<stdin> ", line 1 print a = 4 ^ syntaxerror: Invalid Syntax # The syntax in the video is wrong. It is not the current syntax that is wrong. The tuples can contain list.
In the "Python core programming" section, the tuples are not so immutable. We can find such a case. Later, the teacher corrected it.
>>> a([2, 3], 33)>>> a[0][1]=44>>> print a([2, 44], 33)
The list in the tuples can also change the value.
Dirt:
Definition: A = {:44; 2: '3 '}
Common Operations:
A. has_key (x) x exist in?
For o in A. iteritems ():
Print o # Return tuples
B = [X for X in A. itervalues ()]
A. Values ()
Sorted (a) # A build-in functon
Set:
Set (x) # repeated operations on a set