Python Foundation One

Source: Internet
Author: User
About memory allocation issues

New definition string variable default new piece of new memory space

Other similar indexes, such as lists, tuples, or dictionary assignments, simply point the variable name to the same address space, as shown below

1 # #字符串新定义则开辟新的一块内存空间 2 >>> str1 = ' HoHo ' 3 >>> str2 = str1 4 >>> ID (STR1), id (str2) #查看内存对象地址, view The memory address, namely STR2 new open up memory space 5 (140297199501752, 140297199501752) #这里看到是一样的是由于python的一个内部机制导致的, if the string is large enough, it will be different, do not tangle 6 > >> str2 = ' Heihei ' 7 >>> str1 8 ' hoho ' 9 >>> str210 ' Heihei ' one >>> ID (STR1), id (str2) #看, internal Save address is not changed (140297199501752, 140297214622552) # #非字符器, such as list, tuple, dictionary assignment is actually just the new variable name (can be understood as a label) point to the same memory address, in the dictionary example, as shown in the following 15 >>> Dic1 = {' name ': ' HoHo '}16 >>> dic2 = dic117 >>> ID (DIC1), ID (dic2) 18 (140297199190088, 14029 7199190088) >>> Dic1 = {' name ': ' HoHo '}20 >>> dic2 = dic121 >>> ID (DIC1), id (dic2) #查看内存对象地址, send is now the same, so modify Dic2 in fact Dic1 also followed modified (140297199191752, 140297199191752) >>> dic2[' name '] = ' Heihei ' >>> dic225 {' name ': ' Heihei '}26 >>> dic127 {' name ': ' Heihei '}

list, tuple, and dictionary replication issues (shallow copy and deep copy use of copy module)

1, lists and tuples can be used for shallow copy of the tiles, you can also use the Copy module shallow copy (including dictionaries)

2. Deep copy using Copy.deepcopy () instance

1 >>> Import Copy 2 >>> list1 = [] 3 >>> list2 = List1 4 >>> list2[0] = 2 #list2改了, List1 changed. 5 >>> list1 6 [2, 2] 7 >>> list3 = list1[:] #浅复杂, make shallow copy with array slice 8 >>> list3 = Copy.copy (l IST1) 9 >>> ID (list1), id (LIST2), id (list3) #这里就可看到地址空间是不一样的10 (140297199250696, 140297199250696, 140297199247560) >>>
>>> list4 = [1,[2]] # #复杂结构必须用深复制13 >>> list5 = list4[:]14 >>> list515 [1, [2]]16 >>> ; LIST5[1][0] = 617 >>> list418 [1, [6]] #从这里可以看到内层的列表事实是没复制的, LIST4 also changed. >>> List6 = copy.deepcopy (list 4) #这里使用深复制20 >>> list6[1][0] = 821 >>> list622 [1, [8]]23 >>> list424 [1, [6]] #这里就可以看出已经复制的了

Common built-in functions

Python built-in functions are very much, remember the usual is OK, but will know how to see what built-in functions, function of the Help

3 steps under normal circumstances.

Type (variable)---> Get the class to which the variable belongs

Dir (class name)---> See what are the methods under the class, where similar __abs__ with a double underscore typically have alternative methods such as: __abs__ <=> ABS ()

Help (class name or function name)---> View function usage under class or view function usage directly

Plastic


1 >>> s,y = Divmod (7,3) # # Divmod return data, value (quotient, remainder), can be used for paging 2 >>> s,y3 (2, 1) 4 >>> a = -25 >>> ; ABS ( -2) #abs取绝对值6 >>> len (str ( -2)) #取速度长度8 2

View Code

Floating point


1 >>> a = 7.02 >>> divmod (a,3) 3 (2.0, 1.0) 4 >>> a = 7.2355 >>> a.__round__ (2) #四舍五入6 7 .247 >>> a.__trunc__ () #取整8 7

View Code

String


1 >>> str1 = ' This is a string ' 2 >>> ' was ' in str1 #成员判断 3 True 4 >>> str1[1:3] # slicing operation with index 5 ' Hi ' 6 >>> len (str1) #长度 7 8 >>> str1.find (' is ') #查找字符串, return index value 9 "Str1.find" (' is ', 3,9) 11 5 >>> str1.find (' ISS ') #没有找到返回-1, if index will error 13-114 >>> str1.index (' is ', 3) 516 >>> Str1.index (' ISS ') Traceback (most recent): "", line 1, valueerror:substring not FOUND20 >> > str1 = ' aaa ' >>> str1.strip () go blank, line, enter the ' AAA ' >>> str1.lstrip () ' AAA ' >>> str1. Rstrip () ' aaa ' >>> str1 = ' Duiqi ' #对齐操作28 >>> str1.ljust ' Duiqi ' >>> str1.ljust (+, ' * ') ' duiqi*************** ' >>> str1.rjust (+, ' * ') ' ***************duiqi ' >>> Str1.center (+, ' * ') ' *******duiqi******** ' >>> str1 = ' This is a string ' PNs >>> str1.split () # #分割操作3 8 [' This ', ' is ', ' a ', ' String ']39 >>> Str1.splitlines () [' This is a string ']41 >>> list1 = [1,2,3]42 >>> '. Join ([STR (i)-I in List 1]) #连接操作43 ' 1->2->3 ' >>> str145 ' This is a string ' [Str1.count] (' is ') #计数47 248 >>&gt ; Str1.replace (' is ', ' OS ') #替换49 ' thos os a string ' >>> str1.replace (' is ', ' OS ', 1) #替换, replacing only 1 "thos is a string ' 52 Str1.startswith (' Sub ') #以什么开头54 str1.endswith (' Sub ') #以什么结尾55 str1.lower () #转为小写56 str1.upper () #转为大写

View Code

Lists and tuples (tuples cannot be modified)


1 >>> lst1 = [' A '] 2 >>> lst1.append (' B ') #新增 3 >>> lst1 4 [' A ', ' B '] 5 >>> lst2 = [' C  ', ' d '] 6 >>> lst1.extend (lst2) #扩展新增 7 >>> lst1 8 [' A ', ' B ', ' C ', ' d '] 9 >>> lst1.insert (0, ' z ') #插入10 >>> lst111 [' Z ', ' a ', ' B ', ' C ', ' d ']12 >>> lst1.pop () #去除末尾13 ' d ' + >>> lst115 [' Z ', ' a ' , ' B ', ' C ']16 >>> lst1.remove (' z ') #删除指定元素17 >>> lst118 [' A ', ' B ', ' C ']19 >>> lst1 = [' A ', ' B ', ' C ', ' d ']20 >>> lst2 = lst1.copy () # Shallow copy python3 only a few >>> Lst2 = lst1.copy () >>> lst223 [' a ', ' B ', ' C ', ' d ']24 >>> lst2.clear () #清空列表25 >>> lst226 []27 >>> del lst2 #删除列表28 >>> ls t129 [' d ', ' C ', ' B ', ' A ']30 >>> lst1.sort () #排序31 >>> lst132 [' A ', ' B ', ' C ', ' d ']33 >>> Lst1.ap Pend (' a ') >>> lst1.count (' a ') #计数35 236 >>> lst137 [' A ', ' B ', ' C ', ' d ', ' a ']38 >>> len (lst1) #长度39 540 >>> LSt1.index (' a ') #索引41 042 >>> lst1.index (' A ', 1) #索引43 4

View Code

Dictionary


1 >>> dic1 = {' Key1 ': ' A ', ' Key2 ': ' B '} 2 >>> dic1.get (' Key1 ') #取字典值, no default return None, can also specify 3 ' a ' 4 >>& Gt Dic1.get (' Key3 ') 5 >>> dic1.items () 6 dict_items ([' Key2 ', ' B '), (' Key1 ', ' a ')]) #返回元组列表 7 >>> list (Dic1 . Items ()) 8 [(' Key2 ', ' B '), (' Key1 ', ' a ')] 9 >>> dic1.keys () #返回keys列表10 dict_keys ([' Key2 ', ' Key1 ']) &GT;&GT;&G T Dic1.values () #返回值列表12 dict_values ([' B ', ' a ']) >>> dic2 = dic1.copy () #浅复制14 >>> dic215 {' Key2 ': ' B ', ' key1 ': ' A '}16 >>> dic1[' key3 '] = ' C ' #赋值 (modified) + >>> dic118 {' Key2 ': ' B ', ' key1 ': ' A ', ' Key3 ': ' C '} >>> dic1.pop (' key1 ') #删除指定的key20 ' a ' >>> dic122 {' Key2 ': ' B ', ' Key3 ': ' C '}23 >>> dic1.get ( ' Key1 ', ' a ') #取值, did not return ' a ' to ' a ' >>> dic126 {' Key2 ': ' B ', ' Key3 ': ' C '}27 >>> dic1.setdefault (' Key1 ', ' a ' #设置默认 (seemingly useless) "a" >>> dic130 {' Key2 ': ' B ', ' key1 ': ' A ', ' Key3 ': ' C '}31 >>> dic3 = {' name ': ' Update ' }32 >>> diC1.update (DIC3) #更新33 >>> dic1 {' Key2 ': ' B ', ' Name ': ' Update ', ' key1 ': ' A ', ' Key3 ': ' C '}35 >>> del di C3 #删除36 >>> dic137 {' Key2 ': ' B ', ' Name ': ' Update ', ' key1 ': ' A ', ' Key3 ': ' C '}38 >>> len (dic1) #长度39 4

View Code

The above is the basic Python content, more related articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.