List of Python basics, dictionaries, collections

Source: Internet
Author: User
Tags unique id

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.