第一次使用:字符串操作
In [1]: lst=[11,12,13,7,1,3,2,2,5,6,10,7] In [2]: lst Out[2]: [11, 12, 13, 7, 1, 3, 2, 2, 5, 6, 10, 7] In [3]: lst1=[11,12,13,7,1,3,2,2,5,6,10,7] In [4]: lst1 Out[4]: [11, 12, 13, 7, 1, 3, 2, 2, 5, 6, 10, 7] In [5]: lst1.pop() Out[5]: 7 In [6]: lst1.reverse() In [7]: lst1 Out[7]: [10, 6, 5, 2, 2, 3, 1, 7, 13, 12, 11] In [8]: lst.sort() In [9]: lst Out[9]: [1, 2, 2, 3, 5, 6, 7, 7, 10, 11, 12, 13] In [11]: lst Out[11]: [1, 2, 2, 3, 5, 6, 7, 7, 10, 11, 12, 13] In [12]: lst.sort(reverse=True) In [13]: lst Out[13]: [13, 12, 11, 10, 7, 7, 6, 5, 3, 2, 2, 1] In [14]: lst.sort(key=str) In [15]: lst Out[15]: [1, 10, 11, 12, 13, 2, 2, 3, 5, 6, 7, 7] In [16]: lst.append('a') In [17]: lst Out[17]: [1, 10, 11, 12, 13, 2, 2, 3, 5, 6, 7, 7, 'a'] In [18]: lst.sort(key=str) In [19]: lst Out[19]: [1, 10, 11, 12, 13, 2, 2, 3, 5, 6, 7, 7, 'a'] In [20]: lst.sort(key=str,reverse=True) In [21]: lst Out[21]: ['a', 7, 7, 6, 5, 3, 2, 2, 13, 12, 11, 10, 1] In [23]: lst Out[23]: ['a', 7, 7, 6, 5, 3, 2, 2, 13, 12, 11, 10, 1] In [24]: 3 in lst Out[24]: True In [25]: 'a' in lst Out[25]: True In [26]: if 30 in lst: ...: pass Out[26]: '' In [27]: for x in lst: ...: print(x) In [28]: lst Out[28]: ['a', 7, 7, 6, 5, 3, 2, 2, 13, 12, 11, 10, 1] In [30]: if 7 in lst: ...: print('qq') ...: qq In [31]: for x in [1,2,3]: ...: print(x) ...: 1 2 3 In [32]: lst0=list(range(4)) In [33]: id(lst0) Out[33]: 1193702777928 In [35]: hash(id(lst0)) Out[35]: 1193702777928 In [36]: lst1=list(range(4)) In [37]: id(lst1) Out[37]: 1193702003144 In [38]: lst0==lst1 Out[38]: True In [39]: hash((1,2)) Out[39]: 3713081631934410656 In [40]: lst1=lst0 In [41]: lst1[2]=10 In [42]: lst1 Out[42]: [0, 1, 10, 3] In [43]: lst0 Out[43]: [0, 1, 10, 3] In [44]: lst0[3]=12 In [45]: lst0 Out[45]: [0, 1, 10, 12] In [46]: lst1 Out[46]: [0, 1, 10, 12] In [47]: lst0=list(range(4)) In [48]: lst0 Out[48]: [0, 1, 2, 3] In [49]: lst5=lst0.copy() In [50]: lst5 Out[50]: [0, 1, 2, 3] In [51]: lst5==lst0 Out[51]: True In [52]: lst0=[1,[2,3,4],5] In [53]: lst5=lst0.copy() In [54]: lst5==lst0 Out[54]: True In [55]: lst5[2]=10 In [56]: lst5==lst0 Out[56]: False In [57]: lst5 Out[57]: [1, [2, 3, 4], 10] In [58]: lst0 Out[58]: [1, [2, 3, 4], 5] In [59]: lst5[2]=5 In [60]: lst5==lst0 Out[60]: True In [61]: lst5[1][1]=20 In [62]: lst5==lst0 Out[62]: True In [63]: t=tuple() In [64]: t Out[64]: () In [65]: t=() In [66]: t Out[66]: () In [67]: t=tuple(range(1,7,2)) In [68]: t Out[68]: (1, 3, 5) In [69]: t=(2,4,4,[1,2,3],2,5) In [70]: t Out[70]: (2, 4, 4, [1, 2, 3], 2, 5) In [71]: t[3][0]=6 In [72]: t Out[72]: (2, 4, 4, [6, 2, 3], 2, 5) In [73]: t=(1,) In [74]: t Out[74]: (1,) In [75]: t=(1,)*5 In [76]: t Out[76]: (1, 1, 1, 1, 1) In [77]: t=(1,2,3)*6 In [78]: t Out[78]: (1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3) In [79]: t=tuple(range(1,7,2)) In [80]: t Out[80]: (1, 3, 5) In [81]: for i in t: ...: print(i) ...: 1 3 5 In [83]: from collections import namedtuple In [84]: Point=namedtuple('_Point',['x','y']) In [85]: p=Point(11,22) In [86]: p Out[86]: _Point(x=11, y=22) In [87]: Student=namedtuple('Student','name age') In [88]: tom=Student('tom',20) In [89]: jerry=Student('jerry',18) In [90]: tom.name Out[90]: 'tom' In [91]: jerry.age Out[91]: 18 In [92]: s1='string' In [93]: s1 Out[93]: 'string' In [94]: s2="string2" In [95]: s2 Out[95]: 'string2' In [96]: s3='''this's a "string" ''' In [97]: s3 Out[97]: 'this\'s a "string" ' In [98]: s4='hello \n magedu.com' In [99]: s4 Out[99]: 'hello \n magedu.com' In [100]: s5=r"hello \n magedu.com" In [101]: s5 Out[101]: 'hello \\n magedu.com' In [102]: s6='c:\windows\nt' In [103]: s6 Out[103]: 'c:\\windows\nt' In [104]: s7=R"c:\windows\nt" In [105]: s7 Out[105]: 'c:\\windows\\nt' In [106]: s8='c:\windows\\nt' In [107]: s8 Out[107]: 'c:\\windows\\nt' In [108]: sql="""select * from user where name='tom'""" In [109]: sql Out[109]: "select * from user where name='tom'" In [110]: ============================= In [1]: lst=['1','2','3'] In [4]: lst Out[4]: ['1', '2', '3'] In [5]: s1="I'm \ta super student." In [6]: s1 Out[6]: "I'm \ta super student." In [7]: s1.split() Out[7]: ["I'm", 'a', 'super', 'student.'] In [8]: s1.split('s') Out[8]: ["I'm \ta ", 'uper ', 'tudent.'] In [9]: s1.split('super') Out[9]: ["I'm \ta ", ' student.'] In [10]: s1.split('super ') Out[10]: ["I'm \ta ", 'student.'] In [11]: s1.split(' ') Out[11]: ["I'm", '\ta', 'super', 'student.'] In [12]: s1.split(' ',maxsplit=2) Out[12]: ["I'm", '\ta', 'super student.'] In [13]: s1.split('\t',maxsplit=2) Out[13]: ["I'm ", 'a super student.'] In [14]: s1.partition('s') Out[14]: ("I'm \ta ", 's', 'uper student.') In [15]: s1.partition('stu') Out[15]: ("I'm \ta super ", 'stu', 'dent.') In [17]: s1.partition('abc') Out[17]: ("I'm \ta super student.", '', '') In [18]: s2='abc' In [19]: s2.center(30) Out[19]: ' abc ' In [20]: s2.center(30,'=') Out[20]: '=============abc==============' In [21]: s2.zfill(30) Out[21]: '000000000000000000000000000abc' In [22]: s="\r \n \t Hello Python \n \t" In [23]: s.strip() Out[23]: 'Hello Python' In [24]: s3="I am very very sorry" In [25]: s3.strip("Iy") Out[25]: ' am very very sorr' In [26]: s3.strip("Iy ") Out[26]: 'am very very sorr' In [27]: s Out[27]: '\r \n \t Hello Python \n \t' In [28]: print(s) Hello Python In [29]: s3 Out[29]: 'I am very very sorry' In [30]: s3.find('very') Out[30]: 5 In [31]: s.find('very',5) Out[31]: -1 In [32]: s3.find('very',5) Out[32]: 5 In [33]: s3.find('very',6,13) Out[33]: -1 TypeError: 'str' object is not callable In [35]: s3[-1] Out[35]: 'y' In [36]: s3.rfind('very',10) Out[36]: 10 In [37]: s3.rfind('very',10,15) Out[37]: 10 In [38]: s3.rfind('very',-10,-1) Out[38]: 10 In [39]: '{0}*{1}={2:<2}'.format(3,2,2*3) Out[39]: '3*2=6 ' In [40]: '{0}*{1}={2:<2}'.format(3,2,2*3) ...: Out[40]: '3*2=6 ' In [41]: '{0:.2f}'.format(1/3) Out[41]: '0.33' In [42]: '1{0:b}'.format(10) #二进制 Out[42]: '11010' IIn [43]: In [43]: '1{0:b}'.format(16) Out[43]: '110000' In [44]: '{0:0}'.format(10) #八进制 Out[44]: '10' In [45]: '{0: 0}'.format(10) #八进制 Out[45]: ' 10' In [46]: '{0:o}'.format(10) #八进制 Out[46]: '12' In [47]: '{0:x}'.format(10) #16进制 Out[47]: 'a' In [48]: '{:,}'.format(12369132698) #千分位格式化 Out[48]: '12,369,132,698' In [50]: name='World' In [54]: name='World' In [55]: 'Hello {}'.format(name) Out[55]: 'Hello World' In [56]: coord=(3,5) In [57]: 'X:{0[0]};Y:{0[1]}'.format(coord) Out[57]: 'X:3;Y:5' In [58]: "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2') Out[58]: "repr() shows quotes: 'test1'; str() doesn't: test2" In [59]: '{:<30}'.format('left aligned') Out[59]: 'left aligned ' In [60]: '{:>30}'.format('right aligned') Out[60]: ' right aligned' In [61]: '{:^30}'.format('centered') Out[61]: ' centered ' In [62]: '{:*^30}'.format('centered') # use '*' as a fill char Out[62]: '***********centered***********' In [63]: dir(1-3j) Out[63]: ['__abs__','__add__', .... '__truediv__', 'conjugate', 'imag', 'real'] In [64]: ('The complex number {0} is formed from the real part {0.real} ' ...: 'and the imaginary part {0.imag}.').format(1-2j) Out[64]: 'The complex number (1-2j) is formed from the real part 1.0 and the imaginary part -2.0.' In [65]: class Point: ...: def __init__(self,x,y): ...: self.x,self.y=x,y ...: def __str__(self): ...: return 'Point({self.x},{self.y})'.format(self=self) ...: ...: str(Point(5,4)) ...: ...: Out[65]: 'Point(5,4)' In [66]: '{:,}'.format(1234567890) Out[66]: '1,234,567,890' In [67]: a=18 In [68]: b=22 In [69]: '{:.2%}'.format(a/b) Out[69]: '81.82%' In [70]: import datetime In [71]: d=datetime.datetime(2010,7,4,12,15,35) In [72]: '{:%Y-%m-%d %H:%M:%S}'.format(d) Out[72]: '2010-07-04 12:15:35' In [73]: width=5 In [74]: for num in range(5,12): ...: for base in 'dXob': #分别是10/16/8/2进制 ...: print('{0:{width}{base}}'.format(num,base=base,width=width),end=' ') ...: print() ...: 5 5 5 101 6 6 6 110 7 7 7 111 8 8 10 1000 9 9 11 1001 10 A 12 1010 11 B 13 1011 In [75]: D:\djangoPro\sample\1>git clone [email protected]:lindezhi/python-fundamentals-.git Cloning into 'python-fundamentals-'... remote: Enumerating objects: 81, done. remote: Total 81 (delta 0), reused 0 (delta 0), pack-reused 81R Receiving objects: 100% (81/81), 181.61 KiB | 743.00 KiB/s, done. D:\djangoPro\sample\1>git clone [email protected]:fans180/python-basic-learning.git Cloning into 'python-basic-learning'... remote: Enumerating objects: 77, done. remote: Counting objects: 100% (29/29), done. remote: Compressing objects: 100% (29/29), done. remote: Total 77 (delta 7), reused 0 (delta 0), pack-reused 48 Receiving objects: 100% (77/77), 35.41 KiB | 659.00 KiB/s, done. Resolving deltas: 100% (9/9), done. D:\djangoPro\sample\1>git clone [email protected]:xiaoguangding/python-base.git Cloning into 'python-base'... remote: Enumerating objects: 595, done. remote: Counting objects: 100% (468/468), done. remote: Compressing objects: 100% (449/449), done. ^Cceiving objects: 6% (37/595), 186.26 MiB | 1.16 MiB/s D:\djangoPro\sample\1>git clone [email protected]:AnonyEast/python.git Cloning into 'python'... remote: Enumerating objects: 458, done. remote: Counting objects: 100% (321/321), done. remote: Compressing objects: 100% (291/291), done. Receiving objects: 91% (417/458), 10.42 MiB | 1005.00 KiB/s 37 eceiving objects: 90% (413/458), 10.42 MiB | 1005.00 KiB/s Receiving objects: 100% (458/458), 10.75 MiB | 1.08 MiB/s, done. Resolving deltas: 100% (126/126), done. D:\djangoPro\sample\1>git clone [email protected]:huiyuandev/python-fundamentals.git Cloning into 'python-fundamentals'... remote: Enumerating objects: 24, done. remote: Counting objects: 100% (24/24), done. remote: Compressing objects: 100% (23/23), done. remote: Total 24 (delta 2), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (24/24), 4.03 KiB | 589.00 KiB/s, done. Resolving deltas: 100% (2/2), done. D:\djangoPro\sample\1>git clone [email protected]:shuai222/python-basic-learning.git Cloning into 'python-basic-learning'... remote: Enumerating objects: 1894, done. remote: Total 1894 (delta 0), reused 0 (delta 0), pack-reused 1894 Receiving objects: 100% (1894/1894), 72.20 MiB | 1.14 MiB/s, done. Resolving deltas: 100% (137/137), done. Checking out files: 100% (288/288), done. D:\djangoPro\sample\1>git clone [email protected]:nzp_dany/python.git Cloning into 'python'... remote: Enumerating objects: 4, done. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 4 Receiving objects: 100% (4/4), done. D:\djangoPro\sample\1>ipython Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: bytes(range(100)) Out[1]: b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f ! "#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abc' In [2]: len(bytes(range(100))) Out[2]: 100 In [3]: bytes(range(500)) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-f32657ac3240> in <module>() ----> 1 bytes(range(500)) ValueError: bytes must be in range(0, 256) In [4]: bytes('abv','utf8') Out[4]: b'abv' In [5]: bytes('你好','utf8') Out[5]: b'\xe4\xbd\xa0\xe5\xa5\xbd' InIn [6]: '' Out[6]: '' In [7]: '你好'.encode() Out[7]: b'\xe4\xbd\xa0\xe5\xa5\xbd' In [8]: a='acd'.encode() In [9]: a Out[9]: b'acd' In [10]: a.decode() Out[10]: 'acd' In [11]: b=bytes(a) In [12]: b==a Out[12]: True In [14]: id(b)==id(a) Out[14]: True In [15]: id(b) is id(a) Out[15]: False In [16]: b Out[16]: b'acd' In [17]: a Out[17]: b'acd' In [18]: b.split(b'c') Out[18]: [b'a', b'd'] In [19]: bytes.fromhex('6162 09 6a 6b00') Out[19]: b'ab\tjk\x00' In [20]: 'abc'.encode().hex() Out[20]: '616263' In [21]: b'abcde'[2] Out[21]: 99 In [22]: ret=bytearray('abcd'.encode()) In [23]: ret Out[23]: bytearray(b'abcd') In [26]: ?a Type: bytes String form: b'acd' Length: 3 Docstring: bytes(iterable_of_ints) -> bytes bytes(string, encoding[, errors]) -> bytes bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer bytes(int) -> bytes object of size given by the parameter initialized with null bytes In [27]: abs? Signature: abs(x, /) Docstring: Return the absolute value of the argument. Type: builtin_function_or_method In [28]: _a --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-28-04695a6e8c46> in <module>() ----> 1 _a NameError: name '_a' is not defined In [29]: -a --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-29-bd5f19dddee1> in <module>() ----> 1 -a TypeError: bad operand type for unary -: 'bytes' In [30]: q=3 In [31]: _ Out[31]: bytearray(b'abcd') In [32]: __ Out[32]: bytearray(b'abcd') In [33]: ___ Out[33]: bytearray(b'abcd') In [34]: _dh Out[34]: ['D:\\djangoPro\\sample\\1'] In [35]: _oh Out[35]: {1: b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$% &\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abc', 2: 100, 4: b'abv', 5: b'\xe4\xbd\xa0\xe5\xa5\xbd', 6: '', 7: b'\xe4\xbd\xa0\xe5\xa5\xbd', 9: b'acd', 10: 'acd', 12: True, 14: True, 15: False, 16: b'acd', 17: b'acd', 18: [b'a', b'd'], 19: b'ab\tjk\x00', 20: '616263', 21: 99, 23: bytearray(b'abcd'), 31: bytearray(b'abcd'), 32: bytearray(b'abcd'), 33: bytearray(b'abcd'), 34: ['D:\\djangoPro\\sample\\1']} In [36]:
第二次:
标签:...,12,10,笔记,lst,done,Ipyton,1003,Out From: https://www.cnblogs.com/mengdie1978/p/16750917.html