1, List
(1) L1 = [] # Create an empty list
#需要注意的是, Python is different from other languages such as Java, and does not need to declare a data type when defined. The specific reason is
Instead of declaring variables, Python does not have to declare the return type of a function because of its "if type" language characteristics.
In other languages, for example, in a C + + language, before storing a single data, you need to create a fixed memory space in memory for this data,
and assign a unique ID (variable name) to this type of space before placing the data to be stored in the memory space of the variable name.
Python's approach is ' data-centric ', which puts the data to be stored in memory and then uses a variable name to ' reference ' the data.
(2) L2 = [' A ', ' B ', ' C '] #和数组一样, counting starting from 0.
(3) l1=[' A ', [' B ', ' C ', ' d ']]# nested definition
(4) l4=list ()
Print (L4)
#还有很重要的一个就是, Python's main code shelf write, That is, there is no space in front of the code, and the If while code does not. Otherwise it will report unexpected indent error (5)
Number of print (L2.count (' a ')) #统计a
Print (L2.index (' A ')) # Find the index of a
L2.insert (2, ' Insert ') # Insert
L2.remove (' a ') #删除
L2.sort () # Sort, mainly alphabetically
L2.clear () # Empty All
For i in L2: # Loop Traversal, note to add :
Print (i)
2, Dictionary
# Dictionary similar to the MAP in Java has key and value
dic={
' s1 ': ' Dong Jintao ',
' s2 ': ' Male ',
' S3 ': ' A ',
}
dic[' S4 ']= ' Han ' # add
dic[' S3 ']= ' 18 ' # Modify
For I in DIC: # Traverse Print
print (I,dic[i])
3, set
The # Collection is similar to adding a Delete method and list to a collection in Java.
ip = {' Dong ', ' Jin ', ' Tao ', ' Andy '};
Ip2 = {' Dong ', ' tao ', ' haha '};
Ip.intersection (IP2); # intersection
Ip.difference (IP2); # difference Set
Ip.union (IP2) # and set
Ip.symmetric_difference (IP2); #对称差
List of Python basics, dictionaries, collections