Python Basic Knowledge Vulnerability Summary

Source: Internet
Author: User

1. String Formatting: Format
1 #format2S0 ="my Name {}, this year {} years old, I like { }". Format ("Wang Leehom", 46,"Wahaha")#format by Location3S1 ="My name is {0}, this year {2} years old, I like {1}". Format ("Wang Leehom","Wahaha", 46)#Specify location formatting4S2 ="my name {name}, this year {age}, I like {lover}". Format (name ="Wang Leehom", age = 46,lover ="Xu Jinglei")#Specify Keywords5 Print(S0)6 Print(S1)7 Print(S2)

2. Iteration Add: Extend
1 li = ["a","b","C" ]2 li.extend ("xyz")3Print (LI)

3. Go to repeat 1) use Set properties
1L1 = ['C','D','R','D','R','a','C']2L2 =list (set (L1))3L2.sort (Key=l1.index)#The elements in the set are unordered, and sort can be ordered according to the order of L1 elements.4 Print(L2)
2) for Loop
1L1 = ['C','D','R','D','R','a','C']2L2 = []3  forElinchL1:4     ifEl not inchL2:5 l2.append (EL)6 Print(L2)
3) sort after while loop
1L1 = ['C','D','R','D','R','a','C']2 L1.sort ()3L2=[]4n =05  whileN <Len (L1):6     ifL1[n] not inchL2:7 l2.append (L1[n])8n = n + 19     Else:Tenn = n + 1 One Print(L2)

4. Small data Pool 5. Depth copy

1) Shallow copy: Copy only the first layer, the lower one (same address)

1L1 = ["ants","Blue Cat","Shadow Demon"]2L2 =l1.copy ()3L1.append ("Parker")4 Print(L1)5 Print(L2)6 Print(ID (L1), id (L2))7 #[' Ant ', ' Blue Cat ', ' shadow ', ' Parker ']8 #[' Ant ', ' Blue Cat ', ' Shadow Demon ']9 #2527144744328 2527144744264
L1 = ["ants","Blue Cat","Shadow Demon",["giant Demon","Aircraft","Lena"]]l2=l1.copy () l1[3].append ("Parker")Print(L1)Print(L2)Print(ID (l1[3]), ID (l2[3]))#[' Ant ', ' Blue Cat ', ' shadow ', [' troll ', ' airplane ', ' Lena ', ' Parker ' ]#[' Ant ', ' Blue Cat ', ' shadow ', [' troll ', ' airplane ', ' Lena ', ' Parker ' ]#2988850058632 2988850058632

2) deep copy: completely different individuals

ImportCOPYL1= ["ants","Blue Cat","Shadow Demon",["giant Demon","Aircraft","Lena"]]l2= Copy.deepcopy (L1)#Note the usage, you need to import the copy moduleL1[3].append ("Parker")Print(L1)Print(L2)Print(ID (l1[3]), ID (l2[3]))#[' Ant ', ' Blue Cat ', ' shadow ', [' troll ', ' airplane ', ' Lena ', ' Parker ' ]#[' Ant ', ' Blue Cat ', ' shadow ', [' troll ', ' airplane ', ' Lena ']#1751479821960 1751479823368

6.fromkeys
1DIC = Dict.fromkeys (["Jay","JJ"],["Jay Chou","JJ Lin"])2 Print(DIC)3 #{' Jay ': [' Jay Chou ', ' JJ Lin '], ' JJ ': [' Jay Chou ', ' JJ Lin ']}4Dic.get ("Jay"). Append ("ha ha haha")5 Print(DIC)6 #{' Jay ': [' Jay Chou ', ' JJ Lin ', ' hahaha '], ' JJ ': [' Jay Chou ', ' JJ Lin ', ' ha Ha ']} since the DIC has a common list of Jay and JJ, Jay adds elements and the corresponding JJ also adds elements
7. r+ Mode for file operation

r+ mode is the append read and write, but not only read and write, you can write after reading, note that the first read after writing, can only add the end

8. How to express an empty tuple

(,) Be careful to have commas in the middle

9.join usage

Split () splits the string into a list, and join () can stitch the elements in the list into strings

10. Decoding Code

11.setdefault usage

Python Basic Knowledge Vulnerability Summary

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.